public byte[] asBytes() { return asBytes(0); }
private static byte[] createBitCompactedArray( ShortArray type, Object array, int offsetBytes ) { Class<?> componentType = array.getClass().getComponentType(); boolean isPrimitiveByteArray = componentType.equals( Byte.TYPE ); boolean isByteArray = componentType.equals( Byte.class ) || isPrimitiveByteArray; int arrayLength = Array.getLength( array ); int requiredBits = isByteArray ? Byte.SIZE : type.calculateRequiredBitsForArray( array, arrayLength ); int totalBits = requiredBits * arrayLength; int bitsUsedInLastByte = totalBits % 8; bitsUsedInLastByte = bitsUsedInLastByte == 0 ? 8 : bitsUsedInLastByte; if ( isByteArray ) { return createBitCompactedByteArray( type, isPrimitiveByteArray, array, bitsUsedInLastByte, requiredBits, offsetBytes ); } else { int numberOfBytes = (totalBits - 1) / 8 + 1; numberOfBytes += NUMBER_HEADER_SIZE; // type + rest + requiredBits header. TODO no need to use full bytes Bits bits = Bits.bits( numberOfBytes ); bits.put( (byte) type.intValue() ); bits.put( (byte) bitsUsedInLastByte ); bits.put( (byte) requiredBits ); type.writeAll( array, arrayLength, requiredBits, bits ); return bits.asBytes( offsetBytes ); } }
@Test public void doubleAsBytesWithOffset() { double[] array1 = new double[]{1.0, 2.0, 3.0, 4.0, 5.0}; Bits bits = Bits.bits( array1.length * 8 ); for ( double value : array1 ) { bits.put( Double.doubleToRawLongBits( value ) ); } int offset = 6; byte[] asBytesOffset = bits.asBytes( offset ); byte[] asBytes = bits.asBytes(); assertEquals( asBytes.length, array1.length * 8 ); assertEquals( asBytesOffset.length, array1.length * 8 + offset ); for ( int i = 0; i < asBytes.length; i++ ) { assertEquals( asBytesOffset[i + offset], asBytes[i] ); } }
@Test public void asBytes() { int numberOfBytes = 14; Bits bits = bits( numberOfBytes ); for ( byte i = 0; i < numberOfBytes; i++ ) { bits.put( i ); } byte[] bytes = bits.asBytes(); for ( byte i = 0; i < numberOfBytes; i++ ) { assertEquals( i, bytes[i] ); } }
@Test public void doubleAsBytes() { double[] array1 = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; Bits bits = Bits.bits( array1.length * 8 ); for ( double value : array1 ) { bits.put( Double.doubleToRawLongBits( value ) ); } String first = bits.toString(); byte[] asBytes = bits.asBytes(); String other = Bits.bitsFromBytes( asBytes ).toString(); assertEquals( first, other ); }
public byte[] asBytes() { return asBytes(0); }
private static byte[] createBitCompactedArray( ShortArray type, Object array, int offsetBytes ) { Class<?> componentType = array.getClass().getComponentType(); boolean isPrimitiveByteArray = componentType.equals( Byte.TYPE ); boolean isByteArray = componentType.equals( Byte.class ) || isPrimitiveByteArray; int arrayLength = Array.getLength( array ); int requiredBits = isByteArray ? Byte.SIZE : type.calculateRequiredBitsForArray( array, arrayLength ); int totalBits = requiredBits * arrayLength; int bitsUsedInLastByte = totalBits % 8; bitsUsedInLastByte = bitsUsedInLastByte == 0 ? 8 : bitsUsedInLastByte; if ( isByteArray ) { return createBitCompactedByteArray( type, isPrimitiveByteArray, array, bitsUsedInLastByte, requiredBits, offsetBytes ); } else { int numberOfBytes = (totalBits - 1) / 8 + 1; numberOfBytes += NUMBER_HEADER_SIZE; // type + rest + requiredBits header. TODO no need to use full bytes Bits bits = Bits.bits( numberOfBytes ); bits.put( (byte) type.intValue() ); bits.put( (byte) bitsUsedInLastByte ); bits.put( (byte) requiredBits ); type.writeAll( array, arrayLength, requiredBits, bits ); return bits.asBytes( offsetBytes ); } }