com.google.protobuf
Class ByteString

java.lang.Object
  extended by com.google.protobuf.ByteString

public final class ByteString
extends java.lang.Object

Immutable array of bytes.

Author:
[email protected] Bob Lee, [email protected] Kenton Varda

Nested Class Summary
static class ByteString.Output
          Outputs to a ByteString instance.
 
Field Summary
static ByteString EMPTY
          Empty ByteString.
 
Method Summary
 java.nio.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(java.lang.String text, java.lang.String charsetName)
          Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString.
static ByteString copyFromUtf8(java.lang.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.
 boolean equals(java.lang.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.
 java.io.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[].
 java.lang.String toString(java.lang.String charsetName)
          Constructs a new String by decoding the bytes using the specified charset.
 java.lang.String toStringUtf8()
          Constructs a new String by decoding the bytes as UTF-8.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY

public static final ByteString EMPTY
Empty ByteString.

Method Detail

byteAt

public byte byteAt(int index)
Gets the byte at the given index.

Throws:
java.lang.ArrayIndexOutOfBoundsException - index is < 0 or >= size

size

public int size()
Gets the number of bytes.


isEmpty

public boolean isEmpty()
Returns true if the size is 0, false otherwise.


copyFrom

public static ByteString copyFrom(byte[] bytes,
                                  int offset,
                                  int size)
Copies the given bytes into a ByteString.


copyFrom

public static ByteString copyFrom(byte[] bytes)
Copies the given bytes into a ByteString.


copyFrom

public static ByteString copyFrom(java.lang.String text,
                                  java.lang.String charsetName)
                           throws java.io.UnsupportedEncodingException
Encodes text into a sequence of bytes using the named charset and returns the result as a ByteString.

Throws:
java.io.UnsupportedEncodingException

copyFromUtf8

public static ByteString copyFromUtf8(java.lang.String text)
Encodes text into a sequence of UTF-8 bytes and returns the result as a ByteString.


copyTo

public void copyTo(byte[] target,
                   int offset)
Copies bytes into a buffer at the given offset.

Parameters:
target - buffer to copy into
offset - 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 into
sourceOffset - offset within these bytes
targetOffset - offset within the target buffer
size - number of bytes to copy

toByteArray

public byte[] toByteArray()
Copies bytes to a byte[].


asReadOnlyByteBuffer

public java.nio.ByteBuffer asReadOnlyByteBuffer()
Constructs a new read-only java.nio.ByteBuffer with the same backing byte array.


toString

public java.lang.String toString(java.lang.String charsetName)
                          throws java.io.UnsupportedEncodingException
Constructs a new String by decoding the bytes using the specified charset.

Throws:
java.io.UnsupportedEncodingException

toStringUtf8

public java.lang.String toStringUtf8()
Constructs a new String by decoding the bytes as UTF-8.


equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

newInput

public java.io.InputStream newInput()
Creates an InputStream which can be used to read the bytes.


newCodedInput

public CodedInputStream newCodedInput()
Creates a CodedInputStream which can be used to read the bytes. Using this is more efficient than creating a CodedInputStream wrapping the result of newInput().


newOutput

public static ByteString.Output newOutput(int initialCapacity)
Creates a new ByteString.Output with the given initial capacity.


newOutput

public static ByteString.Output newOutput()
Creates a new ByteString.Output.



Copyright © 2008-2009 Google. All Rights Reserved.