public class ByteString extends Object implements Cloneable, Comparable
String
by directly storing an encoding of that String
in
an array of byte
s. This can save a lot of memory if all of the application's
characters fit in a single byte when encoded by, for instance, UTF-8. In fact, the default
encoding used by this class is UTF-8.Modifier and Type | Field and Description |
---|---|
static String |
defaultEncoding
The default character encoding for instances of this class.
|
static ByteString |
emptyString
A byte string representing
"" . |
protected String |
encoding
The encoding method used by this instance.
|
protected int |
hashCode
The hash code of the
String decoding of this byte string. |
protected byte[] |
value
The encoded characters.
|
Modifier | Constructor and Description |
---|---|
|
ByteString()
Creates an empty byte string.
|
protected |
ByteString(boolean b)
For internal use only.
|
|
ByteString(ByteString b,
String e)
Creates a byte string with the given encoding, which may involve converting the specified
byte string's contents if the encodings differ.
|
|
ByteString(String s)
Creates a byte string by using the default encoding to encode the specified string.
|
|
ByteString(String s,
String e)
Creates a byte string by using the specified encoding to encode the specified string.
|
Modifier and Type | Method and Description |
---|---|
ByteString |
append(ByteString b)
Appends the string represented by the given byte string onto the existing content in this
object.
|
ByteString |
append(ByteString[] b)
Appends the strings represented by the given byte strings onto the existing content in this
object.
|
ByteString |
append(String s)
Appends the encoding of the given string onto the existing encoding in this object.
|
ByteString |
append(String[] s)
Appends the encodings of all the given strings onto the existing encoding in this object.
|
Object |
clone()
Returns a shallow copy of this string.
|
int |
compareTo(Object o)
If the argument object is a byte string, this object's byte array and the argument object's
byte array are compared lexicographically.
|
boolean |
equals(Object o)
Two byte strings are equivalent if they encode the same string.
|
byte |
getByte(int i)
Returns the byte at index
i of value . |
String |
getEncoding()
Returns the name of the encoding method of this byte string.
|
protected void |
handleEncodingException(Exception e)
Handles exceptions generated by unsupported encodings.
|
int |
hashCode()
Returns a hash code for this object.
|
int |
length()
Returns the length of
value . |
void |
lexRead(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in,
ByteString i)
Reads the representation of a byte string as stored by a lexicon, overwriting the data in
this object.
|
static ByteString |
lexReadByteString(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in,
ByteString i)
Reads and returns a byte string as written by a lexicon.
|
void |
lexWrite(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out,
ByteString i)
Writes a binary representation of this byte string intended for use by a lexicon, omitting
redundant information when possible.
|
void |
read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Reads in a complete binary representation of a byte string.
|
static ByteString |
readByteString(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Reads and returns a byte string from an input stream.
|
void |
setValue(String s)
Sets the value of this byte string to the byte encoding of the specified string.
|
String |
toString()
Returns a decoded string.
|
void |
write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
Writes a complete binary representation of this byte string.
|
public static final String defaultEncoding
public static final ByteString emptyString
""
.protected String encoding
protected byte[] value
protected int hashCode
String
decoding of this byte string.protected ByteString(boolean b)
b
- Dummy variable to make a new signature.public ByteString()
public ByteString(String s)
s
- The string to encode.public ByteString(String s, String e)
s
- The string to encode.e
- The encoding method.public ByteString(ByteString b, String e)
b
- The original byte string.e
- The new encoding.protected void handleEncodingException(Exception e)
e
- The exception.public static ByteString readByteString(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
in
- The input stream.public static ByteString lexReadByteString(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in, ByteString i)
in
- The input stream.i
- The assumed identifier. If no identifier is given in the input stream, the
instantiated feature is given this identifier.public String getEncoding()
public void setValue(String s)
s
- The string to encode.public int length()
value
.public byte getByte(int i)
i
of value
.i
- The index of the requested byte.public ByteString append(String s)
value
reference in this object.
Warning: Depending on the character encoding in use, this may introduce byte order markers into the middle of this object's byte array, which usually is not desired.
s
- The string whose encoding will be appended.public ByteString append(String[] s)
value
reference in this object.
Warning: Depending on the character encoding in use, this may introduce byte order markers into the middle of this object's byte array, which usually is not desired.
s
- The strings whose encodings will be appended.public ByteString append(ByteString b)
value
reference in this object.
Warning: Depending on the character encoding in use, this may introduce byte order markers into the middle of this object's byte array, which usually is not desired.
b
- The string whose encoding will be appended.public ByteString append(ByteString[] b)
value
reference in this object.
Warning: Depending on the character encoding in use, this may introduce byte order markers into the middle of this object's byte array, which usually is not desired.
b
- The strings whose encodings will be appended.public int compareTo(Object o)
compareTo
in interface Comparable
public int hashCode()
public boolean equals(Object o)
public void write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
out
- The output stream.public void read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
in
- The input stream.public void lexWrite(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out, ByteString i)
out
- The output stream.i
- The assumed identifier string. This byte strings value, encoding, or both may be
omitted if they are equivalent to i
.public void lexRead(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in, ByteString i)
This method is appropriate for reading byte strings as written by
lexWrite(ExceptionlessOutputStream,ByteString)
.
in
- The input stream.i
- The assumed identifier string.public Object clone()
append(String)
, append(ByteString)
, and
setValue(String)
modify the value
field itself, but the reference is merely
replaced; the contents of the original array do not change.)Copyright © 2016. All rights reserved.