2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import java.nio.ByteBuffer;
\r
12 import com.ibm.icu.impl.Utility;
\r
16 * A simple utility class to wrap a byte array.
\r
19 * Generally passed as an argument object into a method. The method takes
\r
20 * responsibility of writing into the internal byte array and increasing its
\r
21 * size when necessary.
\r
26 public class ByteArrayWrapper implements Comparable<ByteArrayWrapper>
\r
28 // public data member ------------------------------------------------
\r
31 * Internal byte array.
\r
34 public byte[] bytes;
\r
37 * Size of the internal byte array used.
\r
38 * Different from bytes.length, size will be <= bytes.length.
\r
39 * Semantics of size is similar to java.util.Vector.size().
\r
44 // public constructor ------------------------------------------------
\r
47 * Construct a new ByteArrayWrapper with no data.
\r
50 public ByteArrayWrapper() {
\r
51 // leave bytes null, don't allocate twice
\r
55 * Construct a new ByteArrayWrapper from a byte array and size
\r
56 * @param bytesToAdopt the byte array to adopt
\r
57 * @param size the length of valid data in the byte array
\r
58 * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
\r
59 * size < 0, or size > bytesToAdopt.length.
\r
62 public ByteArrayWrapper(byte[] bytesToAdopt, int size) {
\r
63 if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {
\r
64 throw new IndexOutOfBoundsException("illegal size: " + size);
\r
66 this.bytes = bytesToAdopt;
\r
71 * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
\r
72 * @param source the ByteBuffer from which to get the data.
\r
75 public ByteArrayWrapper(ByteBuffer source) {
\r
76 size = source.limit();
\r
77 bytes = new byte[size];
\r
78 source.get(bytes,0,size);
\r
82 * Create from ByteBuffer
\r
84 public ByteArrayWrapper(ByteArrayWrapper source) {
\r
86 bytes = new byte[size];
\r
87 copyBytes(source.bytes, 0, bytes, 0, size);
\r
92 * create from byte buffer
\r
96 public ByteArrayWrapper(byte[] src, int start, int limit) {
\r
97 size = limit - start;
\r
98 bytes = new byte[size];
\r
99 copyBytes(src, start, bytes, 0, size);
\r
103 // public methods ----------------------------------------------------
\r
106 * Ensure that the internal byte array is at least of length capacity.
\r
107 * If the byte array is null or its length is less than capacity, a new
\r
108 * byte array of length capacity will be allocated.
\r
109 * The contents of the array (between 0 and size) remain unchanged.
\r
110 * @param capacity minimum length of internal byte array.
\r
111 * @return this ByteArrayWrapper
\r
114 public ByteArrayWrapper ensureCapacity(int capacity)
\r
116 if (bytes == null || bytes.length < capacity) {
\r
117 byte[] newbytes = new byte[capacity];
\r
118 copyBytes(bytes, 0, newbytes, 0, size);
\r
125 * Set the internal byte array from offset 0 to (limit - start) with the
\r
126 * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new
\r
127 * byte array of length (limit - start) will be allocated.
\r
128 * This resets the size of the internal byte array to (limit - start).
\r
129 * @param src source byte array to copy from
\r
130 * @param start start offset of src to copy from
\r
131 * @param limit end + 1 offset of src to copy from
\r
132 * @return this ByteArrayWrapper
\r
135 public final ByteArrayWrapper set(byte[] src, int start, int limit)
\r
138 append(src, start, limit);
\r
143 public final ByteArrayWrapper get(byte[] target, int start, int limit)
\r
145 int len = limit - start;
\r
146 if (len > size) throw new IllegalArgumentException("limit too long");
\r
147 copyBytes(bytes, 0, target, start, len);
\r
153 * Appends the internal byte array from offset size with the
\r
154 * contents of src from offset start to limit. This increases the size of
\r
155 * the internal byte array to (size + limit - start).
\r
156 * @param src source byte array to copy from
\r
157 * @param start start offset of src to copy from
\r
158 * @param limit end + 1 offset of src to copy from
\r
159 * @return this ByteArrayWrapper
\r
162 public final ByteArrayWrapper append(byte[] src, int start, int limit)
\r
164 int len = limit - start;
\r
165 ensureCapacity(size + len);
\r
166 copyBytes(src, start, bytes, size, len);
\r
172 public final ByteArrayWrapper append(ByteArrayWrapper other)
\r
174 return append(other.bytes, 0, other.size);
\r
179 * Releases the internal byte array to the caller, resets the internal
\r
180 * byte array to null and its size to 0.
\r
181 * @return internal byte array.
\r
184 public final byte[] releaseBytes()
\r
186 byte result[] = bytes;
\r
192 // Boilerplate ----------------------------------------------------
\r
195 * Returns string value for debugging
\r
198 public String toString() {
\r
199 StringBuilder result = new StringBuilder();
\r
200 for (int i = 0; i < size; ++i) {
\r
201 if (i != 0) result.append(" ");
\r
202 result.append(Utility.hex(bytes[i]&0xFF,2));
\r
204 return result.toString();
\r
208 * Return true if the bytes in each wrapper are equal.
\r
209 * @param other the object to compare to.
\r
210 * @return true if the two objects are equal.
\r
213 public boolean equals(Object other) {
\r
214 if (this == other) return true;
\r
215 if (other == null) return false;
\r
217 ByteArrayWrapper that = (ByteArrayWrapper)other;
\r
218 if (size != that.size) return false;
\r
219 for (int i = 0; i < size; ++i) {
\r
220 if (bytes[i] != that.bytes[i]) return false;
\r
224 catch (ClassCastException e) {
\r
230 * Return the hashcode.
\r
231 * @return the hashcode.
\r
234 public int hashCode() {
\r
235 int result = bytes.length;
\r
236 for (int i = 0; i < size; ++i) {
\r
237 result = 37*result + bytes[i];
\r
243 * Compare this object to another ByteArrayWrapper, which must not be null.
\r
244 * @param other the object to compare to.
\r
245 * @return a value <0, 0, or >0 as this compares less than, equal to, or
\r
246 * greater than other.
\r
247 * @throws ClassCastException if the other object is not a ByteArrayWrapper
\r
250 public int compareTo(ByteArrayWrapper other) {
\r
251 if (this == other) return 0;
\r
252 int minSize = size < other.size ? size : other.size;
\r
253 for (int i = 0; i < minSize; ++i) {
\r
254 if (bytes[i] != other.bytes[i]) {
\r
255 return (bytes[i] & 0xFF) - (other.bytes[i] & 0xFF);
\r
258 return size - other.size;
\r
261 // private methods -----------------------------------------------------
\r
264 * Copies the contents of src byte array from offset srcoff to the
\r
265 * target of tgt byte array at the offset tgtoff.
\r
266 * @param src source byte array to copy from
\r
267 * @param srcoff start offset of src to copy from
\r
268 * @param tgt target byte array to copy to
\r
269 * @param tgtoff start offset of tgt to copy to
\r
270 * @param length size of contents to copy
\r
272 private static final void copyBytes(byte[] src, int srcoff, byte[] tgt,
\r
273 int tgtoff, int length) {
\r
275 for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {
\r
280 System.arraycopy(src, srcoff, tgt, tgtoff, length);
\r