1 16 17 package org.apache.poi.util; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.Arrays ; 22 23 31 32 public class LittleEndian 33 implements LittleEndianConsts { 34 35 40 private LittleEndian() { } 41 42 43 50 51 public static short getShort(final byte[] data, final int offset) { 52 return (short) getNumber(data, offset, SHORT_SIZE); 53 } 54 55 56 63 public static int getUShort(final byte[] data, final int offset) { 64 short num = (short) getNumber(data, offset, SHORT_SIZE); 65 int retNum; 66 if (num < 0) { 67 retNum = ((int) Short.MAX_VALUE + 1) * 2 + (int) num; 68 } else { 69 retNum = (int) num; 70 } 71 return retNum; 72 } 73 74 75 83 public static short[] getSimpleShortArray(final byte[] data, final int offset, final int size) { 84 short[] results = new short[size]; 85 for (int i = 0; i < size; i++) { 86 results[i] = getShort(data, offset + 2 + (i * 2)); 87 } 88 return results; 89 } 90 91 92 100 public static short[] getShortArray(final byte[] data, final int offset) { 101 int size = (int) getNumber(data, offset, SHORT_SIZE); 102 short[] results = getSimpleShortArray(data, offset, size); 103 return results; 104 } 105 106 107 113 114 public static short getShort(final byte[] data) { 115 return getShort(data, 0); 116 } 117 118 119 125 public static int getUShort(final byte[] data) { 126 return getUShort(data, 0); 127 } 128 129 130 137 138 public static int getInt(final byte[] data, final int offset) { 139 return (int) getNumber(data, offset, INT_SIZE); 140 } 141 142 143 149 150 public static int getInt(final byte[] data) { 151 return getInt(data, 0); 152 } 153 154 155 162 public static long getUInt(final byte[] data, final int offset) { 163 int num = (int) getNumber(data, offset, INT_SIZE); 164 long retNum; 165 if (num < 0) { 166 retNum = ((long) Integer.MAX_VALUE + 1) * 2 + (long) num; 167 } else { 168 retNum = (int) num; 169 } 170 return retNum; 171 } 172 173 179 public static long getUInt(final byte[] data) { 180 return getUInt(data,0); 181 } 182 183 190 191 public static long getLong(final byte[] data, final int offset) { 192 return getNumber(data, offset, LONG_SIZE); 193 } 194 195 196 202 203 public static long getLong(final byte[] data) { 204 return getLong(data, 0); 205 } 206 207 208 217 218 public static double getDouble(final byte[] data, final int offset) { 219 return Double.longBitsToDouble(getNumber(data, offset, DOUBLE_SIZE)); 220 } 221 222 223 229 230 public static double getDouble(final byte[] data) { 231 return getDouble(data, 0); 232 } 233 234 235 242 public static void putShort(final byte[] data, final int offset, 243 final short value) { 244 putNumber(data, offset, value, SHORT_SIZE); 245 } 246 247 248 255 public static void putShortArray(final byte[] data, final int offset, final short[] value) { 256 putNumber(data, offset, value.length, SHORT_SIZE); 257 for (int i = 0; i < value.length; i++) { 258 putNumber(data, offset + 2 + (i * 2), value[i], SHORT_SIZE); 259 } 260 } 261 262 271 public static void putUShort(final byte[] data, final int offset, 272 final int value) 273 { 274 putNumber(data, offset, value, SHORT_SIZE); 275 } 276 277 283 284 public static void putShort(final byte[] data, final short value) { 285 putShort(data, 0, value); 286 } 287 288 289 296 297 public static void putInt(final byte[] data, final int offset, 298 final int value) { 299 putNumber(data, offset, value, INT_SIZE); 300 } 301 302 303 309 310 public static void putInt(final byte[] data, final int value) { 311 putInt(data, 0, value); 312 } 313 314 315 322 323 public static void putLong(final byte[] data, final int offset, 324 final long value) { 325 putNumber(data, offset, value, LONG_SIZE); 326 } 327 328 329 335 336 public static void putLong(final byte[] data, final long value) { 337 putLong(data, 0, value); 338 } 339 340 341 348 349 public static void putDouble(final byte[] data, final int offset, 350 final double value) { 351 if (Double.isNaN(value)) 353 putNumber(data, offset, -276939487313920L, DOUBLE_SIZE); 354 else 355 putNumber(data, offset, Double.doubleToLongBits(value), DOUBLE_SIZE); 356 } 357 358 359 365 366 public static void putDouble(final byte[] data, final double value) { 367 putDouble(data, 0, value); 368 } 369 370 371 376 377 public static class BufferUnderrunException 378 extends IOException { 379 380 383 384 BufferUnderrunException() { 385 super("buffer underrun"); 386 } 387 } 388 389 390 400 401 public static short readShort(final InputStream stream) 402 throws IOException , BufferUnderrunException { 403 return getShort(readFromStream(stream, SHORT_SIZE)); 404 } 405 406 407 417 418 public static int readInt(final InputStream stream) 419 throws IOException , BufferUnderrunException { 420 return getInt(readFromStream(stream, INT_SIZE)); 421 } 422 423 424 434 435 public static long readLong(final InputStream stream) 436 throws IOException , BufferUnderrunException { 437 return getLong(readFromStream(stream, LONG_SIZE)); 438 } 439 440 458 459 public static byte[] readFromStream(final InputStream stream, 460 final int size) 461 throws IOException , BufferUnderrunException { 462 byte[] buffer = new byte[size]; 463 464 int count = stream.read(buffer); 465 466 if (count == -1) { 467 468 Arrays.fill(buffer, (byte) 0); 470 } else if (count != size) { 471 throw new BufferUnderrunException(); 472 } 473 return buffer; 474 } 475 476 477 485 private static long getNumber(final byte[] data, final int offset, 486 final int size) { 487 long result = 0; 488 489 for (int j = offset + size - 1; j >= offset; j--) { 490 result <<= 8; 491 result |= 0xff & data[j]; 492 } 493 return result; 494 } 495 496 497 505 private static void putNumber(final byte[] data, final int offset, 506 final long value, final int size) { 507 int limit = size + offset; 508 long v = value; 509 510 for (int j = offset; j < limit; j++) { 511 data[j] = (byte) (v & 0xFF); 512 v >>= 8; 513 } 514 } 515 516 517 524 public static int ubyteToInt(byte b) { 525 return ((b & 0x80) == 0 ? (int) b : (int) (b & (byte) 0x7f) + 0x80); 526 } 527 528 529 536 public static int getUnsignedByte(final byte[] data, final int offset) { 537 return (int) getNumber(data, offset, BYTE_SIZE); 538 } 539 540 541 547 public static int getUnsignedByte(final byte[] data) { 548 return getUnsignedByte(data, 0); 549 } 550 551 552 562 public static byte[] getByteArray(final byte[] data, int offset, int size) { 563 byte[] copy = new byte[size]; 564 System.arraycopy(data, offset, copy, 0, size); 565 566 return copy; 567 } 568 569 570 } 571 | Popular Tags |