/** * Reads the given byte array as a little endian long. * @param bytes the byte array to convert * @param off the offset into the array that starts the value * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight */ public static long fromLittleEndian(byte[] bytes, final int off, final int length) { checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { l |= (bytes[off + i] & 0xffL) << (8 * i); } return l; }
/** * Reads the given byte array as a little endian long. * @param bytes the byte array to convert * @return the number read */ public static long fromLittleEndian(byte[] bytes) { return fromLittleEndian(bytes, 0, bytes.length); }
/** * Get value as two bytes in big endian byte order. * @return the value as a a two byte array in big endian byte order */ public byte[] getBytes() { final byte[] result = new byte[2]; ByteUtils.toLittleEndian(result, value, 0, 2); return result; }
/** * Read 8-byte integer from buffer. * * @param buffer * @param offset * @return the 8-byte entry as a long */ public static final long convert64(final byte[] buffer, final int offset) { return ByteUtils.fromLittleEndian(buffer, offset, 8); }
/** * put the value as two bytes in big endian byte order. * @param value the Java int to convert to bytes * @param buf the output buffer * @param offset * The offset within the output buffer of the first byte to be written. * must be non-negative and no larger than <tt>buf.length-2</tt> */ public static void putShort(final int value, final byte[] buf, final int offset) { ByteUtils.toLittleEndian(buf, value, offset, 2); }
/** * Reads the given number of bytes from the given input as little endian long. * @param in the input to read from * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight * @throws IOException if reading fails or the stream doesn't * contain the given number of bytes anymore */ public static long fromLittleEndian(DataInput in, int length) throws IOException { // somewhat duplicates the ByteSupplier version in order to save the creation of a wrapper object checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { long b = in.readUnsignedByte(); l |= (b << (i * 8)); } return l; }
/** * Helper method to get the value as a Java long from four bytes starting at given array offset * @param bytes the array of bytes * @param offset the offset to start * @return the corresponding Java long value */ public static long getValue(final byte[] bytes, final int offset) { return ByteUtils.fromLittleEndian(bytes, offset, 4); }
/** * put the value as four bytes in big endian byte order. * @param value the Java long to convert to bytes * @param buf the output buffer * @param offset * The offset within the output buffer of the first byte to be written. * must be non-negative and no larger than <tt>buf.length-4</tt> */ public static void putLong(final long value, final byte[] buf, int offset) { ByteUtils.toLittleEndian(buf, value, offset, 4); }
/** * Reads the given number of bytes from the given stream as a little endian long. * @param in the stream to read from * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight * @throws IOException if reading fails or the stream doesn't * contain the given number of bytes anymore */ public static long fromLittleEndian(InputStream in, int length) throws IOException { // somewhat duplicates the ByteSupplier version in order to save the creation of a wrapper object checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { long b = in.read(); if (b == -1) { throw new IOException("premature end of data"); } l |= (b << (i * 8)); } return l; }
/** * Read 4-byte integer from buffer. * * @param buffer * @param offset * @return the 4-byte entry as an int */ public static final int convert32(final byte[] buffer, final int offset) { return (int) ByteUtils.fromLittleEndian(buffer, offset, 4); }
private void writeLittleEndian(final int numBytes, int num) throws IOException { ByteUtils.toLittleEndian(consumer, num, numBytes); }
/** * Reads the given number of bytes from the given supplier as a little endian long. * * <p>Typically used by our InputStreams that need to count the * bytes read as well.</p> * * @param supplier the supplier for bytes * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight * @throws IOException if the supplier fails or doesn't supply the * given number of bytes anymore */ public static long fromLittleEndian(ByteSupplier supplier, final int length) throws IOException { checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { long b = supplier.getAsByte(); if (b == -1) { throw new IOException("premature end of data"); } l |= (b << (i * 8)); } return l; }
/** * Read 2-byte integer from buffer. * * @param buffer * @param offset * @return the 2-byte entry as an int */ public static final int convert16(final byte[] buffer, final int offset) { return (int) ByteUtils.fromLittleEndian(buffer, offset, 2); }
private void writeLittleEndian(final int numBytes, long num) throws IOException { ByteUtils.toLittleEndian(consumer, num, numBytes); }
/** * Reads the given byte array as a little endian long. * @param bytes the byte array to convert * @param off the offset into the array that starts the value * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight */ public static long fromLittleEndian(byte[] bytes, final int off, final int length) { checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { l |= (bytes[off + i] & 0xffL) << (8 * i); } return l; }
/** * Helper method to get the value as a java int from two bytes starting at given array offset * @param bytes the array of bytes * @param offset the offset to start * @return the corresponding java int value */ public static int getValue(final byte[] bytes, final int offset) { return (int) ByteUtils.fromLittleEndian(bytes, offset, 2); }
private void writeTrailer() throws IOException { out.write(END_MARK); if (params.withContentChecksum) { ByteUtils.toLittleEndian(out, contentHash.getValue(), 4); } }
/** * Reads the given number of bytes from the given input as little endian long. * @param in the input to read from * @param length the number of bytes representing the value * @return the number read * @throws IllegalArgumentException if len is bigger than eight * @throws IOException if reading fails or the stream doesn't * contain the given number of bytes anymore */ public static long fromLittleEndian(DataInput in, int length) throws IOException { // somewhat duplicates the ByteSupplier version in order to save the creation of a wrapper object checkReadLength(length); long l = 0; for (int i = 0; i < length; i++) { long b = in.readUnsignedByte(); l |= (b << (i * 8)); } return l; }
private int readSize() throws IOException { return (int) ByteUtils.fromLittleEndian(supplier, 3); }
ByteUtils.toLittleEndian(out, currentIndex | FramedLZ4CompressorInputStream.UNCOMPRESSED_FLAG_MASK, 4); out.write(blockData, 0, currentIndex); ByteUtils.toLittleEndian(out, b.length, 4); out.write(b); if (params.withBlockChecksum) { ByteUtils.toLittleEndian(out, blockHash.getValue(), 4); blockHash.reset();