public static Bits bitsFromBytes( byte[] bytes, int startIndex ) { final int count = bytes.length; Bits bits = bits( count - startIndex ); for ( int i = startIndex; i < count; i++ ) { bits.put( bytes[i] ); } return bits; }
@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 ); }
private static Bits newBitsForStep8( int length ) { return Bits.bits(calculateNumberOfBlocksUsedForStep8(length) << 3 ); //*8 }
public static Bits bitsFromBytes( byte[] bytes, int offset, int length ) { Bits bits = bits( length - offset ); for ( int i = offset; i < (offset + length); i++ ) { bits.put( bytes[i] ); } return bits; }
private static Bits newBits( LongerShortString encoding, int length ) { return Bits.bits(calculateNumberOfBlocksUsed( encoding, length ) << 3 ); //*8 }
public static long versionStringToLong( String storeVersion ) { if ( CommonAbstractStore.UNKNOWN_VERSION.equals( storeVersion ) ) { return -1; } Bits bits = Bits.bits( 8 ); int length = storeVersion.length(); if ( length == 0 || length > 7 ) { throw new IllegalArgumentException( format( "The given string %s is not of proper size for a store version string", storeVersion ) ); } bits.put( length, 8 ); for ( int i = 0; i < length; i++ ) { char c = storeVersion.charAt( i ); if ( c >= 256 ) { throw new IllegalArgumentException( format( "Store version strings should be encode-able as Latin1 - %s is not", storeVersion ) ); } bits.put( c, 8 ); // Just the lower byte } return bits.getLong(); }
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 ); } }
private long inlinedLabelsLongRepresentation( long... labelIds ) { long header = (long) labelIds.length << 36; byte bitsPerLabel = (byte) (36 / labelIds.length); Bits bits = bits( 5 ); for ( long labelId : labelIds ) { bits.put( labelId, bitsPerLabel ); } return header | bits.getLongs()[0]; }
static boolean tryInlineInNodeRecord( NodeRecord node, long[] ids, Collection<DynamicRecord> changedDynamicRecords ) { // We reserve the high header bit for future extensions of the format of the in-lined label bits // i.e. the 0-valued high header bit can allow for 0-7 in-lined labels in the bit-packed format. if ( ids.length > 7 ) { return false; } byte bitsPerLabel = (byte) (ids.length > 0 ? (LABEL_BITS / ids.length) : LABEL_BITS); Bits bits = bits( 5 ); if ( !inlineValues( ids, bitsPerLabel, bits ) ) { return false; } node.setLabelField( combineLabelCountAndLabelStorage( (byte) ids.length, bits.getLongs()[0] ), changedDynamicRecords ); return true; }
private long inlinedLabelsLongRepresentation( long... labelIds ) { long header = (long) labelIds.length << 36; byte bitsPerLabel = (byte) (36 / labelIds.length); Bits bits = bits( 5 ); for ( long labelId : labelIds ) { bits.put( labelId, bitsPerLabel ); } return header | bits.getLongs()[0]; }
private Value readShortArray() { Bits bits = Bits.bits( MAX_BYTES_IN_SHORT_STRING_OR_SHORT_ARRAY ); int blocksUsed = ShortArray.calculateNumberOfBlocksUsed( currentBlock() ); for ( int i = 0; i < blocksUsed; i++ ) { bits.put( getBlocks()[block + i] ); } return ShortArray.decode( bits ); }
@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] ); } }
Bits result = Bits.bits( numberOfBytes );
@Test public void writeAndRead() { for ( int b = 5; b <= 8; b++ ) { Bits bits = Bits.bits( 16 ); for ( byte value = 0; value < 16; value++ ) { bits.put( value, b ); } for ( byte expected = 0; bits.available(); expected++ ) { assertEquals( expected, bits.getByte( b ) ); } } for ( byte value = Byte.MIN_VALUE; value < Byte.MAX_VALUE; value++ ) { Bits bits = Bits.bits( 8 ); bits.put( value ); assertEquals( value, bits.getByte() ); } }
@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] ); } }
public static Bits bitsFromBytes( byte[] bytes, int startIndex ) { final int count = bytes.length; Bits bits = bits( count - startIndex ); for ( int i = startIndex; i < count; i++ ) { bits.put( bytes[i] ); } return bits; }
private static Bits newBitsForStep8( int length ) { return Bits.bits(calculateNumberOfBlocksUsedForStep8(length) << 3 ); //*8 }
public static Bits bitsFromBytes( byte[] bytes, int offset, int length ) { Bits bits = bits( length - offset ); for ( int i = offset; i < (offset + length); i++ ) { bits.put( bytes[i] ); } return bits; }
private Value readShortArray() { Bits bits = Bits.bits( MAX_BYTES_IN_SHORT_STRING_OR_SHORT_ARRAY ); int blocksUsed = ShortArray.calculateNumberOfBlocksUsed( currentBlock() ); for ( int i = 0; i < blocksUsed; i++ ) { bits.put( getBlocks()[block + i] ); } return ShortArray.decode( bits ); }
private long inlinedLabelsLongRepresentation( long... labelIds ) { long header = (long) labelIds.length << 36; byte bitsPerLabel = (byte) (36 / labelIds.length); Bits bits = bits( 5 ); for ( long labelId : labelIds ) { bits.put( labelId, bitsPerLabel ); } return header | bits.getLongs()[0]; }