public final class ByteString extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ByteString.Output
Outputs to a
ByteString instance. |
Modifier and Type | Field and Description |
---|---|
static ByteString |
EMPTY
Empty ByteString.
|
Modifier and Type | Method and Description |
---|---|
ByteBuffer |
asReadOnlyByteBuffer()
Constructs a new read-only
java.nio.ByteBuffer with the
same backing byte array. |
byte |
byteAt(int index)
Gets the byte at the given index.
|
static ByteString |
copyFrom(byte[] bytes)
Copies the given bytes into a
ByteString . |
static ByteString |
copyFrom(byte[] bytes,
int offset,
int size)
Copies the given bytes into a
ByteString . |
static ByteString |
copyFrom(ByteBuffer bytes)
Copies the remaining bytes from a
java.nio.ByteBuffer into
a ByteString . |
static ByteString |
copyFrom(ByteBuffer bytes,
int size)
Copies
size bytes from a java.nio.ByteBuffer into
a ByteString . |
static ByteString |
copyFrom(List<ByteString> list)
Concatenates all byte strings in the list and returns the result.
|
static ByteString |
copyFrom(String text,
String charsetName)
Encodes
text into a sequence of bytes using the named charset
and returns the result as a ByteString . |
static ByteString |
copyFromUtf8(String text)
Encodes
text into a sequence of UTF-8 bytes and returns the
result as a ByteString . |
void |
copyTo(byte[] target,
int offset)
Copies bytes into a buffer at the given offset.
|
void |
copyTo(byte[] target,
int sourceOffset,
int targetOffset,
int size)
Copies bytes into a buffer.
|
void |
copyTo(ByteBuffer target)
Copies bytes into a ByteBuffer.
|
boolean |
equals(Object o) |
int |
hashCode() |
boolean |
isEmpty()
Returns
true if the size is 0 , false otherwise. |
CodedInputStream |
newCodedInput()
Creates a
CodedInputStream which can be used to read the bytes. |
InputStream |
newInput()
Creates an
InputStream which can be used to read the bytes. |
static ByteString.Output |
newOutput()
Creates a new
ByteString.Output . |
static ByteString.Output |
newOutput(int initialCapacity)
Creates a new
ByteString.Output with the given initial capacity. |
int |
size()
Gets the number of bytes.
|
byte[] |
toByteArray()
Copies bytes to a
byte[] . |
String |
toString(String charsetName)
Constructs a new
String by decoding the bytes using the
specified charset. |
String |
toStringUtf8()
Constructs a new
String by decoding the bytes as UTF-8. |
public static final ByteString EMPTY
public byte byteAt(int index)
ArrayIndexOutOfBoundsException
- index
is < 0 or >= sizepublic int size()
public boolean isEmpty()
true
if the size is 0
, false
otherwise.public static ByteString copyFrom(byte[] bytes, int offset, int size)
ByteString
.public static ByteString copyFrom(byte[] bytes)
ByteString
.public static ByteString copyFrom(ByteBuffer bytes, int size)
size
bytes from a java.nio.ByteBuffer
into
a ByteString
.public static ByteString copyFrom(ByteBuffer bytes)
java.nio.ByteBuffer
into
a ByteString
.public static ByteString copyFrom(String text, String charsetName) throws UnsupportedEncodingException
text
into a sequence of bytes using the named charset
and returns the result as a ByteString
.UnsupportedEncodingException
public static ByteString copyFromUtf8(String text)
text
into a sequence of UTF-8 bytes and returns the
result as a ByteString
.public static ByteString copyFrom(List<ByteString> list)
The returned ByteString
is not necessarily a unique object.
If the list is empty, the returned object is the singleton empty
ByteString
. If the list has only one element, that
ByteString
will be returned without copying.
public void copyTo(byte[] target, int offset)
target
- buffer to copy intooffset
- in the target bufferpublic void copyTo(byte[] target, int sourceOffset, int targetOffset, int size)
target
- buffer to copy intosourceOffset
- offset within these bytestargetOffset
- offset within the target buffersize
- number of bytes to copypublic void copyTo(ByteBuffer target)
target
- ByteBuffer to copy into.ReadOnlyBufferException
- if the target
is read-onlyBufferOverflowException
- if the target
's remaining()
space is not large enough to hold the data.public byte[] toByteArray()
byte[]
.public ByteBuffer asReadOnlyByteBuffer()
java.nio.ByteBuffer
with the
same backing byte array.public String toString(String charsetName) throws UnsupportedEncodingException
String
by decoding the bytes using the
specified charset.UnsupportedEncodingException
public String toStringUtf8()
String
by decoding the bytes as UTF-8.public InputStream newInput()
InputStream
which can be used to read the bytes.public CodedInputStream newCodedInput()
CodedInputStream
which can be used to read the bytes.
Using this is more efficient than creating a CodedInputStream
wrapping the result of newInput()
.public static ByteString.Output newOutput(int initialCapacity)
ByteString.Output
with the given initial capacity.public static ByteString.Output newOutput()
ByteString.Output
.Copyright © 2008-2015. All Rights Reserved.