Package com.google.protobuf
Class Utf8.UnsafeProcessor
java.lang.Object
com.google.protobuf.Utf8.Processor
com.google.protobuf.Utf8.UnsafeProcessor
- Enclosing class:
Utf8
Utf8.Processor
that uses sun.misc.Unsafe
where possible to improve performance.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) String
decodeUtf8
(byte[] bytes, int index, int size) Decodes the given byte array slice into aString
.(package private) String
decodeUtf8Direct
(ByteBuffer buffer, int index, int size) Decodes directByteBuffer
instances intoString
.(package private) int
encodeUtf8
(String in, byte[] out, int offset, int length) Encodes an input character sequence (in
) to UTF-8 in the target array (out
).(package private) void
encodeUtf8Direct
(String in, ByteBuffer out) Encodes the input character sequence to a directByteBuffer
instance.(package private) static boolean
Indicates whether or not all required unsafe operations are supported on this platform.private static int
partialIsValidUtf8
(byte[] bytes, long offset, int remaining) (package private) int
partialIsValidUtf8
(int state, byte[] bytes, int index, int limit) Tells whether the given byte array slice is a well-formed, malformed, or incomplete UTF-8 byte sequence.private static int
partialIsValidUtf8
(long address, int remaining) (package private) int
partialIsValidUtf8Direct
(int state, ByteBuffer buffer, int index, int limit) Performs validation for directByteBuffer
instances.private static int
unsafeEstimateConsecutiveAscii
(byte[] bytes, long offset, int maxChars) Counts (approximately) the number of consecutive ASCII characters starting from the given position, using the most efficient method available to the platform.private static int
unsafeEstimateConsecutiveAscii
(long address, int maxChars) Same asUtf8.estimateConsecutiveAscii(ByteBuffer, int, int)
except that it uses the most efficient method available to the platform.private static int
unsafeIncompleteStateFor
(byte[] bytes, int byte1, long offset, int remaining) private static int
unsafeIncompleteStateFor
(long address, int byte1, int remaining) Methods inherited from class com.google.protobuf.Utf8.Processor
decodeUtf8, decodeUtf8Default, encodeUtf8, encodeUtf8Default, isValidUtf8, isValidUtf8, partialIsValidUtf8, partialIsValidUtf8Default
-
Constructor Details
-
UnsafeProcessor
UnsafeProcessor()
-
-
Method Details
-
isAvailable
static boolean isAvailable()Indicates whether or not all required unsafe operations are supported on this platform. -
partialIsValidUtf8
int partialIsValidUtf8(int state, byte[] bytes, int index, int limit) Description copied from class:Utf8.Processor
Tells whether the given byte array slice is a well-formed, malformed, or incomplete UTF-8 byte sequence. The range of bytes to be checked extends from indexindex
, inclusive, tolimit
, exclusive.- Specified by:
partialIsValidUtf8
in classUtf8.Processor
- Parameters:
state
- eitherUtf8.COMPLETE
(if this is the initial decoding operation) or the value returned from a call to a partial decoding method for the previous bytes- Returns:
Utf8.MALFORMED
if the partial byte sequence is definitely not well-formed,Utf8.COMPLETE
if it is well-formed (no additional input needed), or if the byte sequence is "incomplete", i.e. apparently terminated in the middle of a character, an opaque integer "state" value containing enough information to decode the character when passed to a subsequent invocation of a partial decoding method.
-
partialIsValidUtf8Direct
Description copied from class:Utf8.Processor
Performs validation for directByteBuffer
instances.- Specified by:
partialIsValidUtf8Direct
in classUtf8.Processor
-
decodeUtf8
Description copied from class:Utf8.Processor
Decodes the given byte array slice into aString
.- Specified by:
decodeUtf8
in classUtf8.Processor
- Throws:
InvalidProtocolBufferException
- if the byte array slice is not valid UTF-8
-
decodeUtf8Direct
String decodeUtf8Direct(ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferException Description copied from class:Utf8.Processor
Decodes directByteBuffer
instances intoString
.- Specified by:
decodeUtf8Direct
in classUtf8.Processor
- Throws:
InvalidProtocolBufferException
-
encodeUtf8
Description copied from class:Utf8.Processor
Encodes an input character sequence (in
) to UTF-8 in the target array (out
). For a string, this method is similar to
but is more efficient in both time and space. One key difference is that this method requires paired surrogates, and therefore does not support chunking. Whilebyte[] a = string.getBytes(UTF_8); System.arraycopy(a, 0, bytes, offset, a.length); return offset + a.length;
String.getBytes(UTF_8)
replaces unpaired surrogates with the default replacement character, this method throwsUtf8.UnpairedSurrogateException
.To ensure sufficient space in the output buffer, either call
Utf8.encodedLength(java.lang.String)
to compute the exact amount needed, or leave room forUtf8.MAX_BYTES_PER_CHAR * sequence.length()
, which is the largest possible number of bytes that any input can be encoded to.- Specified by:
encodeUtf8
in classUtf8.Processor
- Parameters:
in
- the input character sequence to be encodedout
- the target arrayoffset
- the starting offset inbytes
to start writing atlength
- the length of thebytes
, starting fromoffset
- Returns:
- the new offset, equivalent to
offset + Utf8.encodedLength(sequence)
-
encodeUtf8Direct
Description copied from class:Utf8.Processor
Encodes the input character sequence to a directByteBuffer
instance.- Specified by:
encodeUtf8Direct
in classUtf8.Processor
-
unsafeEstimateConsecutiveAscii
private static int unsafeEstimateConsecutiveAscii(byte[] bytes, long offset, int maxChars) Counts (approximately) the number of consecutive ASCII characters starting from the given position, using the most efficient method available to the platform.- Parameters:
bytes
- the array containing the character sequenceoffset
- the offset position of the index (same as index + arrayBaseOffset)maxChars
- the maximum number of characters to count- Returns:
- the number of ASCII characters found. The stopping position will be at or before the first non-ASCII byte.
-
unsafeEstimateConsecutiveAscii
private static int unsafeEstimateConsecutiveAscii(long address, int maxChars) Same asUtf8.estimateConsecutiveAscii(ByteBuffer, int, int)
except that it uses the most efficient method available to the platform. -
partialIsValidUtf8
private static int partialIsValidUtf8(byte[] bytes, long offset, int remaining) -
partialIsValidUtf8
private static int partialIsValidUtf8(long address, int remaining) -
unsafeIncompleteStateFor
private static int unsafeIncompleteStateFor(byte[] bytes, int byte1, long offset, int remaining) -
unsafeIncompleteStateFor
private static int unsafeIncompleteStateFor(long address, int byte1, int remaining)
-