Top Description Inners Fields Constructors Methods
com.google.common.hash

pack-priv final Class LittleEndianByteArray

extends Object
Class Inheritance
Annotations
@ElementTypesAreNonnullByDefault
Imports
com.google.common.primitives.Longs, java.lang.reflect.Field, java.nio.ByteOrder, java.security.AccessController, .PrivilegedActionException, .PrivilegedExceptionAction, sun.misc.Unsafe

Utility functions for loading and storing values from a byte array.
Authors
Kevin Damm, Kyle Maddison

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static enum
LittleEndianByteArray.JavaLittleEndianBytes

Fallback implementation for when Unsafe is not available in our current environment.

private static interface
LittleEndianByteArray.LittleEndianBytes

Common interface for retrieving a 64-bit long from a little-endian byte array.

private static enum
LittleEndianByteArray.UnsafeByteArray

The only reference to Unsafe is in this nested class.

Field Summary

Modifier and TypeField and Description
private static final LittleEndianByteArray.LittleEndianBytes
byteArray

The instance that actually does the work; delegates to Unsafe or a pure-Java fallback.

Constructor Summary

AccessConstructor and Description
private
LittleEndianByteArray()

Deter instantiation of this class.

Method Summary

Modifier and TypeMethod and Description
pack-priv static int

Returns:

the value found in the array in the form of a long
load32
(byte[]
the input bytes
source
,
int
the offset into the array at which to start
offset
)

Load 4 bytes from the provided array at the indicated offset.

pack-priv static long

Returns:

a long of a concatenated 8 bytes
load64
(byte[]
the input bytes
input
,
int
the offset into the array at which to start
offset
)

Load 8 bytes into long in a little endian manner, from the substring between position and position + 8.

pack-priv static long

Returns:

a long of a concatenated 8 bytes
load64Safely
(byte[]
the input bytes
input
,
int
the offset into the array at which to start reading
offset
,
int
the number of bytes from the input to read
length
)

Similar to load64, but allows offset + 8 > input.length, padding the result with zeroes.

pack-priv static void
store64(byte[]
the output byte array
sink
,
int
the offset into the array at which to start writing
offset
,
long
the value to write
value
)

Store 8 bytes into the provided array at the indicated offset, using the value provided.

pack-priv static boolean
usingUnsafe()

Indicates that the loading of Unsafe was successful and the load and store operations will be very efficient.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

byteArrayback to summary
private static final LittleEndianByteArray.LittleEndianBytes byteArray

The instance that actually does the work; delegates to Unsafe or a pure-Java fallback.

Constructor Detail

LittleEndianByteArrayback to summary
private LittleEndianByteArray()

Deter instantiation of this class.

Method Detail

load32back to summary
pack-priv static int load32(byte[] source, int offset)

Load 4 bytes from the provided array at the indicated offset.

Parameters
source:byte[]

the input bytes

offset:int

the offset into the array at which to start

Returns:int

the value found in the array in the form of a long

load64back to summary
pack-priv static long load64(byte[] input, int offset)

Load 8 bytes into long in a little endian manner, from the substring between position and position + 8. The array must have at least 8 bytes from offset (inclusive).

Parameters
input:byte[]

the input bytes

offset:int

the offset into the array at which to start

Returns:long

a long of a concatenated 8 bytes

load64Safelyback to summary
pack-priv static long load64Safely(byte[] input, int offset, int length)

Similar to load64, but allows offset + 8 > input.length, padding the result with zeroes. This has to explicitly reverse the order of the bytes as it packs them into the result which makes it slower than the native version.

Parameters
input:byte[]

the input bytes

offset:int

the offset into the array at which to start reading

length:int

the number of bytes from the input to read

Returns:long

a long of a concatenated 8 bytes

store64back to summary
pack-priv static void store64(byte[] sink, int offset, long value)

Store 8 bytes into the provided array at the indicated offset, using the value provided.

Parameters
sink:byte[]

the output byte array

offset:int

the offset into the array at which to start writing

value:long

the value to write

usingUnsafeback to summary
pack-priv static boolean usingUnsafe()

Indicates that the loading of Unsafe was successful and the load and store operations will be very efficient. May be useful for calling code to fall back on an alternative implementation that is slower than Unsafe.get/store but faster than the pure-Java mask-and-shift.

com.google.common.hash back to summary

private sealed Enum LittleEndianByteArray.JavaLittleEndianBytes

extends Enum<LittleEndianByteArray.JavaLittleEndianBytes>
implements LittleEndianBytes
Class Inheritance
All Implemented Interfaces
com.google.common.hash.LittleEndianByteArray.LittleEndianBytes

Fallback implementation for when Unsafe is not available in our current environment.

Field Summary

Modifier and TypeField and Description
public static final LittleEndianByteArray.JavaLittleEndianBytes

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static LittleEndianByteArray.JavaLittleEndianBytes
public static LittleEndianByteArray.JavaLittleEndianBytes[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

INSTANCEback to summary
public static final LittleEndianByteArray.JavaLittleEndianBytes INSTANCE

Constructor Detail

JavaLittleEndianBytesback to summary
private JavaLittleEndianBytes()

Method Detail

valueOfback to summary
public static LittleEndianByteArray.JavaLittleEndianBytes valueOf(String name)
valuesback to summary
public static LittleEndianByteArray.JavaLittleEndianBytes[] values()
com.google.common.hash back to summary

private Interface LittleEndianByteArray.LittleEndianBytes

Known Direct Implementers
com.google.common.hash.LittleEndianByteArray.UnsafeByteArray, com.google.common.hash.LittleEndianByteArray.JavaLittleEndianBytes

Common interface for retrieving a 64-bit long from a little-endian byte array.

This abstraction allows us to use single-instruction load and put when available, or fall back on the slower approach of using Longs.fromBytes(byte...).

Method Summary

Modifier and TypeMethod and Description
public long
getLongLittleEndian(byte[] array, int offset)

public void
putLongLittleEndian(byte[] array, int offset, long value)

Method Detail

getLongLittleEndianback to summary
public long getLongLittleEndian(byte[] array, int offset)
putLongLittleEndianback to summary
public void putLongLittleEndian(byte[] array, int offset, long value)
com.google.common.hash back to summary

private sealed Enum LittleEndianByteArray.UnsafeByteArray

extends Enum<LittleEndianByteArray.UnsafeByteArray>
implements LittleEndianBytes
Class Inheritance
All Implemented Interfaces
com.google.common.hash.LittleEndianByteArray.LittleEndianBytes

The only reference to Unsafe is in this nested class. We set things up so that if Unsafe.theUnsafe is inaccessible, the attempt to load the nested class fails, and the outer class's static initializer can fall back on a non-Unsafe version.

Field Summary

Modifier and TypeField and Description
private static final int
private static final Unsafe
public static final LittleEndianByteArray.UnsafeByteArray
public static final LittleEndianByteArray.UnsafeByteArray

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Unsafe

Returns:

an Unsafe instance if successful
getUnsafe
()

Returns an Unsafe.

public static LittleEndianByteArray.UnsafeByteArray
public static LittleEndianByteArray.UnsafeByteArray[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BYTE_ARRAY_BASE_OFFSETback to summary
private static final int BYTE_ARRAY_BASE_OFFSET
theUnsafeback to summary
private static final Unsafe theUnsafe
UNSAFE_BIG_ENDIANback to summary
public static final LittleEndianByteArray.UnsafeByteArray UNSAFE_BIG_ENDIAN
UNSAFE_LITTLE_ENDIANback to summary
public static final LittleEndianByteArray.UnsafeByteArray UNSAFE_LITTLE_ENDIAN

Constructor Detail

UnsafeByteArrayback to summary
private UnsafeByteArray()

Method Detail

getUnsafeback to summary
private static Unsafe getUnsafe()

Returns an Unsafe. Suitable for use in a 3rd party package. Replace with a simple call to Unsafe.getUnsafe when integrating into a JDK.

Returns:Unsafe

an Unsafe instance if successful

valueOfback to summary
public static LittleEndianByteArray.UnsafeByteArray valueOf(String name)
valuesback to summary
public static LittleEndianByteArray.UnsafeByteArray[] values()