org.norther.tammi.acorn.util.converter
Class Bytes

java.lang.Object
  extended by org.norther.tammi.acorn.util.converter.Bytes

public abstract class Bytes
extends Object

Converts between bytes and other types.

Author:
Ilkka Priha

Field Summary
static String XML_AMP
          XML reference for &.
static String XML_APOS
          XML reference for '.
static String XML_EURO
          XML reference for €.
static String XML_GT
          XML reference for >.
static String XML_LT
          XML reference for <.
static String XML_QUOT
          XML reference for ".
static String XML_XAPOS
          XML hex reference for '.
static String XML_XQUOT
          XML hex reference for ".
 
Constructor Summary
Bytes()
           
 
Method Summary
static String escape(CharSequence seq)
          Converts the specified sequence to an escaped string.
static String fromXML(Object object)
          Converts the specified XML encoded object to a text string.
static byte[] hexToBytes(String digits)
          Converts a string of hexadecimal digits into the corresponding byte array by encoding each two hexadecimal digits as a byte.
static int hexToInt(byte[] hex)
          Converts hex digits to an int.
static long hexToLong(byte[] hex)
          Converts hex digits to a long.
static short hexToShort(byte[] hex)
          Converts hex digits to a short.
static int to7Bytes(String p, byte[] bb, int off)
          Converts a 7 bit string.
static int toBytes(byte[] p, byte[] bb, int off)
          Converts a byte array.
static int toBytes(byte[] p, int s, int l, byte[] bb, int off)
          Converts a byte array.
static int toBytes(byte p, byte[] bb, int off)
          Converts a byte.
static byte[] toBytes(char[] p, int mask)
          Converts a char array to a null terminated byte array.
static int toBytes(char[] p, int mask, byte[] bb, int off)
          Converts a char array up to eight bits.
static int toBytes(char p, int mask, byte[] bb, int off)
          Converts a char up to eight bits.
static int toBytes(double[] p, byte[] bb, int off)
          Converts a double array.
static int toBytes(double p, byte[] bb, int off)
          Converts a double according to IEEE 754.
static int toBytes(float[] p, byte[] bb, int off)
          Converts a float array.
static int toBytes(float p, byte[] bb, int off)
          Converts a float according to IEEE 754.
static int toBytes(int[] p, byte[] bb, int off)
          Converts an int array.
static int toBytes(int p, byte[] bb, int off)
          Converts an int.
static int toBytes(long[] p, byte[] bb, int off)
          Converts a long array.
static int toBytes(long p, byte[] bb, int off)
          Converts a long.
static int toBytes(short[] p, byte[] bb, int off)
          Converts a short array.
static int toBytes(short p, byte[] bb, int off)
          Converts a short.
static byte[] toBytes(String p)
          Converts an 8 bit string to a null terminated byte array.
static int toBytes(String p, byte[] bb, int off)
          Converts an 8 bit string.
static double toDouble(byte[] bb, int off)
          Converts eight bytes into a double according to IEEE 754.
static float toFloat(byte[] bb, int off)
          Converts four bytes into a float according to IEEE 754.
static String toHex(byte b)
          Converts a byte into a hex string of two chars.
static String toHex(byte[] bytes)
          Converts a byte array into a printable format containing a string of hexadecimal digit characters (two per byte).
static String toHex(byte[] bytes, int off, int len)
          Converts a part of a byte array into a printable format containing a string of hexadecimal digit characters (two per byte).
static String toHex(char c)
          Converts a char into a hex string of four chars.
static int toInt(byte[] bb, int off)
          Converts four bytes into an int (BE).
static int toInt(byte[] bb, int off, int len)
          Converts a number of bytes into an int (BE).
static int toIntBE(byte[] bb, int off)
          Converts four bytes into an int (BE).
static int toIntBE(byte[] bb, int off, int len)
          Converts a number of bytes into an int (BE).
static int toIntLE(byte[] bb, int off)
          Converts four bytes into an int (LE).
static int toIntLE(byte[] bb, int off, int len)
          Converts a number of bytes into an int (LE).
static long toLong(byte[] bb, int off)
          Converts eight bytes into a long (LE).
static long toLongBE(byte[] bb, int off)
          Converts eight bytes into a long (BE).
static long toLongLE(byte[] bb, int off)
          Converts eight bytes into a long (LE).
static String toName(Object object)
          Converts the specified string to a valid id or name attribute.
static short toShort(byte[] bb, int off)
          Converts two bytes into a short (BE).
static short toShortBE(byte[] bb, int off)
          Converts two bytes into a short (BE).
static short toShortLE(byte[] bb, int off)
          Converts two bytes into a short (LE).
static String toString(byte[] bb)
          Converts bytes to string by applying platform's default encoding.
static String toString(byte[] bb, String encoding)
          Converts bytes to string by applying the given encoding.
static String toXML(Object object)
          Converts the specified object to an XML encoded string.
static String unescape(CharSequence seq)
          Converts the specified sequence to an unescaped string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_LT

public static final String XML_LT
XML reference for <.

See Also:
Constant Field Values

XML_GT

public static final String XML_GT
XML reference for >.

See Also:
Constant Field Values

XML_AMP

public static final String XML_AMP
XML reference for &.

See Also:
Constant Field Values

XML_QUOT

public static final String XML_QUOT
XML reference for ".

See Also:
Constant Field Values

XML_XQUOT

public static final String XML_XQUOT
XML hex reference for ".

See Also:
Constant Field Values

XML_APOS

public static final String XML_APOS
XML reference for '.

See Also:
Constant Field Values

XML_XAPOS

public static final String XML_XAPOS
XML hex reference for '.

See Also:
Constant Field Values

XML_EURO

public static final String XML_EURO
XML reference for €.

See Also:
Constant Field Values
Constructor Detail

Bytes

public Bytes()
Method Detail

toBytes

public static final int toBytes(byte p,
                                byte[] bb,
                                int off)
Converts a byte.

Parameters:
p - the byte.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(short p,
                                byte[] bb,
                                int off)
Converts a short.

Parameters:
p - the short.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(int p,
                                byte[] bb,
                                int off)
Converts an int.

Parameters:
p - the int.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(long p,
                                byte[] bb,
                                int off)
Converts a long.

Parameters:
p - the long.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(float p,
                                byte[] bb,
                                int off)
Converts a float according to IEEE 754.

Parameters:
p - the float.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(double p,
                                byte[] bb,
                                int off)
Converts a double according to IEEE 754.

Parameters:
p - the double.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(byte[] p,
                                byte[] bb,
                                int off)
Converts a byte array.

Parameters:
p - the byte array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(byte[] p,
                                int s,
                                int l,
                                byte[] bb,
                                int off)
Converts a byte array.

Parameters:
p - the byte array.
s - the start offset.
l - the length to copy.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(short[] p,
                                byte[] bb,
                                int off)
Converts a short array.

Parameters:
p - the short array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(int[] p,
                                byte[] bb,
                                int off)
Converts an int array.

Parameters:
p - the int array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(long[] p,
                                byte[] bb,
                                int off)
Converts a long array.

Parameters:
p - the long array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(float[] p,
                                byte[] bb,
                                int off)
Converts a float array.

Parameters:
p - the float array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(double[] p,
                                byte[] bb,
                                int off)
Converts a double array.

Parameters:
p - the double array.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(char p,
                                int mask,
                                byte[] bb,
                                int off)
Converts a char up to eight bits.

Parameters:
p - the char.
mask - a byte mask.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final int toBytes(char[] p,
                                int mask,
                                byte[] bb,
                                int off)
Converts a char array up to eight bits.

Parameters:
p - the char array.
mask - a byte mask.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final byte[] toBytes(char[] p,
                                   int mask)
Converts a char array to a null terminated byte array.

Parameters:
p - the char array.
mask - a byte mask.
Returns:
the converted bytes.

toBytes

public static final int toBytes(String p,
                                byte[] bb,
                                int off)
Converts an 8 bit string.

Parameters:
p - the string.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.

toBytes

public static final byte[] toBytes(String p)
Converts an 8 bit string to a null terminated byte array.

Parameters:
p - the string.
Returns:
the converted bytes.

to7Bytes

public static final int to7Bytes(String p,
                                 byte[] bb,
                                 int off)
Converts a 7 bit string.

Parameters:
p - the string.
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the incremented offset.
Throws:
IllegalArgumentException - if encoding not supported.

toString

public static final String toString(byte[] bb)
Converts bytes to string by applying platform's default encoding.

Parameters:
bb - the byte buffer.
Returns:
the converted string.

toString

public static final String toString(byte[] bb,
                                    String encoding)
Converts bytes to string by applying the given encoding. The default encoding is used if the specified one fails.

Parameters:
bb - the byte buffer.
encoding - the encoding.
Returns:
the converted string.

toShort

public static final short toShort(byte[] bb,
                                  int off)
Converts two bytes into a short (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the short.

toShortBE

public static final short toShortBE(byte[] bb,
                                    int off)
Converts two bytes into a short (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the short.

toShortLE

public static final short toShortLE(byte[] bb,
                                    int off)
Converts two bytes into a short (LE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the short.

toInt

public static final int toInt(byte[] bb,
                              int off)
Converts four bytes into an int (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the int.

toIntBE

public static final int toIntBE(byte[] bb,
                                int off)
Converts four bytes into an int (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the int.

toIntLE

public static final int toIntLE(byte[] bb,
                                int off)
Converts four bytes into an int (LE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the int.

toLong

public static final long toLong(byte[] bb,
                                int off)
Converts eight bytes into a long (LE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the long.

toLongBE

public static final long toLongBE(byte[] bb,
                                  int off)
Converts eight bytes into a long (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the long.

toLongLE

public static final long toLongLE(byte[] bb,
                                  int off)
Converts eight bytes into a long (LE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the long.

toInt

public static final int toInt(byte[] bb,
                              int off,
                              int len)
Converts a number of bytes into an int (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
len - the number of bytes.
Returns:
the int.

toIntBE

public static final int toIntBE(byte[] bb,
                                int off,
                                int len)
Converts a number of bytes into an int (BE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
len - the number of bytes.
Returns:
the int.

toIntLE

public static final int toIntLE(byte[] bb,
                                int off,
                                int len)
Converts a number of bytes into an int (LE).

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
len - the number of bytes.
Returns:
the int.

toFloat

public static final float toFloat(byte[] bb,
                                  int off)
Converts four bytes into a float according to IEEE 754.

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the float.

toDouble

public static final double toDouble(byte[] bb,
                                    int off)
Converts eight bytes into a double according to IEEE 754.

Parameters:
bb - the byte buffer.
off - the offset in the buffer.
Returns:
the double.

toHex

public static final String toHex(byte b)
Converts a byte into a hex string of two chars.

Parameters:
b - the byte.
Returns:
the hex string.

toHex

public static final String toHex(char c)
Converts a char into a hex string of four chars.

Parameters:
c - the char.
Returns:
the hex string.

toHex

public static String toHex(byte[] bytes)
Converts a byte array into a printable format containing a string of hexadecimal digit characters (two per byte).

Parameters:
bytes - a byte array representation.
Returns:
a string.

toHex

public static String toHex(byte[] bytes,
                           int off,
                           int len)
Converts a part of a byte array into a printable format containing a string of hexadecimal digit characters (two per byte).

Parameters:
bytes - a byte array representation.
off - an offset from which to start the conversion.
len - the number of bytes to convert.
Returns:
a string.

hexToBytes

public static byte[] hexToBytes(String digits)
Converts a string of hexadecimal digits into the corresponding byte array by encoding each two hexadecimal digits as a byte.

Parameters:
digits - hexadecimal digits representation.
Returns:
a byte array.
Throws:
IllegalArgumentException - for invalid hexadecimal digits.

hexToShort

public static short hexToShort(byte[] hex)
Converts hex digits to a short.

Parameters:
hex - a byte array containing hexadecimal digits.
Returns:
the corresponding short.
Throws:
IllegalArgumentException - for invalid hexadecimal digits.

hexToInt

public static int hexToInt(byte[] hex)
Converts hex digits to an int.

Parameters:
hex - a byte array containing hexadecimal digits.
Returns:
the corresponding int.
Throws:
IllegalArgumentException - for invalid hexadecimal digits.

hexToLong

public static long hexToLong(byte[] hex)
Converts hex digits to a long.

Parameters:
hex - a byte array containing hexadecimal digits.
Returns:
the corresponding long.
Throws:
IllegalArgumentException - for invalid hexadecimal digits.

toXML

public static String toXML(Object object)
Converts the specified object to an XML encoded string.

Parameters:
object - the object to convert.
Returns:
the XML encoded string.

fromXML

public static String fromXML(Object object)
Converts the specified XML encoded object to a text string.

Parameters:
object - the object to convert.
Returns:
the text string.

toName

public static String toName(Object object)
Converts the specified string to a valid id or name attribute.

Parameters:
object - the object to convert.
Returns:
the valid id or name attribute.

escape

public static String escape(CharSequence seq)
Converts the specified sequence to an escaped string.

Parameters:
seq - the sequence to convert.
Returns:
the escaped string.

unescape

public static String unescape(CharSequence seq)
Converts the specified sequence to an unescaped string.

Parameters:
seq - the sequence to convert.
Returns:
the unescaped string.


Copyright © 2004 The Norther Organization. All rights reserved.