]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/util/ByteArrayWrapper.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / util / ByteArrayWrapper.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.util;
9
10 import java.nio.ByteBuffer;
11
12 import com.ibm.icu.impl.Utility;
13
14 /**
15  * <p>
16  * A simple utility class to wrap a byte array.
17  * </p>
18  * <p>
19  * Generally passed as an argument object into a method. The method takes
20  * responsibility of writing into the internal byte array and increasing its
21  * size when necessary.
22  * </p> 
23  * @author syn wee
24  * @stable ICU 2.8
25  */
26 public class ByteArrayWrapper implements Comparable<ByteArrayWrapper>
27 {
28     // public data member ------------------------------------------------
29     
30     /**
31      * Internal byte array.
32      * @stable ICU 2.8
33      */
34     public byte[] bytes;
35
36     /**
37      * Size of the internal byte array used. 
38      * Different from bytes.length, size will be &lt;= bytes.length. 
39      * Semantics of size is similar to java.util.Vector.size().
40      * @stable ICU 2.8
41      */
42     public int size;
43     
44     // public constructor ------------------------------------------------
45
46     /** 
47      * Construct a new ByteArrayWrapper with no data.
48      * @stable ICU 2.8
49      */
50     public ByteArrayWrapper() {
51         // leave bytes null, don't allocate twice
52     }
53
54     /**
55      * Construct a new ByteArrayWrapper from a byte array and size
56      * @param bytesToAdopt the byte array to adopt
57      * @param size the length of valid data in the byte array
58      * @throws IndexOutOfBoundsException if bytesToAdopt == null and size != 0, or
59      * size < 0, or size > bytesToAdopt.length.
60      * @stable ICU 3.2
61      */
62     public ByteArrayWrapper(byte[] bytesToAdopt, int size) {
63         if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) {
64             throw new IndexOutOfBoundsException("illegal size: " + size);
65         }
66         this.bytes = bytesToAdopt;
67         this.size = size;
68     }
69
70     /**
71      * Construct a new ByteArrayWrapper from the contents of a ByteBuffer.
72      * @param source the ByteBuffer from which to get the data.
73      * @stable ICU 3.2
74      */
75     public ByteArrayWrapper(ByteBuffer source) {
76         size = source.limit();
77         bytes = new byte[size];
78         source.get(bytes,0,size);
79     }
80
81     /**
82      * Create from ByteBuffer
83      * @param byteBuffer
84     public ByteArrayWrapper(ByteArrayWrapper source) {
85         size = source.size;
86         bytes = new byte[size];
87         copyBytes(source.bytes, 0, bytes, 0, size);
88     }
89      */
90
91     /**
92      * create from byte buffer
93      * @param src
94      * @param start
95      * @param limit
96     public ByteArrayWrapper(byte[] src, int start, int limit) {
97         size = limit - start;
98         bytes = new byte[size];
99         copyBytes(src, start, bytes, 0, size);
100     }
101      */
102
103     // public methods ----------------------------------------------------
104
105     /**
106      * Ensure that the internal byte array is at least of length capacity.     
107      * If the byte array is null or its length is less than capacity, a new 
108      * byte array of length capacity will be allocated.  
109      * The contents of the array (between 0 and size) remain unchanged. 
110      * @param capacity minimum length of internal byte array.
111      * @return this ByteArrayWrapper
112      * @stable ICU 3.2
113      */
114     public ByteArrayWrapper ensureCapacity(int capacity) 
115     {
116         if (bytes == null || bytes.length < capacity) {
117             byte[] newbytes = new byte[capacity];
118             copyBytes(bytes, 0, newbytes, 0, size);
119             bytes = newbytes;
120         }
121         return this;
122     }
123     
124     /**
125      * Set the internal byte array from offset 0 to (limit - start) with the 
126      * contents of src from offset start to limit. If the byte array is null or its length is less than capacity, a new 
127      * byte array of length (limit - start) will be allocated.  
128      * This resets the size of the internal byte array to (limit - start).
129      * @param src source byte array to copy from
130      * @param start start offset of src to copy from
131      * @param limit end + 1 offset of src to copy from
132      * @return this ByteArrayWrapper
133      * @stable ICU 3.2
134      */
135     public final ByteArrayWrapper set(byte[] src, int start, int limit) 
136     {
137         size = 0;
138         append(src, start, limit);
139         return this;
140     }
141     
142     /*
143     public final ByteArrayWrapper get(byte[] target, int start, int limit) 
144     {
145         int len = limit - start;
146         if (len > size) throw new IllegalArgumentException("limit too long");
147         copyBytes(bytes, 0, target, start, len);
148         return this;
149     }
150     */
151
152     /**
153      * Appends the internal byte array from offset size with the 
154      * contents of src from offset start to limit. This increases the size of
155      * the internal byte array to (size + limit - start).
156      * @param src source byte array to copy from
157      * @param start start offset of src to copy from
158      * @param limit end + 1 offset of src to copy from
159      * @return this ByteArrayWrapper
160      * @stable ICU 3.2
161      */
162     public final ByteArrayWrapper append(byte[] src, int start, int limit) 
163     {
164         int len = limit - start;
165         ensureCapacity(size + len);
166         copyBytes(src, start, bytes, size, len);
167         size += len;
168         return this;
169     }
170
171     /*
172     public final ByteArrayWrapper append(ByteArrayWrapper other) 
173     {
174         return append(other.bytes, 0, other.size);
175     }
176     */
177
178     /**
179      * Releases the internal byte array to the caller, resets the internal
180      * byte array to null and its size to 0.
181      * @return internal byte array.
182      * @stable ICU 2.8
183      */
184     public final byte[] releaseBytes()
185     {
186         byte result[] = bytes;
187         bytes = null;
188         size = 0;
189         return result;
190     }
191     
192     // Boilerplate ----------------------------------------------------
193     
194     /**
195      * Returns string value for debugging
196      * @stable ICU 3.2
197      */
198     public String toString() {
199         StringBuilder result = new StringBuilder();
200         for (int i = 0; i < size; ++i) {
201             if (i != 0) result.append(" ");
202             result.append(Utility.hex(bytes[i]&0xFF,2));
203         }
204         return result.toString();
205     }
206
207     /**
208      * Return true if the bytes in each wrapper are equal.
209      * @param other the object to compare to.
210      * @return true if the two objects are equal.
211      * @stable ICU 3.2
212      */
213     public boolean equals(Object other) {
214         if (this == other) return true;
215         if (other == null) return false;
216         try {
217             ByteArrayWrapper that = (ByteArrayWrapper)other;
218             if (size != that.size) return false;
219             for (int i = 0; i < size; ++i) {
220                 if (bytes[i] != that.bytes[i]) return false;
221             }
222             return true;
223         }
224         catch (ClassCastException e) {
225         }
226         return false;
227     }
228
229     /**
230      * Return the hashcode.
231      * @return the hashcode.
232      * @stable ICU 3.2
233      */
234     public int hashCode() {
235         int result = bytes.length;
236         for (int i = 0; i < size; ++i) {
237             result = 37*result + bytes[i];
238         }
239         return result;
240     }
241
242     /**
243      * Compare this object to another ByteArrayWrapper, which must not be null.
244      * @param other the object to compare to.
245      * @return a value <0, 0, or >0 as this compares less than, equal to, or
246      * greater than other.
247      * @throws ClassCastException if the other object is not a ByteArrayWrapper
248      * @stable ICU 4.4
249      */
250     public int compareTo(ByteArrayWrapper other) {
251         if (this == other) return 0;
252         int minSize = size < other.size ? size : other.size;
253         for (int i = 0; i < minSize; ++i) {
254             if (bytes[i] != other.bytes[i]) {
255                 return (bytes[i] & 0xFF) - (other.bytes[i] & 0xFF);
256             }
257         }
258         return size - other.size;
259     }
260     
261     // private methods -----------------------------------------------------
262     
263     /**
264      * Copies the contents of src byte array from offset srcoff to the 
265      * target of tgt byte array at the offset tgtoff.
266      * @param src source byte array to copy from
267      * @param srcoff start offset of src to copy from
268      * @param tgt target byte array to copy to
269      * @param tgtoff start offset of tgt to copy to
270      * @param length size of contents to copy
271      */
272     private static final void copyBytes(byte[] src, int srcoff, byte[] tgt, 
273                                        int tgtoff, int length) {
274         if (length < 64) {
275             for (int i = srcoff, n = tgtoff; -- length >= 0; ++ i, ++ n) {
276                 tgt[n] = src[i];
277             }
278         } 
279         else {
280             System.arraycopy(src, srcoff, tgt, tgtoff, length);
281         }
282     }      
283 }