Package org.xnio
Class ByteString
java.lang.Object
org.xnio.ByteString
- All Implemented Interfaces:
Serializable,CharSequence,Comparable<ByteString>
public final class ByteString
extends Object
implements Comparable<ByteString>, Serializable, CharSequence
An immutable string of bytes. Since instances of this class are guaranteed to be immutable, they are
safe to use as
Option values and in an OptionMap. Some operations can treat this byte string
as an ASCII- or ISO-8858-1-encoded character string.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidappendTo(ByteBuffer dest) Append the bytes of this string into the given buffer.bytebyteAt(int idx) Get the byte at an index.charcharAt(int index) intcompareTo(ByteString other) Compare this string to another in a case-sensitive manner.intcompareToIgnoreCase(ByteString other) Compare this string to another in a case-insensitive manner.concat(byte[] suffixBytes) concat(byte[] suffixBytes, int offs, int len) static ByteStringstatic ByteStringconcat(String prefix, ByteString suffix) concat(ByteString suffix) concat(ByteString suffix, int offs, int len) booleanDetermine whether this string contains another string (case-sensitive).booleancontains(ByteString other) Determine whether this string contains another string (case-sensitive).booleancontainsIgnoreCase(String other) Determine whether this string contains another string (case-sensitive).booleancontainsIgnoreCase(ByteString other) Determine whether this string contains another string (case-insensitive).static ByteStringcopyOf(byte[] b, int offs, int len) Create a byte string from the given array segment.voidcopyTo(byte[] dst) Copy all the bytes from this string to the given array at the given offset.voidcopyTo(byte[] dst, int offs) Copy all the bytes from this string to the given array at the given offset.voidcopyTo(byte[] dst, int offs, int len) Copylenbytes from this string to the given array at the given offset.voidcopyTo(int srcOffs, byte[] dst, int offs, int len) Copylenbytes from this string at offsetsrcOffsto the given array at the given offset.booleanendsWith(char suffix) booleanbooleanendsWith(ByteString suffix) booleanendsWithIgnoreCase(char suffix) booleanendsWithIgnoreCase(String suffix) booleanendsWithIgnoreCase(ByteString suffix) booleanDetermine if this ByteString equals another ByteString.booleanequals(ByteString other) Determine if this ByteString equals another ByteString.booleanequalsIgnoreCase(ByteString other) Determine if this ByteString equals another ByteString, ignoring case (ASCII).booleanequalToString(String str) Determine whether thisByteStringis equal (case-sensitively) to the givenString.booleanDetermine whether thisByteStringis equal (case-insensitively) to the givenString.static ByteStringfromInt(int val) Get a string version of the given value.static ByteStringfromLong(long val) Get a string version of the given value.byte[]getBytes()Get a copy of the bytes of this ByteString.voidgetBytes(byte[] dest) Deprecated.voidgetBytes(byte[] dest, int offs) Deprecated.Replaced bycopyTo(byte[],int).voidgetBytes(byte[] dest, int offs, int len) Deprecated.Replaced bycopyTo(byte[],int,int).static ByteStringGet a byte string from the bytes of the character string.static ByteStringGet a byte string from the bytes of a character string.static ByteStringGet a byte string from the bytes of a character string.static ByteStringgetBytes(ByteBuffer buffer) Get a byte string from all remaining bytes of a ByteBuffer.static ByteStringgetBytes(ByteBuffer buffer, int length) Get a byte string from a ByteBuffer.inthashCode()Get the hash code for this ByteString.intintindexOf(char c) Get the index of the given character in this string.intindexOf(char c, int start) Get the index of the given character in this string.intintintindexOf(ByteString other) intindexOf(ByteString other, int start) intindexOfIgnoreCase(String other) intindexOfIgnoreCase(String other, int start) intindexOfIgnoreCase(ByteString other) intindexOfIgnoreCase(ByteString other, int start) intlastIndexOf(char c) Get the last index of the given character in this string.intlastIndexOf(char c, int start) Get the last index of the given character in this string.intlastIndexOf(String other) intlastIndexOf(String other, int start) intlastIndexOf(ByteString other) intlastIndexOf(ByteString other, int start) intlastIndexOfIgnoreCase(String other) intlastIndexOfIgnoreCase(String other, int start) intlastIndexOfIgnoreCase(ByteString other) intlastIndexOfIgnoreCase(ByteString other, int start) intlength()Get the number of bytes in this byte string.static ByteStringof(byte... bytes) Create a byte string of the given literal bytes.booleanregionMatches(boolean ignoreCase, int offset, byte[] other, int otherOffset, int len) booleanregionMatches(boolean ignoreCase, int offset, String other, int otherOffset, int len) booleanregionMatches(boolean ignoreCase, int offset, ByteString other, int otherOffset, int len) booleanstartsWith(char prefix) booleanstartsWith(String prefix) booleanstartsWith(ByteString prefix) booleanstartsWithIgnoreCase(char prefix) booleanstartsWithIgnoreCase(String prefix) booleanstartsWithIgnoreCase(ByteString prefix) subSequence(int start, int end) substring(int offs) Get the substring of this string starting at the given offset.substring(int offs, int len) Get the substring of this string starting at the given offset.inttoInt()Get the unsignedintvalue of this string.inttoInt(int start) Get the unsignedintvalue of this string.longtoLong()Get the unsignedlongvalue of this string.longtoLong(int start) Get the unsignedlongvalue of this string.toString()Decode this byte string as a Latin-1 string.Convert this byte string to a standard string.Decode this byte string as a UTF-8 string.inttryAppendTo(int offs, ByteBuffer buffer) Append as many bytes as possible to a byte buffer.voidwriteTo(OutputStream output) Append to an output stream.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
-
Method Details
-
of
Create a byte string of the given literal bytes. The given array is copied.- Parameters:
bytes- the bytes- Returns:
- the byte string
-
copyOf
Create a byte string from the given array segment.- Parameters:
b- the byte arrayoffs- the offset into the arraylen- the number of bytes to copy- Returns:
- the new byte string
-
getBytes
Get a byte string from the bytes of a character string.- Parameters:
str- the character stringcharset- the character set to use- Returns:
- the byte string
- Throws:
UnsupportedEncodingException- if the encoding is not supported
-
getBytes
Get a byte string from the bytes of a character string.- Parameters:
str- the character stringcharset- the character set to use- Returns:
- the byte string
-
getBytes
Get a byte string from the bytes of the character string. The string must be a Latin-1 string.- Parameters:
str- the character string- Returns:
- the byte string
-
getBytes
Get a byte string from all remaining bytes of a ByteBuffer.- Parameters:
buffer- the buffer- Returns:
- the byte string
-
getBytes
Get a byte string from a ByteBuffer.- Parameters:
buffer- the bufferlength- the number of bytes to get- Returns:
- the byte string
-
getBytes
public byte[] getBytes()Get a copy of the bytes of this ByteString.- Returns:
- the copy
-
getBytes
public void getBytes(byte[] dest) Deprecated.Replaced bycopyTo(byte[]).Copy the bytes of this ByteString into the destination array. If the array is too short to hold the bytes, then only enough bytes to fill the array will be copied.- Parameters:
dest- the destination array
-
getBytes
public void getBytes(byte[] dest, int offs) Deprecated.Replaced bycopyTo(byte[],int).Copy the bytes of this ByteString into the destination array. If the array is too short to hold the bytes, then only enough bytes to fill the array will be copied.- Parameters:
dest- the destination arrayoffs- the offset into the destination array
-
getBytes
public void getBytes(byte[] dest, int offs, int len) Deprecated.Replaced bycopyTo(byte[],int,int).Copy the bytes of this ByteString into the destination array. If the array is too short to hold the bytes, then only enough bytes to fill the array will be copied.- Parameters:
dest- the destination arrayoffs- the offset into the destination arraylen- the maximum number of bytes to copy
-
copyTo
public void copyTo(int srcOffs, byte[] dst, int offs, int len) Copylenbytes from this string at offsetsrcOffsto the given array at the given offset.- Parameters:
srcOffs- the source offsetdst- the destinationoffs- the destination offsetlen- the number of bytes to copy
-
copyTo
public void copyTo(byte[] dst, int offs, int len) Copylenbytes from this string to the given array at the given offset.- Parameters:
dst- the destinationoffs- the destination offsetlen- the number of bytes
-
copyTo
public void copyTo(byte[] dst, int offs) Copy all the bytes from this string to the given array at the given offset.- Parameters:
dst- the destinationoffs- the destination offset
-
copyTo
public void copyTo(byte[] dst) Copy all the bytes from this string to the given array at the given offset.- Parameters:
dst- the destination
-
appendTo
Append the bytes of this string into the given buffer.- Parameters:
dest- the target buffer
-
tryAppendTo
Append as many bytes as possible to a byte buffer.- Parameters:
offs- the start offsetbuffer- the buffer to append to- Returns:
- the number of bytes appended
-
writeTo
Append to an output stream.- Parameters:
output- the stream to write to- Throws:
IOException- if an error occurs
-
compareTo
Compare this string to another in a case-sensitive manner.- Specified by:
compareToin interfaceComparable<ByteString>- Parameters:
other- the other string- Returns:
- -1, 0, or 1
-
compareToIgnoreCase
Compare this string to another in a case-insensitive manner.- Parameters:
other- the other string- Returns:
- -1, 0, or 1
-
toString
Convert this byte string to a standard string.- Parameters:
charset- the character set to use- Returns:
- the standard string
- Throws:
UnsupportedEncodingException- if the charset is unknown
-
length
public int length()Get the number of bytes in this byte string.- Specified by:
lengthin interfaceCharSequence- Returns:
- the number of bytes
-
toString
Decode this byte string as a Latin-1 string.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject- Returns:
- the Latin-1-decoded version of this string
-
toUtf8String
Decode this byte string as a UTF-8 string.- Returns:
- the UTF-8-decoded version of this string
-
byteAt
public byte byteAt(int idx) Get the byte at an index.- Returns:
- the byte at an index
-
substring
Get the substring of this string starting at the given offset.- Parameters:
offs- the offset- Returns:
- the substring
-
substring
Get the substring of this string starting at the given offset.- Parameters:
offs- the offsetlen- the substring length- Returns:
- the substring
-
hashCode
public int hashCode()Get the hash code for this ByteString. -
hashCodeIgnoreCase
public int hashCodeIgnoreCase() -
equals
Determine if this ByteString equals another ByteString. -
equals
Determine if this ByteString equals another ByteString.- Parameters:
other- the other object- Returns:
trueif they are equal
-
equalsIgnoreCase
Determine if this ByteString equals another ByteString, ignoring case (ASCII).- Parameters:
other- the other object- Returns:
trueif they are equal
-
toInt
public int toInt(int start) Get the unsignedintvalue of this string. If the value is greater than would fit in 32 bits, only the low 32 bits are returned. Parsing stops on the first non-digit character.- Parameters:
start- the index to start at (must be less than or equal to length)- Returns:
- the value
-
toInt
public int toInt()Get the unsignedintvalue of this string. If the value is greater than would fit in 32 bits, only the low 32 bits are returned. Parsing stops on the first non-digit character.- Returns:
- the value
-
toLong
public long toLong(int start) Get the unsignedlongvalue of this string. If the value is greater than would fit in 64 bits, only the low 64 bits are returned. Parsing stops on the first non-digit character.- Parameters:
start- the index to start at (must be less than or equal to length)- Returns:
- the value
-
toLong
public long toLong()Get the unsignedlongvalue of this string. If the value is greater than would fit in 64 bits, only the low 64 bits are returned. Parsing stops on the first non-digit character.- Returns:
- the value
-
fromLong
Get a string version of the given value.- Parameters:
val- the value- Returns:
- the string
-
fromInt
Get a string version of the given value.- Parameters:
val- the value- Returns:
- the string
-
equalToString
Determine whether thisByteStringis equal (case-sensitively) to the givenString.- Parameters:
str- the string to check- Returns:
trueif the given string is equal (case-sensitively) to this instance,falseotherwise
-
equalToStringIgnoreCase
Determine whether thisByteStringis equal (case-insensitively) to the givenString.- Parameters:
str- the string to check- Returns:
trueif the given string is equal (case-insensitively) to this instance,falseotherwise
-
indexOf
public int indexOf(char c) Get the index of the given character in this string.- Parameters:
c- the character- Returns:
- the index, or -1 if it was not found
-
indexOf
public int indexOf(char c, int start) Get the index of the given character in this string.- Parameters:
c- the character- Returns:
- the index, or -1 if it was not found
-
lastIndexOf
public int lastIndexOf(char c) Get the last index of the given character in this string.- Parameters:
c- the character- Returns:
- the index, or -1 if it was not found
-
lastIndexOf
public int lastIndexOf(char c, int start) Get the last index of the given character in this string.- Parameters:
c- the character- Returns:
- the index, or -1 if it was not found
-
contains
Determine whether this string contains another string (case-sensitive).- Parameters:
other- the string to test- Returns:
trueif this string containsother,falseotherwise
-
contains
Determine whether this string contains another string (case-sensitive).- Parameters:
other- the string to test- Returns:
trueif this string containsother,falseotherwise
-
containsIgnoreCase
Determine whether this string contains another string (case-insensitive).- Parameters:
other- the string to test- Returns:
trueif this string containsother,falseotherwise
-
containsIgnoreCase
Determine whether this string contains another string (case-sensitive).- Parameters:
other- the string to test- Returns:
trueif this string containsother,falseotherwise
-
indexOf
-
indexOf
-
indexOf
-
indexOf
-
indexOfIgnoreCase
-
indexOfIgnoreCase
-
indexOfIgnoreCase
-
indexOfIgnoreCase
-
lastIndexOf
-
lastIndexOf
-
lastIndexOf
-
lastIndexOf
-
lastIndexOfIgnoreCase
-
lastIndexOfIgnoreCase
-
lastIndexOfIgnoreCase
-
lastIndexOfIgnoreCase
-
regionMatches
public boolean regionMatches(boolean ignoreCase, int offset, byte[] other, int otherOffset, int len) -
regionMatches
public boolean regionMatches(boolean ignoreCase, int offset, ByteString other, int otherOffset, int len) -
regionMatches
public boolean regionMatches(boolean ignoreCase, int offset, String other, int otherOffset, int len) -
startsWith
-
startsWith
-
startsWith
public boolean startsWith(char prefix) -
startsWithIgnoreCase
-
startsWithIgnoreCase
-
startsWithIgnoreCase
public boolean startsWithIgnoreCase(char prefix) -
endsWith
-
endsWith
-
endsWith
public boolean endsWith(char suffix) -
endsWithIgnoreCase
-
endsWithIgnoreCase
-
endsWithIgnoreCase
public boolean endsWithIgnoreCase(char suffix) -
concat
-
concat
-
concat
-
concat
-
concat
-
concat
-
concat
-
concat
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
subSequence
- Specified by:
subSequencein interfaceCharSequence
-
copyTo(byte[]).