Class Utf8.Processor
- java.lang.Object
-
- com.google.protobuf.Utf8.Processor
-
- Direct Known Subclasses:
Utf8.SafeProcessor,Utf8.UnsafeProcessor
- Enclosing class:
- Utf8
abstract static class Utf8.Processor extends java.lang.ObjectA processor of UTF-8 strings, providing methods for checking validity and encoding.
-
-
Constructor Summary
Constructors Constructor Description Processor()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) abstract java.lang.StringdecodeUtf8(byte[] bytes, int index, int size)Decodes the given byte array slice into aString.(package private) java.lang.StringdecodeUtf8(java.nio.ByteBuffer buffer, int index, int size)Decodes the given portion of theByteBufferinto aString.(package private) java.lang.StringdecodeUtf8Default(java.nio.ByteBuffer buffer, int index, int size)DecodesByteBufferinstances using theByteBufferAPI rather than potentially faster approaches.(package private) abstract java.lang.StringdecodeUtf8Direct(java.nio.ByteBuffer buffer, int index, int size)Decodes directByteBufferinstances intoString.(package private) abstract intencodeUtf8(java.lang.String in, byte[] out, int offset, int length)Encodes an input character sequence (in) to UTF-8 in the target array (out).(package private) voidencodeUtf8(java.lang.String in, java.nio.ByteBuffer out)Encodes an input character sequence (in) to UTF-8 in the target buffer (out).(package private) voidencodeUtf8Default(java.lang.String in, java.nio.ByteBuffer out)Encodes the input character sequence to aByteBufferinstance using theByteBufferAPI, rather than potentially faster approaches.(package private) abstract voidencodeUtf8Direct(java.lang.String in, java.nio.ByteBuffer out)Encodes the input character sequence to a directByteBufferinstance.(package private) booleanisValidUtf8(byte[] bytes, int index, int limit)Returnstrueif the given byte array slice is a well-formed UTF-8 byte sequence.(package private) booleanisValidUtf8(java.nio.ByteBuffer buffer, int index, int limit)Returnstrueif the given portion of theByteBufferis a well-formed UTF-8 byte sequence.(package private) abstract intpartialIsValidUtf8(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.(package private) intpartialIsValidUtf8(int state, java.nio.ByteBuffer buffer, int index, int limit)Indicates whether or not the given buffer contains a valid UTF-8 string.private static intpartialIsValidUtf8(java.nio.ByteBuffer buffer, int index, int limit)Performs validation forByteBufferinstances using theByteBufferAPI rather than potentially faster approaches.(package private) intpartialIsValidUtf8Default(int state, java.nio.ByteBuffer buffer, int index, int limit)Performs validation forByteBufferinstances using theByteBufferAPI rather than potentially faster approaches.(package private) abstract intpartialIsValidUtf8Direct(int state, java.nio.ByteBuffer buffer, int index, int limit)Performs validation for directByteBufferinstances.
-
-
-
Method Detail
-
isValidUtf8
final boolean isValidUtf8(byte[] bytes, int index, int limit)Returnstrueif the given byte array slice is a well-formed UTF-8 byte sequence. The range of bytes to be checked extends from indexindex, inclusive, tolimit, exclusive.This is a convenience method, equivalent to
partialIsValidUtf8(bytes, index, limit) == Utf8.COMPLETE.
-
partialIsValidUtf8
abstract 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. The range of bytes to be checked extends from indexindex, inclusive, tolimit, exclusive.- 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.MALFORMEDif the partial byte sequence is definitely not well-formed,Utf8.COMPLETEif 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.
-
isValidUtf8
final boolean isValidUtf8(java.nio.ByteBuffer buffer, int index, int limit)Returnstrueif the given portion of theByteBufferis a well-formed UTF-8 byte sequence. The range of bytes to be checked extends from indexindex, inclusive, tolimit, exclusive.This is a convenience method, equivalent to
partialIsValidUtf8(bytes, index, limit) == Utf8.COMPLETE.
-
partialIsValidUtf8
final int partialIsValidUtf8(int state, java.nio.ByteBuffer buffer, int index, int limit)Indicates whether or not the given buffer contains a valid UTF-8 string.- Parameters:
buffer- the buffer to check.- Returns:
trueif the given buffer contains a valid UTF-8 string.
-
partialIsValidUtf8Direct
abstract int partialIsValidUtf8Direct(int state, java.nio.ByteBuffer buffer, int index, int limit)Performs validation for directByteBufferinstances.
-
partialIsValidUtf8Default
final int partialIsValidUtf8Default(int state, java.nio.ByteBuffer buffer, int index, int limit)Performs validation forByteBufferinstances using theByteBufferAPI rather than potentially faster approaches. This first completes validation for the current character (provided bystate) and then finishes validation for the sequence.
-
partialIsValidUtf8
private static int partialIsValidUtf8(java.nio.ByteBuffer buffer, int index, int limit)Performs validation forByteBufferinstances using theByteBufferAPI rather than potentially faster approaches.
-
decodeUtf8
abstract java.lang.String decodeUtf8(byte[] bytes, int index, int size) throws InvalidProtocolBufferExceptionDecodes the given byte array slice into aString.- Throws:
InvalidProtocolBufferException- if the byte array slice is not valid UTF-8
-
decodeUtf8
final java.lang.String decodeUtf8(java.nio.ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferExceptionDecodes the given portion of theByteBufferinto aString.- Throws:
InvalidProtocolBufferException- if the portion of the buffer is not valid UTF-8
-
decodeUtf8Direct
abstract java.lang.String decodeUtf8Direct(java.nio.ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferExceptionDecodes directByteBufferinstances intoString.- Throws:
InvalidProtocolBufferException
-
decodeUtf8Default
final java.lang.String decodeUtf8Default(java.nio.ByteBuffer buffer, int index, int size) throws InvalidProtocolBufferExceptionDecodesByteBufferinstances using theByteBufferAPI rather than potentially faster approaches.- Throws:
InvalidProtocolBufferException
-
encodeUtf8
abstract int encodeUtf8(java.lang.String in, byte[] out, int offset, int length)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.- Parameters:
in- the input character sequence to be encodedout- the target arrayoffset- the starting offset inbytesto start writing atlength- the length of thebytes, starting fromoffset- Returns:
- the new offset, equivalent to
offset + Utf8.encodedLength(sequence) - Throws:
Utf8.UnpairedSurrogateException- ifsequencecontains ill-formed UTF-16 (unpaired surrogates)java.lang.ArrayIndexOutOfBoundsException- ifsequenceencoded in UTF-8 is longer thanbytes.length - offset
-
encodeUtf8
final void encodeUtf8(java.lang.String in, java.nio.ByteBuffer out)Encodes an input character sequence (in) to UTF-8 in the target buffer (out). Upon returning from this method, theoutposition will point to the position after the last encoded byte. This method requires paired surrogates, and therefore does not support chunking.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 * in.length(), which is the largest possible number of bytes that any input can be encoded to.- Parameters:
in- the source character sequence to be encodedout- the target buffer- Throws:
Utf8.UnpairedSurrogateException- ifincontains ill-formed UTF-16 (unpaired surrogates)java.lang.ArrayIndexOutOfBoundsException- ifinencoded in UTF-8 is longer thanout.remaining()
-
encodeUtf8Direct
abstract void encodeUtf8Direct(java.lang.String in, java.nio.ByteBuffer out)Encodes the input character sequence to a directByteBufferinstance.
-
encodeUtf8Default
final void encodeUtf8Default(java.lang.String in, java.nio.ByteBuffer out)Encodes the input character sequence to aByteBufferinstance using theByteBufferAPI, rather than potentially faster approaches.
-
-