Package com.google.common.hash
Class LittleEndianByteArray
java.lang.Object
com.google.common.hash.LittleEndianByteArray
Utility functions for loading and storing values from a byte array.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enum
Fallback implementation for when Unsafe is not available in our current environment.private static interface
Common interface for retrieving a 64-bit long from a little-endian byte array.private static enum
The only reference to Unsafe is in this nested class. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final LittleEndianByteArray.LittleEndianBytes
The instance that actually does the work; delegates to Unsafe or a pure-Java fallback. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static int
load32
(byte[] source, int offset) Load 4 bytes from the provided array at the indicated offset.(package private) 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.(package private) static long
load64Safely
(byte[] input, int offset, int length) Similar to load64, but allows offset + 8 > input.length, padding the result with zeroes.(package private) static void
store64
(byte[] sink, int offset, long value) Store 8 bytes into the provided array at the indicated offset, using the value provided.(package private) static boolean
Indicates that the loading of Unsafe was successful and the load and store operations will be very efficient.
-
Field Details
-
byteArray
The instance that actually does the work; delegates to Unsafe or a pure-Java fallback.
-
-
Constructor Details
-
LittleEndianByteArray
private LittleEndianByteArray()Deter instantiation of this class.
-
-
Method Details
-
load64
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
- the input bytesoffset
- the offset into the array at which to start- Returns:
- a long of a concatenated 8 bytes
-
load64Safely
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
- the input bytesoffset
- the offset into the array at which to start readinglength
- the number of bytes from the input to read- Returns:
- a long of a concatenated 8 bytes
-
store64
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
- the output byte arrayoffset
- the offset into the array at which to start writingvalue
- the value to write
-
load32
static int load32(byte[] source, int offset) Load 4 bytes from the provided array at the indicated offset.- Parameters:
source
- the input bytesoffset
- the offset into the array at which to start- Returns:
- the value found in the array in the form of a long
-
usingUnsafe
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.
-