org.crosswire.jsword.book.sword
Class SwordUtil

java.lang.Object
  extended by org.crosswire.jsword.book.sword.SwordUtil

public final class SwordUtil
extends Object

Various utilities used by different Sword classes.

Author:
Joe Walker [joe at eireneh dot com]
See Also:
for license details. The copyright to this program is held by it's authors.

Field Summary
private static Logger log
          The log stream
 
Constructor Summary
private SwordUtil()
          Prevent Instansiation
 
Method Summary
static void clean1252(Key key, byte[] data)
          Remove rogue characters in the source.
static void clean1252(Key key, byte[] data, int length)
          Remove rogue characters in the source.
static String decode(Key key, byte[] data, int length, String charset)
          Transform a byte array into a string given the encoding.
static String decode(Key key, byte[] data, String charset)
          Transform a byte array into a string given the encoding.
protected static int decodeLittleEndian16(byte[] data, int offset)
          Decode little endian data from a byte array
protected static int decodeLittleEndian32(byte[] data, int offset)
          Decode little endian data from a byte array.
protected static int findByte(byte[] data, byte sought)
          Find a byte of data in an array
protected static byte[] readNextRAF(RandomAccessFile raf, int theSize)
          Read a RandomAccessFile from the current location in the file.
protected static byte[] readRAF(RandomAccessFile raf, long offset, int theSize)
          Read a RandomAccessFile
protected static byte[] readUntilRAF(RandomAccessFile raf, byte stopByte)
          Read a RandomAccessFile until a particular byte is seen
protected static byte[] readUntilRAF(RandomAccessFile raf, int offset, byte stopByte)
          Read a RandomAccessFile until a particular byte is seen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final Logger log
The log stream

Constructor Detail

SwordUtil

private SwordUtil()
Prevent Instansiation

Method Detail

readRAF

protected static byte[] readRAF(RandomAccessFile raf,
                                long offset,
                                int theSize)
                         throws IOException
Read a RandomAccessFile

Parameters:
raf - The file to read
offset - The start of the record to read
theSize - The number of bytes to read
Returns:
the read data
Throws:
IOException

readNextRAF

protected static byte[] readNextRAF(RandomAccessFile raf,
                                    int theSize)
                             throws IOException
Read a RandomAccessFile from the current location in the file.

Parameters:
raf - The file to read
theSize - The number of bytes to read
Returns:
the read data
Throws:
IOException

readUntilRAF

protected static byte[] readUntilRAF(RandomAccessFile raf,
                                     int offset,
                                     byte stopByte)
                              throws IOException
Read a RandomAccessFile until a particular byte is seen

Parameters:
raf - The file to read
offset - The start of the record to read
stopByte - The point at which to stop reading
Returns:
the read data
Throws:
IOException

readUntilRAF

protected static byte[] readUntilRAF(RandomAccessFile raf,
                                     byte stopByte)
                              throws IOException
Read a RandomAccessFile until a particular byte is seen

Parameters:
raf - The file to read
stopByte - The point at which to stop reading
Returns:
the read data
Throws:
IOException

decodeLittleEndian32

protected static int decodeLittleEndian32(byte[] data,
                                          int offset)
Decode little endian data from a byte array. This assumes that the high order bit is not set as this is used solely for an offset in a file in bytes. For a practical limit, 2**31 is way bigger than any document that we can have.

Parameters:
data - the byte[] from which to read 4 bytes
offset - the offset into the array
Returns:
The decoded data

decodeLittleEndian16

protected static int decodeLittleEndian16(byte[] data,
                                          int offset)
Decode little endian data from a byte array

Parameters:
data - the byte[] from which to read 4 bytes
offset - the offset into the array
Returns:
The decoded data

findByte

protected static int findByte(byte[] data,
                              byte sought)
Find a byte of data in an array

Parameters:
data - The array to search
sought - The data to search for
Returns:
The index of the found position or -1 if not found

decode

public static String decode(Key key,
                            byte[] data,
                            String charset)
Transform a byte array into a string given the encoding. If the encoding is bad then it just does it as a string.

Parameters:
data - The byte array to be converted
charset - The encoding of the byte array
Returns:
a string that is UTF-8 internally

decode

public static String decode(Key key,
                            byte[] data,
                            int length,
                            String charset)
Transform a byte array into a string given the encoding. If the encoding is bad then it just does it as a string.

Parameters:
data - The byte array to be converted
charset - The encoding of the byte array
Returns:
a string that is UTF-8 internally

clean1252

public static void clean1252(Key key,
                             byte[] data)
Remove rogue characters in the source. These are characters that are not valid in cp1252 aka WINDOWS-1252 and in UTF-8 or are non-printing control characters in the range of 0-32.


clean1252

public static void clean1252(Key key,
                             byte[] data,
                             int length)
Remove rogue characters in the source. These are characters that are not valid in cp1252 aka WINDOWS-1252 and in UTF-8 or are non-printing control characters in the range of 0-32.


Copyright ยจ 2003-2007