private long getLongBigEndian( long p ) { long a = UnsafeUtil.getByte( p ) & 0xFF; long b = UnsafeUtil.getByte( p + 1 ) & 0xFF; long c = UnsafeUtil.getByte( p + 2 ) & 0xFF; long d = UnsafeUtil.getByte( p + 3 ) & 0xFF; long e = UnsafeUtil.getByte( p + 4 ) & 0xFF; long f = UnsafeUtil.getByte( p + 5 ) & 0xFF; long g = UnsafeUtil.getByte( p + 6 ) & 0xFF; long h = UnsafeUtil.getByte( p + 7 ) & 0xFF; return (a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h; }
private byte getByte( long p ) { return UnsafeUtil.getByte( p ); }
/** * Gets a {@code short} at memory address {@code p} by reading byte for byte, instead of the whole value * in one go. This can be useful, even necessary in some scenarios where {@link #allowUnalignedMemoryAccess} * is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been * previously put using {@link #putShortByteWiseLittleEndian(long, short)}. * * @param p address pointer to start reading at. * @return the read value, which was read byte for byte. */ public static short getShortByteWiseLittleEndian( long p ) { short a = (short) (UnsafeUtil.getByte( p ) & 0xFF); short b = (short) (UnsafeUtil.getByte( p + 1 ) & 0xFF); return (short) ((b << 8) | a); }
/** * Gets a {@code int} at memory address {@code p} by reading byte for byte, instead of the whole value * in one go. This can be useful, even necessary in some scenarios where {@link #allowUnalignedMemoryAccess} * is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been * previously put using {@link #putIntByteWiseLittleEndian(long, int)}. * * @param p address pointer to start reading at. * @return the read value, which was read byte for byte. */ public static int getIntByteWiseLittleEndian( long p ) { int a = UnsafeUtil.getByte( p ) & 0xFF; int b = UnsafeUtil.getByte( p + 1 ) & 0xFF; int c = UnsafeUtil.getByte( p + 2 ) & 0xFF; int d = UnsafeUtil.getByte( p + 3 ) & 0xFF; return (d << 24) | (c << 16) | (b << 8) | a; }
/** * Gets a {@code long} at memory address {@code p} by reading byte for byte, instead of the whole value * in one go. This can be useful, even necessary in some scenarios where {@link #allowUnalignedMemoryAccess} * is {@code false} and {@code p} isn't aligned properly. Values read with this method should have been * previously put using {@link #putLongByteWiseLittleEndian(long, long)}. * * @param p address pointer to start reading at. * @return the read value, which was read byte for byte. */ public static long getLongByteWiseLittleEndian( long p ) { long a = UnsafeUtil.getByte( p ) & 0xFF; long b = UnsafeUtil.getByte( p + 1 ) & 0xFF; long c = UnsafeUtil.getByte( p + 2 ) & 0xFF; long d = UnsafeUtil.getByte( p + 3 ) & 0xFF; long e = UnsafeUtil.getByte( p + 4 ) & 0xFF; long f = UnsafeUtil.getByte( p + 5 ) & 0xFF; long g = UnsafeUtil.getByte( p + 6 ) & 0xFF; long h = UnsafeUtil.getByte( p + 7 ) & 0xFF; return (h << 56) | (g << 48) | (f << 40) | (e << 32) | (d << 24) | (c << 16) | (b << 8) | a; }
@Override public void get( long index, byte[] into ) { long address = address( index, 0 ); for ( int i = 0; i < itemSize; i++, address++ ) { into[i] = UnsafeUtil.getByte( address ); } }
@Override public byte getByte( int offset ) { long p = getBoundedPointer( offset, SIZE_OF_BYTE ); return UnsafeUtil.getByte( p ); }
@Override public byte getByte( long index, int offset ) { return UnsafeUtil.getByte( address( index, offset ) ); }
@Override public final byte getByte() { long p = nextBoundedPointer( SIZE_OF_BYTE ); byte b = UnsafeUtil.getByte( p ); offset++; return b; }
@Override public void getBytes( byte[] data, int arrayOffset, int length ) { long p = getBoundedPointer( offset, length ); if ( !outOfBounds ) { for ( int i = 0; i < length; i++ ) { data[arrayOffset + i] = UnsafeUtil.getByte( p + i ); } } offset += length; }
protected byte getByte( long address, int offset ) { return UnsafeUtil.getByte( address + offset ); }
@Override public long get5ByteLong( long index, int offset ) { long address = address( index, offset ); long low4b = getInt( address ) & 0xFFFFFFFFL; long high1b = UnsafeUtil.getByte( address + Integer.BYTES ) & 0xFF; long result = low4b | (high1b << 32); return result == 0xFFFFFFFFFFL ? -1 : result; }
@Override public int get3ByteInt( long index, int offset ) { long address = address( index, offset ); int lowWord = UnsafeUtil.getShort( address ) & 0xFFFF; int highByte = UnsafeUtil.getByte( address + Short.BYTES ) & 0xFF; int result = lowWord | (highByte << Short.SIZE); return result == 0xFFFFFF ? -1 : result; }
private byte[] array( long page ) { int size = sizeOfAsInt( page ); byte[] array = new byte[size]; for ( int i = 0; i < size; i++ ) { array[i] = UnsafeUtil.getByte( page + i ); } return array; }
private void unsafeShiftRight( int fromPos, int toPos, int length, int shift ) { int longSteps = length >> 3; if ( UnsafeUtil.allowUnalignedMemoryAccess && longSteps > 0 ) { for ( int i = 0; i < longSteps; i++ ) { fromPos -= Long.BYTES; long x = UnsafeUtil.getLong( pointer + fromPos ); UnsafeUtil.putLong( pointer + fromPos + shift, x ); } } while ( fromPos > toPos ) { fromPos--; byte b = UnsafeUtil.getByte( pointer + fromPos ); UnsafeUtil.putByte( pointer + fromPos + shift, b ); } }
private void unsafeShiftLeft( int fromPos, int toPos, int length, int shift ) { int longSteps = length >> 3; if ( UnsafeUtil.allowUnalignedMemoryAccess && longSteps > 0 ) { for ( int i = 0; i < longSteps; i++ ) { long x = UnsafeUtil.getLong( pointer + fromPos ); UnsafeUtil.putLong( pointer + fromPos + shift, x ); fromPos += Long.BYTES; } } while ( fromPos < toPos ) { byte b = UnsafeUtil.getByte( pointer + fromPos ); UnsafeUtil.putByte( pointer + fromPos + shift, b ); fromPos++; } }
assertThat( a.position(), is( 0 ) ); assertThat( a.remaining(), is( sizeInBytes ) ); assertThat( getByte( address ), is( (byte) 0 ) ); a.put( (byte) -1 ); assertThat( getByte( address ), is( (byte) -1 ) ); assertThat( a.position(), is( 0 ) ); assertThat( a.remaining(), is( sizeInBytes2 ) ); assertThat( getByte( address2 ), is( (byte) 0 ) ); a.put( (byte) -1 ); assertThat( getByte( address2 ), is( (byte) -1 ) );
for ( int i = 0; i < pageSize; i++ ) byte actualByteContents = UnsafeUtil.getByte( address + i ); if ( actualByteContents != pageByteContents )