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