KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > ByteArrayWrapper


1 //##header 1189099963000 FOUNDATION
2
/**
3  *******************************************************************************
4  * Copyright (C) 1996-2006, International Business Machines Corporation and *
5  * others. All Rights Reserved. *
6  *******************************************************************************
7  */

8
9 package com.ibm.icu.util;
10
11 //#ifndef FOUNDATION
12
//##import java.nio.ByteBuffer;
13
//#else
14
import com.ibm.icu.impl.ByteBuffer;
15 //#endif
16
import com.ibm.icu.impl.Utility;
17
18 /**
19  * <p>
20  * A simple utility class to wrap a byte array.
21  * </p>
22  * <p>
23  * Generally passed as an argument object into a method. The method takes
24  * responsibility of writing into the internal byte array and increasing its
25  * size when necessary.
26  * </p>
27  * @author syn wee
28  * @stable ICU 2.8
29  */

30 public class ByteArrayWrapper implements Comparable JavaDoc
31 {
32     // public data member ------------------------------------------------
33

34     /**
35      * Internal byte array.
36      * @stable ICU 2.8
37      */

38     public byte[] bytes;
39
40     /**
41      * Size of the internal byte array used.
42      * Different from bytes.length, size will be &lt;= bytes.length.
43      * Semantics of size is similar to java.util.Vector.size().
44      * @stable ICU 2.8
45      */

46     public int size;
47     
48     // public constructor ------------------------------------------------
49

50     /**
51      * Construct a new ByteArrayWrapper with no data.
52      * @stable ICU 2.8
53      */

54     public ByteArrayWrapper() {
55         // leave bytes null, don't allocate twice
56
}
57
58     /**
59      * Construct a new ByteArrayWrapper from a byte array and size
60      * @param bytesToAdopt the byte array to adopt
61      * @param size the length of valid data in the byte array
62      * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
63      * size < 0, or size > bytesToAdopt.length.
64      * @draft ICU 3.2
65      * @provisional This API might change or be removed in a future release.
66      */

67     public ByteArrayWrapper(byte[] bytesToAdopt, int size) {
68         if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {
69             throw new IndexOutOfBoundsException JavaDoc("illegal size: " + size);
70         }
71         this.bytes = bytesToAdopt;
72         this.size = size;
73     }
74
75     /**
76      * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
77      * @param source the ByteBuffer from which to get the data.
78      * @draft ICU 3.2
79      * @provisional This API might change or be removed in a future release.
80      */

81     public ByteArrayWrapper(ByteBuffer source) {
82         size = source.limit();
83         bytes = new byte[size];
84         source.get(bytes,0,size);
85     }
86
87     /**
88      * Create from ByteBuffer
89      * @param byteBuffer
90     public ByteArrayWrapper(ByteArrayWrapper source) {
91         size = source.size;
92         bytes = new byte[size];
93         copyBytes(source.bytes, 0, bytes, 0, size);
94     }
95      */

96
97     /**
98      * create from byte buffer
99      * @param src
100      * @param start
101      * @param limit
102     public ByteArrayWrapper(byte[] src, int start, int limit) {
103         size = limit - start;
104         bytes = new byte[size];
105         copyBytes(src, start, bytes, 0, size);
106     }
107      */

108
109     // public methods ----------------------------------------------------
110

111     /**
112      * Ensure that the internal byte array is at least of length capacity.
113      * If the byte array is null or its length is less than capacity, a new
114      * byte array of length capacity will be allocated.
115      * The contents of the array (between 0 and size) remain unchanged.
116      * @param capacity minimum length of internal byte array.
117      * @return this ByteArrayWrapper
118      * @draft ICU 3.2
119      * @provisional This API might change or be removed in a future release.
120      */

121     public ByteArrayWrapper ensureCapacity(int capacity)
122     {
123         if (bytes == null || bytes.length < capacity) {
124             byte[] newbytes = new byte[capacity];
125             copyBytes(bytes, 0, newbytes, 0, size);
126             bytes = newbytes;
127         }
128         return this;
129     }
130     
131     /**
132      * Set the internal byte array from offset 0 to (limit - start) with the
133      * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new
134      * byte array of length (limit - start) will be allocated.
135      * This resets the size of the internal byte array to (limit - start).
136      * @param src source byte array to copy from
137      * @param start start offset of src to copy from
138      * @param limit end + 1 offset of src to copy from
139      * @return this ByteArrayWrapper
140      * @draft ICU 3.2
141      * @provisional This API might change or be removed in a future release.
142      */

143     public final ByteArrayWrapper set(byte[] src, int start, int limit)
144     {
145         size = 0;
146         append(src, start, limit);
147         return this;
148     }
149     
150     /*
151     public final ByteArrayWrapper get(byte[] target, int start, int limit)
152     {
153         int len = limit - start;
154         if (len > size) throw new IllegalArgumentException("limit too long");
155         copyBytes(bytes, 0, target, start, len);
156         return this;
157     }
158     */

159
160     /**
161      * Appends the internal byte array from offset size with the
162      * contents of src from offset start to limit. This increases the size of
163      * the internal byte array to (size + limit - start).
164      * @param src source byte array to copy from
165      * @param start start offset of src to copy from
166      * @param limit end + 1 offset of src to copy from
167      * @return this ByteArrayWrapper
168      * @draft ICU 3.2
169      * @provisional This API might change or be removed in a future release.
170      */

171     public final ByteArrayWrapper append(byte[] src, int start, int limit)
172     {
173         int len = limit - start;
174         ensureCapacity(size + len);
175         copyBytes(src, start, bytes, size, len);
176         size += len;
177         return this;
178     }
179
180     /*
181     public final ByteArrayWrapper append(ByteArrayWrapper other)
182     {
183         return append(other.bytes, 0, other.size);
184     }
185     */

186
187     /**
188      * Releases the internal byte array to the caller, resets the internal
189      * byte array to null and its size to 0.
190      * @return internal byte array.
191      * @stable ICU 2.8
192      */

193     public final byte[] releaseBytes()
194     {
195         byte result[] = bytes;
196         bytes = null;
197         size = 0;
198         return result;
199     }
200     
201     // Boilerplate ----------------------------------------------------
202

203     /**
204      * Returns string value for debugging
205      * @draft ICU 3.2
206      * @provisional This API might change or be removed in a future release.
207      */

208     public String JavaDoc toString() {
209         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
210         for (int i = 0; i < size; ++i) {
211             if (i != 0) result.append(" ");
212             result.append(Utility.hex(bytes[i]&0xFF,2));
213         }
214         return result.toString();
215     }
216
217     /**
218      * Return true if the bytes in each wrapper are equal.
219      * @param other the object to compare to.
220      * @return true if the two objects are equal.
221      * @draft ICU 3.2
222      * @provisional This API might change or be removed in a future release.
223      */

224     public boolean equals(Object JavaDoc other) {
225         if (this == other) return true;
226         if (other == null) return false;
227         try {
228             ByteArrayWrapper that = (ByteArrayWrapper)other;
229             if (size != that.size) return false;
230             for (int i = 0; i < size; ++i) {
231                 if (bytes[i] != that.bytes[i]) return false;
232             }
233             return true;
234         }
235         catch (ClassCastException JavaDoc e) {
236         }
237         return false;
238     }
239
240     /**
241      * Return the hashcode.
242      * @return the hashcode.
243      * @draft ICU 3.2
244      * @provisional This API might change or be removed in a future release.
245      */

246     public int hashCode() {
247         int result = bytes.length;
248         for (int i = 0; i < size; ++i) {
249             result = 37*result + bytes[i];
250         }
251         return result;
252     }
253
254     /**
255      * Compare this object to another ByteArrayWrapper, which must not be null.
256      * @param other the object to compare to.
257      * @return a value <0, 0, or >0 as this compares less than, equal to, or
258      * greater than other.
259      * @throws ClassCastException if the other object is not a ByteArrayWrapper
260      * @draft ICU 3.2
261      * @provisional This API might change or be removed in a future release.
262      */

263     public int compareTo(Object JavaDoc other) {
264         if (this == other) return 0;
265         ByteArrayWrapper that = (ByteArrayWrapper) other;
266         int minSize = size < that.size ? size : that.size;
267         for (int i = 0; i < minSize; ++i) {
268             if (bytes[i] != that.bytes[i]) {
269                 return (bytes[i] & 0xFF) - (that.bytes[i] & 0xFF);
270             }
271         }
272         return size - that.size;
273     }
274     
275     // private methods -----------------------------------------------------
276

277     /**
278      * Copies the contents of src byte array from offset srcoff to the
279      * target of tgt byte array at the offset tgtoff.
280      * @param src source byte array to copy from
281      * @param srcoff start offset of src to copy from
282      * @param tgt target byte array to copy to
283      * @param tgtoff start offset of tgt to copy to
284      * @param length size of contents to copy
285      */

286     private static final void copyBytes(byte[] src, int srcoff, byte[] tgt,
287                                        int tgtoff, int length) {
288         if (length < 64) {
289             for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {
290                 tgt[n] = src[i];
291             }
292         }
293         else {
294             System.arraycopy(src, srcoff, tgt, tgtoff, length);
295         }
296     }
297 }
298
Popular Tags