1 package org.apache.java.lang; 2 3 18 19 27 public class Bytes 28 { 29 private static final char[] hexDigits = 30 { 31 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 32 }; 33 34 41 public static byte[] append(byte[] a, 42 byte[] b) 43 { 44 byte[] z = new byte[a.length + b.length]; 45 System.arraycopy(a, 0, z, 0, a.length); 46 System.arraycopy(b, 0, z, a.length, b.length); 47 return z; 48 } 49 50 58 public static byte[] append(byte[] a, 59 byte[] b, 60 byte[] c) 61 { 62 byte[] z = new byte[a.length + b.length + c.length]; 63 System.arraycopy(a, 0, z, 0, a.length); 64 System.arraycopy(b, 0, z, a.length, b.length); 65 System.arraycopy(c, 0, z, a.length + b.length, c.length); 66 return z; 67 } 68 69 76 public static boolean areEqual(byte[] a, 77 byte[] b) 78 { 79 int aLength = a.length; 80 if (aLength != b.length) return false; 81 82 for (int i = 0; i < aLength; i++) 83 if (a[i] != b[i]) 84 return false; 85 86 return true; 87 } 88 89 97 public static byte[] copy(byte[] b, 98 int pos) 99 { 100 return copy(b, pos, b.length - pos); 101 } 102 103 114 public static byte[] copy(byte[] b, 115 int pos, 116 int length) 117 { 118 byte[] z = new byte[length]; 119 System.arraycopy(b, pos, z, 0, length); 120 return z; 121 } 122 123 129 public static void merge(byte[] src, 130 byte[] dest) 131 { 132 System.arraycopy(src, 0, dest, 0, src.length); 133 } 134 135 143 public static void merge(byte[] src, 144 byte[] dest, 145 int pos) 146 { 147 System.arraycopy(src, 0, dest, pos, src.length); 148 } 149 150 159 public static void merge(byte[] src, 160 byte[] dest, 161 int pos, 162 int length) 163 { 164 System.arraycopy(src, 0, dest, pos, length); 165 } 166 167 177 public static void merge(byte[] src, 178 byte[] dest, 179 int srcpos, 180 int destpos, 181 int length) 182 { 183 System.arraycopy(src, srcpos, dest, destpos, length); 184 } 185 186 192 public static byte[] toBytes(int n) 193 { 194 return toBytes(n, new byte[4]); 195 } 196 197 205 public static byte[] toBytes(int n, 206 byte[] b) 207 { 208 b[3] = (byte) (n); 209 n >>>= 8; 210 b[2] = (byte) (n); 211 n >>>= 8; 212 b[1] = (byte) (n); 213 n >>>= 8; 214 b[0] = (byte) (n); 215 216 return b; 217 } 218 219 225 public static byte[] toBytes(long n) 226 { 227 return toBytes(n, new byte[8]); 228 } 229 230 238 public static byte[] toBytes(long n, 239 byte[] b) 240 { 241 b[7] = (byte) (n); 242 n >>>= 8; 243 b[6] = (byte) (n); 244 n >>>= 8; 245 b[5] = (byte) (n); 246 n >>>= 8; 247 b[4] = (byte) (n); 248 n >>>= 8; 249 b[3] = (byte) (n); 250 n >>>= 8; 251 b[2] = (byte) (n); 252 n >>>= 8; 253 b[1] = (byte) (n); 254 n >>>= 8; 255 b[0] = (byte) (n); 256 257 return b; 258 } 259 260 266 public static int toInt(byte[] b) 267 { 268 return ((((int) b[3]) & 0xFF) + 269 ((((int) b[2]) & 0xFF) << 8) + 270 ((((int) b[1]) & 0xFF) << 16) + 271 ((((int) b[0]) & 0xFF) << 24)); 272 } 273 274 280 public static long toLong(byte[] b) 281 { 282 return ((((long) b[7]) & 0xFF) + 283 ((((long) b[6]) & 0xFF) << 8) + 284 ((((long) b[5]) & 0xFF) << 16) + 285 ((((long) b[4]) & 0xFF) << 24) + 286 ((((long) b[3]) & 0xFF) << 32) + 287 ((((long) b[2]) & 0xFF) << 40) + 288 ((((long) b[1]) & 0xFF) << 48) + 289 ((((long) b[0]) & 0xFF) << 56)); 290 } 291 292 298 public static String toString(byte[] b) 299 { 300 return toString(b, 0, b.length); 301 } 302 303 312 public static String toString(byte[] b, 313 int offset, 314 int length) 315 { 316 char[] buf = new char[length * 2]; 317 318 for (int i = offset, j = 0, k; i < offset + length; i++) 319 { 320 k = b[i]; 321 buf[j++] = hexDigits[(k >>> 4) & 0x0F]; 322 buf[j++] = hexDigits[k & 0x0F]; 323 } 324 325 return new String (buf); 326 } 327 } 328 | Popular Tags |