3 *******************************************************************************
\r
4 * Copyright (C) 1996-2009, International Business Machines Corporation and *
\r
5 * others. All Rights Reserved. *
\r
6 *******************************************************************************
\r
9 package com.ibm.icu.util;
\r
11 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
12 //##import com.ibm.icu.impl.ByteBuffer;
\r
14 import java.nio.ByteBuffer;
\r
16 import com.ibm.icu.impl.Utility;
\r
20 * A simple utility class to wrap a byte array.
\r
23 * Generally passed as an argument object into a method. The method takes
\r
24 * responsibility of writing into the internal byte array and increasing its
\r
25 * size when necessary.
\r
30 public class ByteArrayWrapper implements Comparable
\r
32 // public data member ------------------------------------------------
\r
35 * Internal byte array.
\r
38 public byte[] bytes;
\r
41 * Size of the internal byte array used.
\r
42 * Different from bytes.length, size will be <= bytes.length.
\r
43 * Semantics of size is similar to java.util.Vector.size().
\r
48 // public constructor ------------------------------------------------
\r
51 * Construct a new ByteArrayWrapper with no data.
\r
54 public ByteArrayWrapper() {
\r
55 // leave bytes null, don't allocate twice
\r
59 * Construct a new ByteArrayWrapper from a byte array and size
\r
60 * @param bytesToAdopt the byte array to adopt
\r
61 * @param size the length of valid data in the byte array
\r
62 * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
\r
63 * size < 0, or size > bytesToAdopt.length.
\r
66 public ByteArrayWrapper(byte[] bytesToAdopt, int size) {
\r
67 if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {
\r
68 throw new IndexOutOfBoundsException("illegal size: " + size);
\r
70 this.bytes = bytesToAdopt;
\r
75 * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
\r
76 * @param source the ByteBuffer from which to get the data.
\r
79 public ByteArrayWrapper(ByteBuffer source) {
\r
80 size = source.limit();
\r
81 bytes = new byte[size];
\r
82 source.get(bytes,0,size);
\r
86 * Create from ByteBuffer
\r
88 public ByteArrayWrapper(ByteArrayWrapper source) {
\r
90 bytes = new byte[size];
\r
91 copyBytes(source.bytes, 0, bytes, 0, size);
\r
96 * create from byte buffer
\r
100 public ByteArrayWrapper(byte[] src, int start, int limit) {
\r
101 size = limit - start;
\r
102 bytes = new byte[size];
\r
103 copyBytes(src, start, bytes, 0, size);
\r
107 // public methods ----------------------------------------------------
\r
110 * Ensure that the internal byte array is at least of length capacity.
\r
111 * If the byte array is null or its length is less than capacity, a new
\r
112 * byte array of length capacity will be allocated.
\r
113 * The contents of the array (between 0 and size) remain unchanged.
\r
114 * @param capacity minimum length of internal byte array.
\r
115 * @return this ByteArrayWrapper
\r
118 public ByteArrayWrapper ensureCapacity(int capacity)
\r
120 if (bytes == null || bytes.length < capacity) {
\r
121 byte[] newbytes = new byte[capacity];
\r
122 copyBytes(bytes, 0, newbytes, 0, size);
\r
129 * Set the internal byte array from offset 0 to (limit - start) with the
\r
130 * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new
\r
131 * byte array of length (limit - start) will be allocated.
\r
132 * This resets the size of the internal byte array to (limit - start).
\r
133 * @param src source byte array to copy from
\r
134 * @param start start offset of src to copy from
\r
135 * @param limit end + 1 offset of src to copy from
\r
136 * @return this ByteArrayWrapper
\r
139 public final ByteArrayWrapper set(byte[] src, int start, int limit)
\r
142 append(src, start, limit);
\r
147 public final ByteArrayWrapper get(byte[] target, int start, int limit)
\r
149 int len = limit - start;
\r
150 if (len > size) throw new IllegalArgumentException("limit too long");
\r
151 copyBytes(bytes, 0, target, start, len);
\r
157 * Appends the internal byte array from offset size with the
\r
158 * contents of src from offset start to limit. This increases the size of
\r
159 * the internal byte array to (size + limit - start).
\r
160 * @param src source byte array to copy from
\r
161 * @param start start offset of src to copy from
\r
162 * @param limit end + 1 offset of src to copy from
\r
163 * @return this ByteArrayWrapper
\r
166 public final ByteArrayWrapper append(byte[] src, int start, int limit)
\r
168 int len = limit - start;
\r
169 ensureCapacity(size + len);
\r
170 copyBytes(src, start, bytes, size, len);
\r
176 public final ByteArrayWrapper append(ByteArrayWrapper other)
\r
178 return append(other.bytes, 0, other.size);
\r
183 * Releases the internal byte array to the caller, resets the internal
\r
184 * byte array to null and its size to 0.
\r
185 * @return internal byte array.
\r
188 public final byte[] releaseBytes()
\r
190 byte result[] = bytes;
\r
196 // Boilerplate ----------------------------------------------------
\r
199 * Returns string value for debugging
\r
202 public String toString() {
\r
203 StringBuffer result = new StringBuffer();
\r
204 for (int i = 0; i < size; ++i) {
\r
205 if (i != 0) result.append(" ");
\r
206 result.append(Utility.hex(bytes[i]&0xFF,2));
\r
208 return result.toString();
\r
212 * Return true if the bytes in each wrapper are equal.
\r
213 * @param other the object to compare to.
\r
214 * @return true if the two objects are equal.
\r
217 public boolean equals(Object other) {
\r
218 if (this == other) return true;
\r
219 if (other == null) return false;
\r
221 ByteArrayWrapper that = (ByteArrayWrapper)other;
\r
222 if (size != that.size) return false;
\r
223 for (int i = 0; i < size; ++i) {
\r
224 if (bytes[i] != that.bytes[i]) return false;
\r
228 catch (ClassCastException e) {
\r
234 * Return the hashcode.
\r
235 * @return the hashcode.
\r
238 public int hashCode() {
\r
239 int result = bytes.length;
\r
240 for (int i = 0; i < size; ++i) {
\r
241 result = 37*result + bytes[i];
\r
247 * Compare this object to another ByteArrayWrapper, which must not be null.
\r
248 * @param other the object to compare to.
\r
249 * @return a value <0, 0, or >0 as this compares less than, equal to, or
\r
250 * greater than other.
\r
251 * @throws ClassCastException if the other object is not a ByteArrayWrapper
\r
254 public int compareTo(Object other) {
\r
255 if (this == other) return 0;
\r
256 ByteArrayWrapper that = (ByteArrayWrapper) other;
\r
257 int minSize = size < that.size ? size : that.size;
\r
258 for (int i = 0; i < minSize; ++i) {
\r
259 if (bytes[i] != that.bytes[i]) {
\r
260 return (bytes[i] & 0xFF) - (that.bytes[i] & 0xFF);
\r
263 return size - that.size;
\r
266 // private methods -----------------------------------------------------
\r
269 * Copies the contents of src byte array from offset srcoff to the
\r
270 * target of tgt byte array at the offset tgtoff.
\r
271 * @param src source byte array to copy from
\r
272 * @param srcoff start offset of src to copy from
\r
273 * @param tgt target byte array to copy to
\r
274 * @param tgtoff start offset of tgt to copy to
\r
275 * @param length size of contents to copy
\r
277 private static final void copyBytes(byte[] src, int srcoff, byte[] tgt,
\r
278 int tgtoff, int length) {
\r
280 for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {
\r
285 System.arraycopy(src, srcoff, tgt, tgtoff, length);
\r