net.i2p.data
Class DataHelper

java.lang.Object
  extended by net.i2p.data.DataHelper

public class DataHelper
extends java.lang.Object

Defines some simple IO routines for dealing with marshalling data structures

Author:
jrandom

Field Summary
static byte BOOLEAN_FALSE
           
static int BOOLEAN_LENGTH
           
static byte BOOLEAN_TRUE
           
static byte BOOLEAN_UNKNOWN
           
static int DATE_LENGTH
           
 
Constructor Summary
DataHelper()
           
 
Method Summary
static int compareTo(byte[] lhs, byte[] rhs)
           
static byte[] compress(byte[] orig)
          compress the data and return a new GZIP compressed array
static byte[] compress(byte[] orig, int offset, int size)
           
static byte[] decompress(byte[] orig)
          decompress the GZIP compressed data (returning null on error)
static byte[] decompress(byte[] orig, int offset, int length)
           
static boolean eq(byte[] lhs, byte[] rhs)
          Run a comparison on the byte arrays, byte by byte.
static boolean eq(byte[] lhs, int offsetLeft, byte[] rhs, int offsetRight, int length)
           
static boolean eq(byte lhs, byte rhs)
          Compare two bytes, really just for consistency.
static boolean eq(java.util.Collection lhs, java.util.Collection rhs)
          Run a deep comparison across the two collections.
static boolean eq(int lhs, int rhs)
          Compare two integers, really just for consistency.
static boolean eq(long lhs, long rhs)
          Compare two longs, really just for consistency.
static boolean eq(java.lang.Object lhs, java.lang.Object rhs)
          Helper util to compare two objects, including null handling.
static java.lang.String formatDuration(long ms)
           
static java.lang.Boolean fromBoolean(byte[] data, int offset)
           
static java.util.Date fromDate(byte[] src, int offset)
           
static byte[] fromHexString(java.lang.String val)
           
static long fromLong(byte[] src, int offset, int numBytes)
           
static int fromProperties(byte[] source, int offset, java.util.Properties target)
           
static java.lang.String getUTF8(byte[] orig)
           
static java.lang.String getUTF8(byte[] orig, int offset, int len)
           
static byte[] getUTF8(java.lang.String orig)
           
static byte[] getUTF8(java.lang.StringBuffer orig)
           
static int hashCode(byte[] b)
          Calculate the hashcode of the byte array, using 0 for null
static int hashCode(java.util.Collection col)
          Calculate the hashcode of the collection, using 0 for null
static int hashCode(java.util.Date obj)
          Calculate the hashcode of the date, using 0 for null
static int hashCode(java.lang.Object obj)
          Calculate the hashcode of the object, using 0 for null
static void loadProps(java.util.Properties props, java.io.File file)
          A more efficient Properties.load
static void loadProps(java.util.Properties props, java.io.File file, boolean forceLowerCase)
           
static void loadProps(java.util.Properties props, java.io.InputStream inStr)
           
static void loadProps(java.util.Properties props, java.io.InputStream inStr, boolean forceLowerCase)
           
static int read(java.io.InputStream in, byte[] target)
           
static int read(java.io.InputStream in, byte[] target, int offset, int length)
           
static java.lang.Boolean readBoolean(java.io.InputStream in)
          Read in a boolean as specified by the I2P data structure spec.
static java.util.Date readDate(java.io.InputStream in)
          Read in a date from the stream as specified by the I2P data structure spec.
static java.lang.String readLine(java.io.InputStream in)
          Read a newline delimited line from the stream, returning the line (without the newline), or null if EOF reached before the newline was found
static java.lang.String readLine(java.io.InputStream in, Sha256Standalone hash)
          update the hash along the way
static boolean readLine(java.io.InputStream in, java.lang.StringBuffer buf)
          Read in a line, placing it into the buffer (excluding the newline).
static boolean readLine(java.io.InputStream in, java.lang.StringBuffer buf, Sha256Standalone hash)
          update the hash along the way
static long readLong(java.io.InputStream rawStream, int numBytes)
          Read the stream for an integer as defined by the I2P data structure specification.
static java.util.Properties readProperties(java.io.InputStream rawStream)
          Read a mapping from the stream, as defined by the I2P data structure spec, and store it into a Properties object.
static java.lang.String readString(java.io.InputStream in)
          Read in a string from the stream as specified by the I2P data structure spec.
static java.util.List sortStructures(java.util.Collection dataStructures)
           
static void storeProps(java.util.Properties props, java.io.File file)
           
static java.lang.String stripHTML(java.lang.String orig)
          Strip out any HTML (simply removing any less than / greater than symbols)
static void toBoolean(byte[] data, int offset, boolean value)
           
static void toBoolean(byte[] data, int offset, java.lang.Boolean value)
           
static void toDate(byte[] target, int offset, long when)
           
static byte[] toDate(java.util.Date date)
           
static java.lang.String toDecimalString(byte[] buf, int len)
           
static java.lang.String toHexString(byte[] data)
           
static void toLong(byte[] target, int offset, int numBytes, long value)
           
static byte[] toLong(int numBytes, long value)
           
static int toProperties(byte[] target, int offset, java.util.Properties props)
           
static byte[] toProperties(java.util.Properties opts)
           
static java.lang.String toString(byte[] buf)
           
static java.lang.String toString(byte[] buf, int len)
           
static java.lang.String toString(java.util.Collection col)
          Pretty print the collection
static java.lang.String toString(java.util.Properties options)
          Pretty print the mapping
static void write(java.io.OutputStream out, byte[] data, Sha256Standalone hash)
           
static void writeBoolean(java.io.OutputStream out, java.lang.Boolean bool)
          Write out a boolean as specified by the I2P data structure spec.
static void writeDate(java.io.OutputStream out, java.util.Date date)
          Write out a date to the stream as specified by the I2P data structure spec.
static void writeLong(java.io.OutputStream rawStream, int numBytes, long value)
          Write an integer as defined by the I2P data structure specification to the stream.
static void writeProperties(java.io.OutputStream rawStream, java.util.Properties props)
          Write a mapping to the stream, as defined by the I2P data structure spec, and store it into a Properties object.
static void writeString(java.io.OutputStream out, java.lang.String string)
          Write out a string to the stream as specified by the I2P data structure spec.
static byte[] xor(byte[] lhs, byte[] rhs)
           
static void xor(byte[] lhs, int startLeft, byte[] rhs, int startRight, byte[] out, int startOut, int len)
          xor the lhs with the rhs, storing the result in out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATE_LENGTH

public static final int DATE_LENGTH
See Also:
Constant Field Values

BOOLEAN_TRUE

public static final byte BOOLEAN_TRUE
See Also:
Constant Field Values

BOOLEAN_FALSE

public static final byte BOOLEAN_FALSE
See Also:
Constant Field Values

BOOLEAN_UNKNOWN

public static final byte BOOLEAN_UNKNOWN
See Also:
Constant Field Values

BOOLEAN_LENGTH

public static final int BOOLEAN_LENGTH
See Also:
Constant Field Values
Constructor Detail

DataHelper

public DataHelper()
Method Detail

readProperties

public static java.util.Properties readProperties(java.io.InputStream rawStream)
                                           throws DataFormatException,
                                                  java.io.IOException
Read a mapping from the stream, as defined by the I2P data structure spec, and store it into a Properties object. A mapping is a set of key / value pairs. It starts with a 2 byte Integer (ala readLong(rawStream, 2)) defining how many bytes make up the mapping. After that comes that many bytes making up a set of UTF-8 encoded characters. The characters are organized as key=value;. The key is a String (ala readString(rawStream)) unique as a key within the current mapping that does not include the UTF-8 characters '=' or ';'. After the key comes the literal UTF-8 character '='. After that comes a String (ala readString(rawStream)) for the value. Finally after that comes the literal UTF-8 character ';'. This key=value; is repeated until there are no more bytes (not characters!) left as defined by the first two byte integer.

Parameters:
rawStream - stream to read the mapping from
Returns:
mapping
Throws:
DataFormatException - if the format is invalid
java.io.IOException - if there is a problem reading the data

writeProperties

public static void writeProperties(java.io.OutputStream rawStream,
                                   java.util.Properties props)
                            throws DataFormatException,
                                   java.io.IOException
Write a mapping to the stream, as defined by the I2P data structure spec, and store it into a Properties object. See readProperties for the format.

Parameters:
rawStream - stream to write to
props - properties to write out
Throws:
DataFormatException - if there is not enough valid data to write out
java.io.IOException - if there is an IO error writing out the data

toProperties

public static int toProperties(byte[] target,
                               int offset,
                               java.util.Properties props)
                        throws DataFormatException,
                               java.io.IOException
Throws:
DataFormatException
java.io.IOException

fromProperties

public static int fromProperties(byte[] source,
                                 int offset,
                                 java.util.Properties target)
                          throws DataFormatException,
                                 java.io.IOException
Throws:
DataFormatException
java.io.IOException

toProperties

public static byte[] toProperties(java.util.Properties opts)

toString

public static java.lang.String toString(java.util.Properties options)
Pretty print the mapping


loadProps

public static void loadProps(java.util.Properties props,
                             java.io.File file)
                      throws java.io.IOException
A more efficient Properties.load

Throws:
java.io.IOException

loadProps

public static void loadProps(java.util.Properties props,
                             java.io.File file,
                             boolean forceLowerCase)
                      throws java.io.IOException
Throws:
java.io.IOException

loadProps

public static void loadProps(java.util.Properties props,
                             java.io.InputStream inStr)
                      throws java.io.IOException
Throws:
java.io.IOException

loadProps

public static void loadProps(java.util.Properties props,
                             java.io.InputStream inStr,
                             boolean forceLowerCase)
                      throws java.io.IOException
Throws:
java.io.IOException

storeProps

public static void storeProps(java.util.Properties props,
                              java.io.File file)
                       throws java.io.IOException
Throws:
java.io.IOException

toString

public static java.lang.String toString(java.util.Collection col)
Pretty print the collection


toString

public static java.lang.String toString(byte[] buf)

toString

public static java.lang.String toString(byte[] buf,
                                        int len)

toDecimalString

public static java.lang.String toDecimalString(byte[] buf,
                                               int len)

toHexString

public static final java.lang.String toHexString(byte[] data)

fromHexString

public static final byte[] fromHexString(java.lang.String val)

readLong

public static long readLong(java.io.InputStream rawStream,
                            int numBytes)
                     throws DataFormatException,
                            java.io.IOException
Read the stream for an integer as defined by the I2P data structure specification. Integers are a fixed number of bytes (numBytes), stored as unsigned integers in network byte order.

Parameters:
rawStream - stream to read from
numBytes - number of bytes to read and format into a number
Returns:
number
Throws:
DataFormatException - if the stream doesn't contain a validly formatted number of that many bytes
java.io.IOException - if there is an IO error reading the number

writeLong

public static void writeLong(java.io.OutputStream rawStream,
                             int numBytes,
                             long value)
                      throws DataFormatException,
                             java.io.IOException
Write an integer as defined by the I2P data structure specification to the stream. Integers are a fixed number of bytes (numBytes), stored as unsigned integers in network byte order.

Parameters:
value - value to write out
rawStream - stream to write to
numBytes - number of bytes to write the number into (padding as necessary)
Throws:
DataFormatException - if the stream doesn't contain a validly formatted number of that many bytes
java.io.IOException - if there is an IO error writing to the stream

toLong

public static byte[] toLong(int numBytes,
                            long value)
                     throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

toLong

public static void toLong(byte[] target,
                          int offset,
                          int numBytes,
                          long value)
                   throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

fromLong

public static long fromLong(byte[] src,
                            int offset,
                            int numBytes)

readDate

public static java.util.Date readDate(java.io.InputStream in)
                               throws DataFormatException,
                                      java.io.IOException
Read in a date from the stream as specified by the I2P data structure spec. A date is an 8 byte unsigned integer in network byte order specifying the number of milliseconds since midnight on January 1, 1970 in the GMT timezone. If the number is 0, the date is undefined or null. (yes, this means you can't represent midnight on 1/1/1970)

Parameters:
in - stream to read from
Returns:
date read, or null
Throws:
DataFormatException - if the stream doesn't contain a validly formatted date
java.io.IOException - if there is an IO error reading the date

writeDate

public static void writeDate(java.io.OutputStream out,
                             java.util.Date date)
                      throws DataFormatException,
                             java.io.IOException
Write out a date to the stream as specified by the I2P data structure spec.

Parameters:
out - stream to write to
date - date to write (can be null)
Throws:
DataFormatException - if the date is not valid
java.io.IOException - if there is an IO error writing the date

toDate

public static byte[] toDate(java.util.Date date)
                     throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

toDate

public static void toDate(byte[] target,
                          int offset,
                          long when)
                   throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

fromDate

public static java.util.Date fromDate(byte[] src,
                                      int offset)
                               throws DataFormatException
Throws:
DataFormatException

readString

public static java.lang.String readString(java.io.InputStream in)
                                   throws DataFormatException,
                                          java.io.IOException
Read in a string from the stream as specified by the I2P data structure spec. A string is 1 or more bytes where the first byte is the number of bytes (not characters!) in the string and the remaining 0-255 bytes are the non-null terminated UTF-8 encoded character array.

Parameters:
in - stream to read from
Returns:
UTF-8 string
Throws:
DataFormatException - if the stream doesn't contain a validly formatted string
java.io.IOException - if there is an IO error reading the string

writeString

public static void writeString(java.io.OutputStream out,
                               java.lang.String string)
                        throws DataFormatException,
                               java.io.IOException
Write out a string to the stream as specified by the I2P data structure spec. Note that the max size for a string allowed by the spec is 255 bytes.

Parameters:
out - stream to write string
string - string to write out: null strings are perfectly valid, but strings of excess length will cause a DataFormatException to be thrown
Throws:
DataFormatException - if the string is not valid
java.io.IOException - if there is an IO error writing the string

readBoolean

public static java.lang.Boolean readBoolean(java.io.InputStream in)
                                     throws DataFormatException,
                                            java.io.IOException
Read in a boolean as specified by the I2P data structure spec. A boolean is 1 byte that is either 0 (false), 1 (true), or 2 (null)

Parameters:
in - stream to read from
Returns:
boolean value, or null
Throws:
DataFormatException - if the boolean is not valid
java.io.IOException - if there is an IO error reading the boolean

writeBoolean

public static void writeBoolean(java.io.OutputStream out,
                                java.lang.Boolean bool)
                         throws DataFormatException,
                                java.io.IOException
Write out a boolean as specified by the I2P data structure spec. A boolean is 1 byte that is either 0 (false), 1 (true), or 2 (null)

Parameters:
out - stream to write to
bool - boolean value, or null
Throws:
DataFormatException - if the boolean is not valid
java.io.IOException - if there is an IO error writing the boolean

fromBoolean

public static java.lang.Boolean fromBoolean(byte[] data,
                                            int offset)

toBoolean

public static void toBoolean(byte[] data,
                             int offset,
                             boolean value)

toBoolean

public static void toBoolean(byte[] data,
                             int offset,
                             java.lang.Boolean value)

eq

public static final boolean eq(java.lang.Object lhs,
                               java.lang.Object rhs)
Helper util to compare two objects, including null handling.

This treats (null == null) as true, and (null == (!null)) as false.


eq

public static final boolean eq(java.util.Collection lhs,
                               java.util.Collection rhs)
Run a deep comparison across the two collections.

This treats (null == null) as true, (null == (!null)) as false, and then comparing each element via eq(object, object).

If the size of the collections are not equal, the comparison returns false. The collection order should be consistent, as this simply iterates across both and compares based on the value of each at each step along the way.


eq

public static final boolean eq(byte[] lhs,
                               byte[] rhs)
Run a comparison on the byte arrays, byte by byte.

This treats (null == null) as true, (null == (!null)) as false, and unequal length arrays as false.


eq

public static final boolean eq(int lhs,
                               int rhs)
Compare two integers, really just for consistency.


eq

public static final boolean eq(long lhs,
                               long rhs)
Compare two longs, really just for consistency.


eq

public static final boolean eq(byte lhs,
                               byte rhs)
Compare two bytes, really just for consistency.


eq

public static final boolean eq(byte[] lhs,
                               int offsetLeft,
                               byte[] rhs,
                               int offsetRight,
                               int length)

compareTo

public static final int compareTo(byte[] lhs,
                                  byte[] rhs)

xor

public static final byte[] xor(byte[] lhs,
                               byte[] rhs)

xor

public static final void xor(byte[] lhs,
                             int startLeft,
                             byte[] rhs,
                             int startRight,
                             byte[] out,
                             int startOut,
                             int len)
xor the lhs with the rhs, storing the result in out.

Parameters:
lhs - one of the source arrays
startLeft - starting index in the lhs array to begin the xor
rhs - the other source array
startRight - starting index in the rhs array to begin the xor
out - output array
startOut - starting index in the out array to store the result
len - how many bytes into the various arrays to xor

hashCode

public static int hashCode(java.lang.Object obj)
Calculate the hashcode of the object, using 0 for null


hashCode

public static int hashCode(java.util.Date obj)
Calculate the hashcode of the date, using 0 for null


hashCode

public static int hashCode(byte[] b)
Calculate the hashcode of the byte array, using 0 for null


hashCode

public static int hashCode(java.util.Collection col)
Calculate the hashcode of the collection, using 0 for null


read

public static int read(java.io.InputStream in,
                       byte[] target)
                throws java.io.IOException
Throws:
java.io.IOException

read

public static int read(java.io.InputStream in,
                       byte[] target,
                       int offset,
                       int length)
                throws java.io.IOException
Throws:
java.io.IOException

readLine

public static java.lang.String readLine(java.io.InputStream in)
                                 throws java.io.IOException
Read a newline delimited line from the stream, returning the line (without the newline), or null if EOF reached before the newline was found

Throws:
java.io.IOException

readLine

public static java.lang.String readLine(java.io.InputStream in,
                                        Sha256Standalone hash)
                                 throws java.io.IOException
update the hash along the way

Throws:
java.io.IOException

readLine

public static boolean readLine(java.io.InputStream in,
                               java.lang.StringBuffer buf)
                        throws java.io.IOException
Read in a line, placing it into the buffer (excluding the newline).

Returns:
true if the line was read, false if eof was reached before a newline was found
Throws:
java.io.IOException

readLine

public static boolean readLine(java.io.InputStream in,
                               java.lang.StringBuffer buf,
                               Sha256Standalone hash)
                        throws java.io.IOException
update the hash along the way

Throws:
java.io.IOException

write

public static void write(java.io.OutputStream out,
                         byte[] data,
                         Sha256Standalone hash)
                  throws java.io.IOException
Throws:
java.io.IOException

sortStructures

public static java.util.List sortStructures(java.util.Collection dataStructures)

formatDuration

public static java.lang.String formatDuration(long ms)

stripHTML

public static java.lang.String stripHTML(java.lang.String orig)
Strip out any HTML (simply removing any less than / greater than symbols)


compress

public static byte[] compress(byte[] orig)
compress the data and return a new GZIP compressed array


compress

public static byte[] compress(byte[] orig,
                              int offset,
                              int size)

decompress

public static byte[] decompress(byte[] orig)
                         throws java.io.IOException
decompress the GZIP compressed data (returning null on error)

Throws:
java.io.IOException

decompress

public static byte[] decompress(byte[] orig,
                                int offset,
                                int length)
                         throws java.io.IOException
Throws:
java.io.IOException

getUTF8

public static byte[] getUTF8(java.lang.String orig)

getUTF8

public static byte[] getUTF8(java.lang.StringBuffer orig)

getUTF8

public static java.lang.String getUTF8(byte[] orig)

getUTF8

public static java.lang.String getUTF8(byte[] orig,
                                       int offset,
                                       int len)