SQLite
Class StringEncoder

java.lang.Object
  extended bySQLite.StringEncoder

public class StringEncoder
extends Object

String encoder/decoder for SQLite. This module was kindly donated by Eric van der Maarel of Nedap N.V. This encoder was implemented based on an original idea from an anonymous author in the source code of the SQLite distribution. I feel obliged to provide a quote from the original C-source code: "The author disclaims copyright to this source code. In place of a legal notice, here is a blessing: May you do good and not evil. May you find forgiveness for yourself and forgive others. May you share freely, never taking more than you give."


Field Summary
(package private) static char[] xdigits
           
 
Constructor Summary
StringEncoder()
           
 
Method Summary
private static byte[] byteCopy(byte[] source, int offset, int count, byte[] target)
          Copies count elements from source, starting at element with index offset, to the given target.
static byte[] decode(String s)
          Decodes the given string that is assumed to be a valid encoding of a byte array.
static String encode(byte[] a)
          Encodes the given byte array into a string that can be used by the SQLite database.
static String encodeX(byte[] a)
          Encodes the given byte array into SQLite3 blob notation, ie X'..'
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xdigits

static final char[] xdigits
Constructor Detail

StringEncoder

public StringEncoder()
Method Detail

byteCopy

private static byte[] byteCopy(byte[] source,
                               int offset,
                               int count,
                               byte[] target)
Copies count elements from source, starting at element with index offset, to the given target.

Parameters:
source - the source.
offset - the offset.
count - the number of elements to be copied.
target - the target to be returned.
Returns:
the target being copied to.

decode

public static byte[] decode(String s)
Decodes the given string that is assumed to be a valid encoding of a byte array. Typically the given string is generated by this class' encode method.

Parameters:
s - the given string encoding.
Returns:
the byte array obtained from the decoding.
Throws:
IllegalArgumentException - when the string given is not a valid encoded string for this encoder.

encode

public static String encode(byte[] a)
Encodes the given byte array into a string that can be used by the SQLite database. The database cannot handle null (0x00) and the character '\'' (0x27). The encoding consists of escaping these characters with a reserved character (0x01). The escaping is applied after determining and applying a shift that minimizes the number of escapes required. With this encoding the data of original size n is increased to a maximum of 1+(n*257)/254. For sufficiently large n the overhead is thus less than 1.2%.

Parameters:
a - the byte array to be encoded. A null reference is handled as an empty array.
Returns:
the encoded bytes as a string. When an empty array is provided a string of length 1 is returned, the value of which is bogus. When decoded with this class' decode method a string of size 1 will return an empty byte array.

encodeX

public static String encodeX(byte[] a)
Encodes the given byte array into SQLite3 blob notation, ie X'..'

Parameters:
a - the byte array to be encoded. A null reference is handled as an empty array.
Returns:
the encoded bytes as a string.


Contact: Christian Werner