|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.buildforge.services.common.text.StringConverter
public class StringConverter
This class performs translation to and from UTF-8 or ISO-8859-1 encoding, as directed. The ISO-8859-1 conversion is done directly because it is such a simple conversion to perform and because that avoids the overhead of using the library conversion routines, which often involve unnecessary creation of wrapper objects. The conversion to ISO-8859-1 (Latin-1) does not check for invalid input characters. If there are any, their high-order bits are silently discarded.
The conversion routines use cached CharBuffer and ByteBuffer objects whenever it is possible to do so. These cached objects should be large enough for the vast majority of conversion requests, but when they are not, a temporary buffer is allocated instead.
As the cached buffer is reused whenever possible, the caller should
make immediate use of the return value. Old buffer contents should
not be trusted once another conversion has been requested. For
efficiency reasons, this class requires thread confinement for
correct behavior. Threads should always obtain their own reference
to a string converter using get()
, and such objects should
never be shared with other threads.
Field Summary | |
---|---|
static int |
BYTE_BUFFER_SIZE
Size of the char buffer we keep around for short pieces of data. |
static int |
CHAR_BUFFER_SIZE
Size of the char buffer we keep around for short pieces of data. |
Constructor Summary | |
---|---|
StringConverter()
|
Method Summary | |
---|---|
java.lang.String |
fromLatin1(byte[] bary)
Converts a sequence of ISO-8859-1 (Latin-1) bytes into a string. |
java.lang.String |
fromLatin1(byte[] bary,
int offset,
int len)
Converts a sequence of ISO-8859-1 (Latin-1) bytes into a string. |
java.lang.String |
fromLatin1(java.nio.ByteBuffer bbuf)
Converts a sequence of ISO-8859-1 (Latin-1) bytes into a string. |
java.lang.String |
fromLatin1(java.nio.ByteBuffer bbuf,
int len)
Converts a sequence of ISO-8859-1 (Latin-1) bytes into a string. |
java.lang.String |
fromUtf8(byte[] bary)
Converts a sequence of UTF-8 bytes into a string. |
java.lang.String |
fromUtf8(byte[] bary,
int offset,
int len)
Converts a sequence of UTF-8 bytes into a string. |
java.lang.String |
fromUtf8(java.nio.ByteBuffer bbuf)
Converts a sequence of UTF-8 bytes into a string. |
java.lang.String |
fromUtf8(java.nio.ByteBuffer bbuf,
int len)
Converts a sequence of UTF-8 bytes into a string. |
static StringConverter |
get()
Factory method for getting a thread-local converter. |
java.nio.ByteBuffer |
toLatin1(java.lang.String s)
Converts a string into a sequence of ISO-8859-1 (Latin-1) bytes. |
byte[] |
toLatin1Bytes(java.lang.String s)
Converts a string into a sequence of ISO-8859-1 (Latin-1) bytes. |
java.nio.ByteBuffer |
toUtf8(java.lang.String s)
Converts a string into a sequence of UTF-8 bytes. |
byte[] |
toUtf8Bytes(java.lang.String s)
Converts a string into a sequence of UTF-8 bytes. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int CHAR_BUFFER_SIZE
public static final int BYTE_BUFFER_SIZE
CHAR_BUFFER_SIZE
characters, as 1:4 is the worst-case
expansion of characters to bytes.
Constructor Detail |
---|
public StringConverter()
Method Detail |
---|
public java.lang.String fromLatin1(byte[] bary)
bary
- the byte array to convert into a string
java.lang.NullPointerException
- if bary is nullpublic java.lang.String fromLatin1(byte[] bary, int offset, int len)
bary
- the byte array to convert into a stringoffset
- the offset into the array of the bytes to translatelen
- the length of the data to translate
java.lang.NullPointerException
- if bary is null and
len is positive
java.lang.NegativeArraySizeException
- if len is negative
java.lang.IndexOutOfBoundsException
- if the offset and
len values violate the bounds of the arraypublic java.lang.String fromLatin1(java.nio.ByteBuffer bbuf)
bbuf
- the byte buffer to convert into a string
java.lang.NullPointerException
- if bbuf is nullpublic java.lang.String fromLatin1(java.nio.ByteBuffer bbuf, int len)
bbuf
- the byte buffer to convert into a stringlen
- the length of the data to translate
java.lang.NullPointerException
- if bbuf is null
java.lang.NegativeArraySizeException
- if the len value
is negative
java.nio.BufferUnderflowException
- if the specified len
would underflow the bufferpublic java.nio.ByteBuffer toLatin1(java.lang.String s)
s
- the string to convert
java.lang.NullPointerException
- if s is nullpublic byte[] toLatin1Bytes(java.lang.String s)
s
- the string to convert
java.lang.NullPointerException
- if s is nullpublic java.lang.String fromUtf8(byte[] bary)
bary
- the array to decode
public java.lang.String fromUtf8(byte[] bary, int offset, int len)
bary
- the array to decodeoffset
- the starting index within the arraylen
- the number of bytes to decode
public java.lang.String fromUtf8(java.nio.ByteBuffer bbuf, int len)
bbuf
- the byte buffer to convert into a stringlen
- the length of the data to translate
java.lang.NullPointerException
- if bbuf is null
java.lang.NegativeArraySizeException
- if the len value
is negative
java.nio.BufferUnderflowException
- if the specified len
would underflow the bufferpublic java.lang.String fromUtf8(java.nio.ByteBuffer bbuf)
bbuf
- the byte buffer to convert into a string
java.lang.NullPointerException
- if bbuf is nullpublic java.nio.ByteBuffer toUtf8(java.lang.String s)
s
- the string to encode
public byte[] toUtf8Bytes(java.lang.String s)
s
- the string to encode
public static StringConverter get()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |