Class ByteString
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]static final ByteStringEmpty ByteString.static final byte[]Empty byte array.static final StringEmpty String.private int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConstructs a new read-onlyjava.nio.ByteBufferwith the same backing byte array.static byte[]byteArrayDefaultValue(String bytes) Helper called by generated code to construct default values for byte array fields.bytebyteAt(int index) Gets the byte at the given index.static ByteStringbytesDefaultValue(String bytes) Helper called by generated code to construct default values for bytes fields.static ByteStringcopyFrom(byte[] bytes) Copies the given bytes into aByteString.static ByteStringcopyFrom(byte[] bytes, int offset, int size) Copies the given bytes into aByteString.static ByteStringEncodestextinto a sequence of bytes using the named charset and returns the result as aByteString.static ByteStringcopyFromUtf8(String text) Encodestextinto a sequence of UTF-8 bytes and returns the result as aByteString.voidcopyTo(byte[] target, int offset) Copies bytes into a buffer at the given offset.voidcopyTo(byte[] target, int sourceOffset, int targetOffset, int size) Copies bytes into a buffer.booleanequals(byte[] data) Returns true if the contents of the internal array and the provided array match.booleanequals(byte[] data, int offset, int len) Returns true if the contents of the internal array and the provided array match.static booleanequals(ByteString bs, ByteString other, boolean checkHash) Returns true if the contents of both match.boolean(package private) byte[]getBytes()inthashCode()booleanisEmpty()Returnstrueif the size is0,falseotherwise.intsize()Gets the number of bytes.static StringstringDefaultValue(String bytes) Helper called by generated code to construct default values for string fields.byte[]Copies bytes to abyte[].toString()Constructs a newStringby decoding the bytes as UTF-8.(package private) static ByteStringwrap(byte[] bytes) static voidwriteTo(Output output, ByteString bs, int fieldNumber, boolean repeated) Writes the bytes to theOutput.static voidwriteTo(DataOutput out, ByteString bs) Writes the bytes to theDataOutput.static voidwriteTo(OutputStream out, ByteString bs) Writes the bytes to theOutputStream.
-
Field Details
-
bytes
private final byte[] bytes -
EMPTY_STRING
-
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAYEmpty byte array. -
EMPTY
Empty ByteString. -
hash
private volatile int hash
-
-
Constructor Details
-
ByteString
private ByteString(byte[] bytes)
-
-
Method Details
-
wrap
-
getBytes
byte[] getBytes() -
writeTo
Writes the bytes to theOutputStream.- Throws:
IOException
-
writeTo
Writes the bytes to theDataOutput.- Throws:
IOException
-
writeTo
public static void writeTo(Output output, ByteString bs, int fieldNumber, boolean repeated) throws IOException Writes the bytes to theOutput.- Throws:
IOException
-
toString
-
byteAt
public byte byteAt(int index) Gets the byte at the given index.- Throws:
ArrayIndexOutOfBoundsException-indexis < 0 or >= size
-
size
public int size()Gets the number of bytes. -
isEmpty
public boolean isEmpty()Returnstrueif the size is0,falseotherwise. -
copyFrom
Copies the given bytes into aByteString. -
copyFrom
Copies the given bytes into aByteString. -
copyFrom
Encodestextinto a sequence of bytes using the named charset and returns the result as aByteString. -
copyFromUtf8
Encodestextinto a sequence of UTF-8 bytes and returns the result as aByteString. -
copyTo
public void copyTo(byte[] target, int offset) Copies bytes into a buffer at the given offset.- Parameters:
target- buffer to copy intooffset- in the target buffer
-
copyTo
public void copyTo(byte[] target, int sourceOffset, int targetOffset, int size) Copies bytes into a buffer.- Parameters:
target- buffer to copy intosourceOffset- offset within these bytestargetOffset- offset within the target buffersize- number of bytes to copy
-
toByteArray
public byte[] toByteArray()Copies bytes to abyte[]. -
asReadOnlyByteBuffer
Constructs a new read-onlyjava.nio.ByteBufferwith the same backing byte array. -
toStringUtf8
Constructs a newStringby decoding the bytes as UTF-8. -
equals
-
equals
Returns true if the contents of both match. -
equals
public boolean equals(byte[] data) Returns true if the contents of the internal array and the provided array match. -
equals
public boolean equals(byte[] data, int offset, int len) Returns true if the contents of the internal array and the provided array match. -
hashCode
-
stringDefaultValue
Helper called by generated code to construct default values for string fields.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.
-
bytesDefaultValue
Helper called by generated code to construct default values for bytes fields.This is a lot like
stringDefaultValue(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. -
byteArrayDefaultValue
Helper called by generated code to construct default values for byte array fields.This is a lot like
stringDefaultValue(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.
-