public abstract class ByteString extends Object implements Comparable<ByteString>
The purpose of this class is to serve as a bridge between Java strings (with their characters corresponding to 16-bit code units in the Basic Multilingual Plane (BMP)) and Lua strings (raw 8-bit sequences).
Byte strings come in two flavours:
java.lang.String
with a given Charset
— constructed using of(String)
;byte
arrays
— constructed using wrap(byte[])
.The ByteString
class provides the functionality for treating both cases
as sequences of bytes when they take part in Lua operations, and as Java strings when
used by an outer Java application. However, these perspectives are as lazy
as possible, in order to avoid doing unnecessary work.
This class provides a natural lexicographical ordering that is consistent with equals.
Modifier and Type | Method and Description |
---|---|
InputStream |
asInputStream()
Returns an input stream that reads the contents of this string.
|
abstract byte |
byteAt(int index)
Returns the byte at position
index . |
abstract ByteIterator |
byteIterator()
Returns an iterator over the bytes in this byte string.
|
int |
compareTo(ByteString that)
Compares this byte string lexicographically with
that . |
ByteString |
concat(ByteString other)
Returns a byte string formed by a concatenating this byte string with the byte string
other . |
ByteString |
concat(String other)
Returns a byte string formed by concatenating this byte string with the string
other . |
static ByteString |
constOf(String s)
Returns a byte string corresponding to the bytes in
s as encoded by the default
charset in a form suitable for use as a string constant. |
boolean |
contains(byte b)
Returns
true if the byte string contains the byte b . |
static ByteString |
copyOf(byte[] bytes)
Returns a byte string containing a copy of the byte array
bytes . |
static ByteString |
copyOf(byte[] bytes,
int offset,
int length)
Returns a byte string containing a copy of a slice of the byte array
bytes
starting at the offset offset and consisting of length bytes. |
String |
decode()
Returns a string represented by this byte string decoded using the default charset
of the virtual machine.
|
String |
decode(Charset charset)
Returns a string representation of this byte string that uses the specified
charset
charset to decode characters from bytes. |
static ByteString |
empty()
Returns an empty byte string.
|
boolean |
equals(Object o)
Returns
true if o is a byte string containing the same bytes as
this byte string. |
static ByteString |
fromRaw(String s)
Returns a new byte string corresponding to bytes in
s by taking the
least significant byte of each character. |
abstract byte[] |
getBytes()
Returns a new byte array containing the bytes of this byte string.
|
abstract int |
hashCode()
Returns the hash code of this byte string.
|
abstract boolean |
isEmpty()
Returns
true iff this byte string is empty, i.e., if the number of bytes it
contains is 0. |
abstract int |
length()
Returns the length of this byte string, i.e., the number of bytes it contains.
|
static ByteString |
of(String s)
Returns a new byte string corresponding to the bytes in
s as encoded
by the default charset (Charset.defaultCharset() ). |
static ByteString |
of(String s,
Charset charset)
Returns a new byte string corresponding to the bytes in
s as encoded
by the specified charset . |
abstract void |
putTo(ByteBuffer buffer)
Puts the contents of this byte string to the specified
buffer . |
ByteString |
replace(ByteString target,
ByteString replacement)
Replaces all occurrences of the byte string
target in this byte string
with the replacement replacement . |
abstract boolean |
startsWith(byte b)
Returns
true if the first byte of this byte string is b . |
abstract ByteString |
substring(int start,
int end)
Returns a substring of this byte string starting at position
start (inclusive),
ending at position end (exclusive). |
abstract String |
toRawString()
Returns a string in which all characters are directly mapped to the bytes in this
byte string by treating them as unsigned integers.
|
abstract String |
toString()
Returns the interpretation of this byte string as a Java string.
|
abstract void |
writeTo(OutputStream stream)
Writes the contents of this byte string to the specified
stream . |
public static ByteString of(String s, Charset charset)
s
as encoded
by the specified charset
.s
- the string to take the byte view of, must not be null
charset
- the charset to use for decoding s
into bytes, must not be null
s
using charset
NullPointerException
- if s
or charset
is null
IllegalArgumentException
- if charset
does not provide encoding
capability (see Charset.canEncode()
)public static ByteString of(String s)
s
as encoded
by the default charset (Charset.defaultCharset()
).s
- the string to take the perspective of, must not be null
s
NullPointerException
- if s
is null
public static ByteString fromRaw(String s)
s
by taking the
least significant byte of each character.s
- the string to get bytes from, must not be null
s
by taking the least significant
byte of each charNullPointerException
- if s
is null
public static ByteString constOf(String s)
s
as encoded by the default
charset in a form suitable for use as a string constant.
This method differs from of(String)
in that it may force the computation
of lazily-evaluated properties of the resulting string at instantiation time and
cache them for use at runtime.
s
- the string to get bytes from, must not be null
s
public static ByteString copyOf(byte[] bytes)
bytes
.bytes
- the byte array to use as the byte string, must not be null
bytes
NullPointerException
- if bytes
is null
public static ByteString copyOf(byte[] bytes, int offset, int length)
bytes
starting at the offset offset
and consisting of length
bytes.bytes
- the byte array to use as the byte string, must not be null
offset
- offset in bytes
to start reading fromlength
- the number of bytes to copy from bytes
bytes
NullPointerException
- if bytes
is null
IndexOutOfBoundsException
- if offset
or length
are negative,
or if (offset + length)
is greater than
bytes.length
public static ByteString empty()
public final boolean equals(Object o)
true
if o
is a byte string containing the same bytes as
this byte string.
Note: this method uses the strict interpretation of byte strings as byte
sequences. It is therefore not necessarily true that for two byte strings a
and b
, the result of their comparison is the same as the result of comparing
their images provided by toString()
:
boolean byteEq = a.equals(b); boolean stringEq = a.toString().equals(b.toString()); // may fail! assert (byteEq == stringEq);
public abstract int hashCode()
String.hashCode()
, interpreting the byte string's bytes
as unsigned integers.public abstract byte[] getBytes()
public abstract byte byteAt(int index)
index
.index
- the position in the stringindex
IndexOutOfBoundsException
- if index < 0
or index >= length()
public abstract ByteIterator byteIterator()
public InputStream asInputStream()
public abstract int length()
public abstract boolean isEmpty()
true
iff this byte string is empty, i.e., if the number of bytes it
contains is 0.true
iff this byte string is emptypublic abstract ByteString substring(int start, int end)
start
(inclusive),
ending at position end
(exclusive).
The indices refer to the byte position in the byte string.
start
- the first index to include in the new substring (inclusive)end
- the smallest index immediately following the new substring in this byte stringstart
(inclusive)
to end
(exclusive)IndexOutOfBoundsException
- if start < 0
, end > length()
or start > end
public abstract void putTo(ByteBuffer buffer)
buffer
.buffer
- the buffer to use, must not be null
NullPointerException
- if buffer
is null
public abstract void writeTo(OutputStream stream) throws IOException
stream
.stream
- the stream to use, must not be null
IOException
- when I/O error happens during the writeNullPointerException
- if stream
is null
public abstract String toString()
public String decode(Charset charset)
charset
to decode characters from bytes.charset
- the charset to use, must not be null
charset
NullPointerException
- if charset
is null
public String decode()
This is effectively equivalent to decode(Charset)
called with Charset.defaultCharset()
.
public abstract String toRawString()
This method is the complement of fromRaw(String)
.
public int compareTo(ByteString that)
that
. Returns a negative
integer, zero, or a positive integer if this
is lesser than, equal to or greater
than that
in this ordering.
For the purposes of this ordering, bytes are interpreted as unsigned integers.
Note: this method uses the strict interpretation of byte strings as byte
sequences. It is therefore not necessarily true that for two byte strings a
and b
, the result of their comparison is the same as the result of comparing
their images provided by toString()
:
int byteCmp = a.compareTo(b); int stringCmp = a.toString().compareTo(b.toString()); // may fail! assert(Integer.signum(byteCmp) == Integer.signum(stringCmp));
This is done in order to ensure that the natural ordering provided by this
compareTo()
is consistent with equals.
compareTo
in interface Comparable<ByteString>
that
- byte string to compare to, must not be null
this
is lexicographically
lesser than, equal to or greater than that
NullPointerException
- if that
is null
public ByteString concat(ByteString other)
other
.
Note: this method uses the non-strict interpretation and therefore may (but might not necessarily) preserve unmappable and malformed characters occurring in the two strings.
other
- the byte string to concatenate this byte string with, must not be null
other
NullPointerException
- if other
is null
public ByteString concat(String other)
other
.
This is a convenience method equivalent to
concat(ByteString.of(other))
other
- the string to concatenate with, must not be null
other
NullPointerException
- if other
is null
public abstract boolean startsWith(byte b)
true
if the first byte of this byte string is b
.b
- the byte to compare the first byte of this byte string totrue
if this byte string starts with b
public boolean contains(byte b)
true
if the byte string contains the byte b
.b
- the byte to search for in the byte stringtrue
if this byte string contains b
public ByteString replace(ByteString target, ByteString replacement)
target
in this byte string
with the replacement replacement
.target
- the substring to replace, must not be null
replacement
- the replacement, must not be null
target
replaced
by replacement
NullPointerException
- if target
or replacement
is null
Copyright © 2016. All rights reserved.