1 8 9 package com.ibm.icu.util; 10 11 import com.ibm.icu.impl.ByteBuffer; 15 import com.ibm.icu.impl.Utility; 17 18 30 public class ByteArrayWrapper implements Comparable 31 { 32 34 38 public byte[] bytes; 39 40 46 public int size; 47 48 50 54 public ByteArrayWrapper() { 55 } 57 58 67 public ByteArrayWrapper(byte[] bytesToAdopt, int size) { 68 if ((bytesToAdopt == null && size != 0) || size < 0 || size > bytesToAdopt.length) { 69 throw new IndexOutOfBoundsException ("illegal size: " + size); 70 } 71 this.bytes = bytesToAdopt; 72 this.size = size; 73 } 74 75 81 public ByteArrayWrapper(ByteBuffer source) { 82 size = source.limit(); 83 bytes = new byte[size]; 84 source.get(bytes,0,size); 85 } 86 87 96 97 108 109 111 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 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 159 160 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 186 187 193 public final byte[] releaseBytes() 194 { 195 byte result[] = bytes; 196 bytes = null; 197 size = 0; 198 return result; 199 } 200 201 203 208 public String toString() { 209 StringBuffer result = new StringBuffer (); 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 224 public boolean equals(Object 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 e) { 236 } 237 return false; 238 } 239 240 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 263 public int compareTo(Object 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 277 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 |