public class Internal extends Object
protobuf package. Others should not use this class directly.
| Modifier and Type | Class and Description |
|---|---|
static interface |
Internal
Interface for an enum value or value descriptor, to be used in FieldSet.
|
static interface |
Internal
Interface for an object which maps integers to
Internal.EnumLites.
|
| Modifier and Type | Field and Description |
|---|---|
static byte[] |
EMPTY_BYTE_ARRAY
An empty byte array constant used in generated code.
|
static ByteBuffer |
EMPTY_BYTE_BUFFER
An empty byte array constant used in generated code.
|
| Constructor and Description |
|---|
Internal()
|
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
byteArrayDefaultValue(String
Helper called by generated code to construct default values for bytes fields.
|
static ByteBuffer |
byteBufferDefaultValue(String
Helper called by generated code to construct default values for bytes fields.
|
static ByteString |
bytesDefaultValue(String
Helper called by generated code to construct default values for bytes fields.
|
static ByteBuffer |
copyByteBuffer(ByteBuffer
Create a new ByteBuffer and copy all the content of
source ByteBuffer to the new ByteBuffer.
|
static boolean |
equals(List
Helper method for implementing
MessageLite#equals() for bytes field.
|
static boolean |
equalsByteBuffer(ByteBuffer
Helper method for implementing
MessageLite#equals() for bytes field.
|
static boolean |
equalsByteBuffer(List
Helper method for implementing
MessageLite#equals() for bytes field.
|
static int |
hashBoolean(boolean b)
Helper method for implementing
MessageLite#hashCode() for booleans.
|
static int |
hashCode(byte[] bytes)
Helper method for implementing
MessageLite#hashCode() for bytes field.
|
static int |
hashCode(List
Helper method for implementing
MessageLite#hashCode() for bytes field.
|
static int |
hashCodeByteBuffer(ByteBuffer
Helper method for implementing
MessageLite#hashCode() for bytes field.
|
static int |
hashCodeByteBuffer(List
Helper method for implementing
MessageLite#hashCode() for bytes field.
|
static int |
hashEnum(Internal
Helper method for implementing
MessageLite#hashCode() for enums.
|
static int |
hashEnumList(List
Helper method for implementing
MessageLite#hashCode() for enum lists.
|
static int |
hashLong(long n)
Helper method for implementing
MessageLite#hashCode() for longs.
|
static boolean |
isValidUtf8(byte[] byteArray)
Like
isValidUtf8(ByteString) but for byte arrays.
|
static boolean |
isValidUtf8(ByteString
Helper called by generated code to determine if a byte array is a valid UTF-8 encoded string such that the original bytes can be converted to a String object and then back to a byte array round tripping the bytes without loss.
|
static String |
stringDefaultValue(String
Helper called by generated code to construct default values for string fields.
|
static byte[] |
toByteArray(String
Helper method to get the UTF-8 bytes of a string.
|
static String |
toStringUtf8(byte[] bytes)
Helper method to convert a byte array to a string using UTF-8 encoding.
|
public static final byte[] EMPTY_BYTE_ARRAY
public static final ByteBufferEMPTY_BYTE_BUFFER
public static StringstringDefaultValue(String bytes)
The protocol compiler does not actually contain a UTF-8 decoder -- it just pushes UTF-8-encoded text around without touching it. The one place where this presents a problem is when generating Java string literals. Unicode characters in the string literal would normally need to be encoded using a Unicode escape sequence, which would require decoding them. To get around this, protoc instead embeds the UTF-8 bytes into the generated code and leaves it to the runtime library to decode them.
It gets worse, though. If protoc just generated a byte array, like: new byte[] {0x12, 0x34, 0x56, 0x78} Java actually generates *code* which allocates an array and then fills in each value. This is much less efficient than just embedding the bytes directly into the bytecode. To get around this, we need another work-around. String literals are embedded directly, so protoc actually generates a string literal corresponding to the bytes. The easiest way to do this is to use the ISO-8859-1 character set, which corresponds to the first 256 characters of the Unicode range. Protoc can then use good old CEscape to generate the string.
So we have a string literal which represents a set of bytes which represents another string. This function -- stringDefaultValue -- converts from the generated string to the string we actually want. The generated code calls this automatically.
public static ByteStringbytesDefaultValue(String bytes)
This is a lot like stringDefaultValue(java.lang.String), but for bytes fields. In this case we only need the second of the two hacks -- allowing us to embed raw bytes as a string literal with ISO-8859-1 encoding.
public static byte[] byteArrayDefaultValue(Stringbytes)
This is like bytesDefaultValue(java.lang.String), but returns a byte array.
public static ByteBufferbyteBufferDefaultValue(String bytes)
This is like bytesDefaultValue(java.lang.String), but returns a ByteBuffer.
public static ByteBuffercopyByteBuffer(ByteBuffer source)
source ByteBuffer to the new ByteBuffer. The new ByteBuffer's limit and capacity will be source.capacity(), and its position will be 0. Note that the state of
source ByteBuffer won't be changed.
public static boolean isValidUtf8(ByteStringbyteString)
true whenever:
Arrays.equals(byteString.toByteArray(), new String(byteString.toByteArray(), "UTF-8").getBytes("UTF-8"))
This method rejects "overlong" byte sequences, as well as 3-byte sequences that would map to a surrogate character, in accordance with the restricted definition of UTF-8 introduced in Unicode 3.1. Note that the UTF-8 decoder included in Oracle's JDK has been modified to also reject "overlong" byte sequences, but currently (2011) still accepts 3-byte surrogate character byte sequences.
See the Unicode Standard,
Table 3-6. UTF-8 Bit Distribution,
Table 3-7. Well Formed UTF-8 Byte Sequences.
As of 2011-02, this method simply returns the result of ByteString. Calling that method directly is preferred.
byteString - the string to check
public static boolean isValidUtf8(byte[] byteArray)
isValidUtf8(ByteString) but for byte arrays.
public static byte[] toByteArray(Stringvalue)
public static StringtoStringUtf8(byte[] bytes)
public static int hashLong(long n)
MessageLite#hashCode() for longs.
Long.hashCode()
public static int hashBoolean(boolean b)
MessageLite#hashCode() for booleans.
Boolean.hashCode()
public static int hashEnum(Internal.EnumLite e)
MessageLite#hashCode() for enums.
This is needed because Enum is final, but we need to use the field number as the hash code to ensure compatibility between statically and dynamically generated enum objects.
public static int hashEnumList(List<? extends Internal .EnumLite > list)
MessageLite#hashCode() for enum lists.
public static boolean equals(List<byte[]> a, List <byte[]> b)
MessageLite#equals() for bytes field.
public static int hashCode(List<byte[]> list)
MessageLite#hashCode() for bytes field.
public static int hashCode(byte[] bytes)
MessageLite#hashCode() for bytes field.
public static boolean equalsByteBuffer(ByteBuffera, ByteBuffer b)
MessageLite#equals() for bytes field.
public static boolean equalsByteBuffer(List<ByteBuffer > a, List <ByteBuffer > b)
MessageLite#equals() for bytes field.
public static int hashCodeByteBuffer(List<ByteBuffer > list)
MessageLite#hashCode() for bytes field.
public static int hashCodeByteBuffer(ByteBufferbytes)
MessageLite#hashCode() for bytes field.