- java.lang.Object
-
- org.hsqldb.lib.StringConverter
-
public class StringConverter extends java.lang.Object
Collection of static methods for converting strings between different formats and to and from byte arrays.Includes two methods based on Hypersonic code as indicated.
- Since:
- 1.7.2
- Author:
- Thomas Mueller (Hypersonic SQL Group), Fred Toussi (fredt@users dot sourceforge.net)
-
-
Constructor Summary
Constructors Constructor Description StringConverter()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
byteArrayToBitString(byte[] bytes, int bitCount)
Converts a byte array into a bit stringstatic java.lang.String
byteArrayToHexString(byte[] b)
Converts a byte array into a hexadecimal stringstatic java.lang.String
byteArrayToSQLBitString(byte[] bytes, int bitCount)
Converts a byte array into an SQL binary stringstatic java.lang.String
byteArrayToSQLHexString(byte[] b)
Converts a byte array into an SQL hexadecimal stringstatic java.lang.String
byteArrayToString(byte[] b, java.lang.String charset)
static int
getUTFSize(java.lang.String s)
static byte[]
hexStringToByteArray(java.lang.String s)
Converts a hexadecimal string into a byte arraystatic java.lang.String
inputStreamToString(java.io.InputStream is, java.lang.String encoding)
Using an output stream, returns a String from an InputStream.static java.lang.String
readUTF(byte[] bytearr, int offset, int length)
static java.lang.String
readUTF(byte[] bytearr, int offset, int length, char[] buf)
static org.hsqldb.map.BitMap
sqlBitStringToBitMap(java.lang.String s)
Compacts a bit string into a BitMapstatic void
stringToHtmlBytes(HsqlByteArrayOutputStream b, java.lang.String s)
Converts the string to an HTML representation in the ASCII character set and appends it to a byte array output stream.static void
stringToUnicodeBytes(HsqlByteArrayOutputStream b, java.lang.String s, boolean doubleSingleQuotes)
Hsqldb specific encoding used only for log files.static java.lang.String
stringToUnicodeEscaped(java.lang.String s)
static int
stringToUTFBytes(java.lang.String str, HsqlByteArrayOutputStream out)
Writes a string to the specified DataOutput using UTF-8 encoding in a machine-independent manner.static byte[]
toBinaryUUID(java.lang.String s)
Returns a byte[] representation in UUID form from a UUID string.static void
toJSONString(java.lang.String s, java.lang.StringBuilder sb)
static java.lang.String
toQuotedString(java.lang.String s, char quoteChar, boolean extraQuote)
Returns the quoted version of the string using the quotechar argument.static java.lang.String
toStringUUID(byte[] b)
Returns a string representation in UUID form from a binary string.static java.lang.String
unicodeEscapedToString(java.lang.String s)
static java.lang.String
unicodeStringToString(java.lang.String s)
Hsqldb specific decoding used only for log files.static int
writeHexBytes(byte[] o, int from, byte[] b)
Converts a byte array into hexadecimal characters which are written as ASCII to the given output stream.static int
writeUUIDHexBytes(byte[] o, int from, byte[] b)
-
-
-
Method Detail
-
hexStringToByteArray
public static byte[] hexStringToByteArray(java.lang.String s) throws java.io.IOException
Converts a hexadecimal string into a byte array- Parameters:
s
- hexadecimal string- Returns:
- byte array for the hex string
- Throws:
java.io.IOException
- on error
-
sqlBitStringToBitMap
public static org.hsqldb.map.BitMap sqlBitStringToBitMap(java.lang.String s) throws java.io.IOException
Compacts a bit string into a BitMap- Parameters:
s
- bit string- Returns:
- BitMap for the bit string
- Throws:
java.io.IOException
- on error
-
byteArrayToHexString
public static java.lang.String byteArrayToHexString(byte[] b)
Converts a byte array into a hexadecimal string- Parameters:
b
- byte array- Returns:
- hex string
-
byteArrayToSQLHexString
public static java.lang.String byteArrayToSQLHexString(byte[] b)
Converts a byte array into an SQL hexadecimal string- Parameters:
b
- byte array- Returns:
- hex string
-
byteArrayToBitString
public static java.lang.String byteArrayToBitString(byte[] bytes, int bitCount)
Converts a byte array into a bit string- Parameters:
bytes
- byte arraybitCount
- number of bits- Returns:
- hex string
-
byteArrayToSQLBitString
public static java.lang.String byteArrayToSQLBitString(byte[] bytes, int bitCount)
Converts a byte array into an SQL binary string- Parameters:
bytes
- byte arraybitCount
- number of bits- Returns:
- hex string
-
writeHexBytes
public static int writeHexBytes(byte[] o, int from, byte[] b)
Converts a byte array into hexadecimal characters which are written as ASCII to the given output stream.- Parameters:
o
- output arrayfrom
- offset into output arrayb
- input array- Returns:
- written count
-
byteArrayToString
public static java.lang.String byteArrayToString(byte[] b, java.lang.String charset)
-
stringToUnicodeBytes
public static void stringToUnicodeBytes(HsqlByteArrayOutputStream b, java.lang.String s, boolean doubleSingleQuotes)
Hsqldb specific encoding used only for log files. The SQL statements that need to be written to the log file (input) are Java Unicode strings. input is converted into a 7bit escaped ASCII string (output)with the following transformations. All characters outside the 0x20-7f range are converted to an escape sequence and added to output. If a backslash character is immediately followed by 'u', the backslash character is converted to escape sequence and added to output. All the remaining characters in input are added to output without conversion. The escape sequence is backslash, letter u, xxxx, where xxxx is the hex representation of the character code. (fredt@users)Method based on Hypersonic Code
- Parameters:
b
- output stream to wite tos
- Java stringdoubleSingleQuotes
- boolean
-
unicodeStringToString
public static java.lang.String unicodeStringToString(java.lang.String s)
Hsqldb specific decoding used only for log files. This method converts the 7 bit escaped ASCII strings in a log file back into Java Unicode strings. See stringToUnicodeBytes() above.Method based on Hypersonic Code
- Parameters:
s
- encoded ASCII string in byte array- Returns:
- Java string
-
readUTF
public static java.lang.String readUTF(byte[] bytearr, int offset, int length) throws java.io.IOException
- Throws:
java.io.IOException
-
readUTF
public static java.lang.String readUTF(byte[] bytearr, int offset, int length, char[] buf) throws java.io.IOException
- Throws:
java.io.IOException
-
stringToUTFBytes
public static int stringToUTFBytes(java.lang.String str, HsqlByteArrayOutputStream out)
Writes a string to the specified DataOutput using UTF-8 encoding in a machine-independent manner.- Parameters:
str
- a string to be written.out
- destination to write to- Returns:
- The number of bytes written out.
-
getUTFSize
public static int getUTFSize(java.lang.String s)
-
inputStreamToString
public static java.lang.String inputStreamToString(java.io.InputStream is, java.lang.String encoding) throws java.io.IOException
Using an output stream, returns a String from an InputStream.- Parameters:
is
- InputStream to read fromencoding
- character encoding of the string- Returns:
- a Java string
- Throws:
java.io.IOException
- on error
-
toQuotedString
public static java.lang.String toQuotedString(java.lang.String s, char quoteChar, boolean extraQuote)
Returns the quoted version of the string using the quotechar argument. doublequote argument indicates whether each instance of quotechar inside the string is doubled.null string argument returns null. If the caller needs the literal "NULL" it should be created it itself
- Parameters:
s
- Java stringquoteChar
- character used for quotingextraQuote
- true if quoteChar itself should be repeated- Returns:
- String
-
stringToHtmlBytes
public static void stringToHtmlBytes(HsqlByteArrayOutputStream b, java.lang.String s)
Converts the string to an HTML representation in the ASCII character set and appends it to a byte array output stream.- Parameters:
b
- the output byte array output streams
- the input string
-
toStringUUID
public static java.lang.String toStringUUID(byte[] b)
Returns a string representation in UUID form from a binary string. UUID string is composed of 8-4-4-4-12 hexadecimal characters.- Parameters:
b
- the byte array- Returns:
- UUID string form
-
writeUUIDHexBytes
public static int writeUUIDHexBytes(byte[] o, int from, byte[] b)
-
toBinaryUUID
public static byte[] toBinaryUUID(java.lang.String s)
Returns a byte[] representation in UUID form from a UUID string.- Parameters:
s
- the UUID string- Returns:
- byte array
-
stringToUnicodeEscaped
public static java.lang.String stringToUnicodeEscaped(java.lang.String s)
-
unicodeEscapedToString
public static java.lang.String unicodeEscapedToString(java.lang.String s) throws java.io.IOException
- Throws:
java.io.IOException
-
toJSONString
public static void toJSONString(java.lang.String s, java.lang.StringBuilder sb)
-
-