net.i2p.data
Class DataHelper

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

public class DataHelper
extends Object

Defines some simple IO routines for dealing with marshalling data structures

Author:
jrandom

Field Summary
static byte BOOLEAN_FALSE
          deprecated - used only in DatabaseLookupMessage
static int BOOLEAN_LENGTH
          Deprecated. unused
static byte BOOLEAN_TRUE
          deprecated - used only in DatabaseLookupMessage
static byte BOOLEAN_UNKNOWN
          Deprecated. unused
static int DATE_LENGTH
           
static int MAX_COMPRESSION
           
static int MAX_UNCOMPRESSED
           
static int NO_COMPRESSION
           
 
Constructor Summary
DataHelper()
           
 
Method Summary
static int compareTo(byte[] lhs, byte[] rhs)
          Big endian compare, treats bytes as unsigned.
static byte[] compress(byte[] orig)
          Compress the data and return a new GZIP compressed byte array.
static byte[] compress(byte[] orig, int offset, int size)
          Compress the data and return a new GZIP compressed byte array.
static byte[] compress(byte[] orig, int offset, int size, int level)
          Compress the data and return a new GZIP compressed byte array.
static byte[] decompress(byte[] orig)
          Decompress the GZIP compressed data (returning null on error).
static byte[] decompress(byte[] orig, int offset, int length)
          Decompress the GZIP compressed data (returning null on error).
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)
          Unlike eq(byte[], byte[]), this returns false if either lhs or rhs is null.
static boolean eq(byte lhs, byte rhs)
          Deprecated. inefficient
static boolean eq(Collection lhs, Collection rhs)
          Run a deep comparison across the two collections.
static boolean eq(int lhs, int rhs)
          Deprecated. inefficient
static boolean eq(long lhs, long rhs)
          Deprecated. inefficient
static boolean eq(Object lhs, Object rhs)
          Helper util to compare two objects, including null handling.
static String escapeHTML(String unescaped)
          Escape a string for inclusion in HTML
static String formatDuration(long ms)
          NOTE: formatDuration2() recommended in most cases for readability
static String formatDuration2(long ms)
          Like formatDuration but with a non-breaking space after the number, 0 is unitless, and the unit is translated.
static String formatSize(long bytes)
          Caller should append 'B' or 'b' as appropriate NOTE: formatDuration2() recommended in most cases for readability
static String formatSize2(long bytes)
          Like formatSize but with a non-breaking space after the number This seems consistent with most style guides out there.
static Boolean fromBoolean(byte[] data, int offset)
          Deprecated. unused
static Date fromDate(byte[] src, int offset)
           
static byte[] fromHexString(String val)
           
static long fromLong(byte[] src, int offset, int numBytes)
           
static long fromLongLE(byte[] src, int offset, int numBytes)
          Little endian, i.e.
static int fromProperties(byte[] source, int offset, Properties target)
          Reads the props from the byte array and puts them in the Properties target See readProperties() for the format.
static String getUTF8(byte[] orig)
          Same as new String(orig, "UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use.
static String getUTF8(byte[] orig, int offset, int len)
          Deprecated. unused
static byte[] getUTF8(String orig)
          Same as orig.getBytes("UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use.
static byte[] getUTF8(StringBuffer orig)
          Deprecated. unused
static int hashCode(byte[] b)
          Calculate the hashcode of the byte array, using 0 for null
static int hashCode(Collection col)
          Calculate the hashcode of the collection, using 0 for null
static int hashCode(Date obj)
          Calculate the hashcode of the date, using 0 for null
static int hashCode(Object obj)
          Calculate the hashcode of the object, using 0 for null
static void loadProps(Properties props, File file)
          A more efficient Properties.load Some of the other differences: - This does not process or drop backslashes - '#' or ';' starts a comment line, but '!' does not - Leading whitespace is not trimmed - '=' is the only key-termination character (not ':' or whitespace)
static void loadProps(Properties props, File file, boolean forceLowerCase)
           
static void loadProps(Properties props, InputStream inStr)
           
static void loadProps(Properties props, InputStream inStr, boolean forceLowerCase)
           
static int read(InputStream in, byte[] target)
          This is different than InputStream.read(target), in that it does repeated reads until the full data is received.
static int read(InputStream in, byte[] target, int offset, int length)
          This is different than InputStream.read(target, offset, length), in that it returns the new offset (== old offset + bytes read).
static Boolean readBoolean(InputStream in)
          Deprecated. unused
static Date readDate(InputStream in)
          Read in a date from the stream as specified by the I2P data structure spec.
static String readLine(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 Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8
static String readLine(InputStream in, MessageDigest hash)
          update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8
static String readLine(InputStream in, Sha256Standalone hash)
          Deprecated. use MessageDigest version
static boolean readLine(InputStream in, StringBuffer buf)
          Deprecated. use StringBuilder version
static boolean readLine(InputStream in, StringBuffer buf, Sha256Standalone hash)
          Deprecated. use StringBuilder / MessageDigest version
static boolean readLine(InputStream in, StringBuilder buf)
          Read in a line, placing it into the buffer (excluding the newline).
static boolean readLine(InputStream in, StringBuilder buf, MessageDigest hash)
          update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8
static boolean readLine(InputStream in, StringBuilder buf, Sha256Standalone hash)
          Deprecated. use MessageDigest version
static long readLong(InputStream rawStream, int numBytes)
          Read the stream for an integer as defined by the I2P data structure specification.
static Properties readProperties(InputStream rawStream)
          Read a mapping from the stream, as defined by the I2P data structure spec, and store it into a Properties object.
static Properties readProperties(InputStream rawStream, Properties props)
          Ditto, load into an existing properties
static String readString(InputStream in)
          Read in a string from the stream as specified by the I2P data structure spec.
(package private) static void sortStructureList(List<? extends DataStructure> dataStructures)
          See above.
static List<? extends DataStructure> sortStructures(Collection<? extends DataStructure> dataStructures)
          Sort based on the Hash of the DataStructure.
static void storeProps(Properties props, File file)
          Writes the props to the file, unsorted (unless props is an OrderedProperties) Note that this does not escape the \r or \n that are unescaped in loadProps() above.
static String stripHTML(String orig)
          Strip out any HTML (simply removing any less than / greater than symbols)
static void toBoolean(byte[] data, int offset, boolean value)
          Deprecated. unused
static void toBoolean(byte[] data, int offset, Boolean value)
          Deprecated. unused
static void toDate(byte[] target, int offset, long when)
           
static byte[] toDate(Date date)
          Deprecated. unused
static String toDecimalString(byte[] buf, int len)
           
static String toHexString(byte[] data)
          Hex without leading zeros.
static void toLong(byte[] target, int offset, int numBytes, long value)
           
static byte[] toLong(int numBytes, long value)
           
static void toLongLE(byte[] target, int offset, int numBytes, long value)
          Little endian, i.e.
static int toProperties(byte[] target, int offset, Properties props)
          Deprecated. 
static byte[] toProperties(Properties opts)
          Writes the props to returned byte array, not sorted (unless the opts param is an OrderedProperties) Strings will be UTF-8 encoded in the byte array.
static String toString(byte[] buf)
          Hex with leading zeros.
static String toString(byte[] buf, int len)
          Hex with leading zeros.
static String toString(Collection col)
          Pretty print the collection
static String toString(Properties options)
          Pretty print the mapping, unsorted (unless the options param is an OrderedProperties)
static void write(OutputStream out, byte[] data, MessageDigest hash)
          update the hash along the way
static void write(OutputStream out, byte[] data, Sha256Standalone hash)
          Deprecated. use MessageDigest version
static void writeBoolean(OutputStream out, Boolean bool)
          Deprecated. unused
static void writeDate(OutputStream out, Date date)
          Write out a date to the stream as specified by the I2P data structure spec.
static void writeLong(OutputStream rawStream, int numBytes, long value)
          Write an integer as defined by the I2P data structure specification to the stream.
static void writeProperties(OutputStream rawStream, 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 writeProperties(OutputStream rawStream, Properties props, boolean utf8)
          Writes the props to the stream, sorted by property name.
static void writeProperties(OutputStream rawStream, Properties props, boolean utf8, boolean sort)
          Writes the props to the stream, sorted by property name if sort == true or if props is an OrderedProperties.
static void writeString(OutputStream out, 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
deprecated - used only in DatabaseLookupMessage

See Also:
Constant Field Values

BOOLEAN_FALSE

public static final byte BOOLEAN_FALSE
deprecated - used only in DatabaseLookupMessage

See Also:
Constant Field Values

BOOLEAN_UNKNOWN

@Deprecated
public static final byte BOOLEAN_UNKNOWN
Deprecated. unused
See Also:
Constant Field Values

BOOLEAN_LENGTH

@Deprecated
public static final int BOOLEAN_LENGTH
Deprecated. unused
See Also:
Constant Field Values

MAX_UNCOMPRESSED

public static final int MAX_UNCOMPRESSED
See Also:
Constant Field Values

MAX_COMPRESSION

public static final int MAX_COMPRESSION
See Also:
Constant Field Values

NO_COMPRESSION

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

DataHelper

public DataHelper()
Method Detail

readProperties

public static Properties readProperties(InputStream rawStream)
                                 throws DataFormatException,
                                        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:
an OrderedProperties
Throws:
DataFormatException - if the format is invalid
IOException - if there is a problem reading the data

readProperties

public static Properties readProperties(InputStream rawStream,
                                        Properties props)
                                 throws DataFormatException,
                                        IOException
Ditto, load into an existing properties

Parameters:
props - the Properties to load into
Throws:
DataFormatException
IOException
Since:
0.8.13

writeProperties

public static void writeProperties(OutputStream rawStream,
                                   Properties props)
                            throws DataFormatException,
                                   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. Output is sorted by property name. Property keys and values must not contain '=' or ';', this is not checked and they are not escaped Keys and values must be 255 bytes or less, Formatted length must not exceed 65535 bytes

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

writeProperties

public static void writeProperties(OutputStream rawStream,
                                   Properties props,
                                   boolean utf8)
                            throws DataFormatException,
                                   IOException
Writes the props to the stream, sorted by property name. See readProperties() for the format. Property keys and values must not contain '=' or ';', this is not checked and they are not escaped Keys and values must be 255 bytes or less, Formatted length must not exceed 65535 bytes

Parameters:
props - source may be null
Throws:
DataFormatException - if either is too long. jrandom disabled UTF-8 in mid-2004, for performance reasons, i.e. slow foo.getBytes("UTF-8") Re-enable it so we can pass UTF-8 tunnel names through the I2CP SessionConfig. Use utf8 = false for RouterAddress (fast, non UTF-8) Use utf8 = true for SessionConfig (slow, UTF-8)
IOException

writeProperties

public static void writeProperties(OutputStream rawStream,
                                   Properties props,
                                   boolean utf8,
                                   boolean sort)
                            throws DataFormatException,
                                   IOException
Writes the props to the stream, sorted by property name if sort == true or if props is an OrderedProperties. See readProperties() for the format. Property keys and values must not contain '=' or ';', this is not checked and they are not escaped Keys and values must be 255 bytes or less, Formatted length must not exceed 65535 bytes jrandom disabled UTF-8 in mid-2004, for performance reasons, i.e. slow foo.getBytes("UTF-8") Re-enable it so we can pass UTF-8 tunnel names through the I2CP SessionConfig. Use utf8 = false for RouterAddress (fast, non UTF-8) Use utf8 = true for SessionConfig (slow, UTF-8)

Parameters:
props - source may be null
sort - should we sort the properties? (set to false if already sorted, e.g. OrderedProperties)
Throws:
DataFormatException - if any string is over 255 bytes long, or if the total length (not including the two length bytes) is greater than 65535 bytes.
IOException
Since:
0.8.7

toProperties

@Deprecated
public static int toProperties(byte[] target,
                                          int offset,
                                          Properties props)
                        throws DataFormatException,
                               IOException
Deprecated. 

Throws:
DataFormatException
IOException

fromProperties

public static int fromProperties(byte[] source,
                                 int offset,
                                 Properties target)
                          throws DataFormatException
Reads the props from the byte array and puts them in the Properties target See readProperties() for the format. Warning - confusing method name, Properties is the target. Strings must be UTF-8 encoded in the byte array.

Parameters:
source - source
target - returned Properties
Returns:
new offset
Throws:
DataFormatException

toProperties

public static byte[] toProperties(Properties opts)
                           throws DataFormatException
Writes the props to returned byte array, not sorted (unless the opts param is an OrderedProperties) Strings will be UTF-8 encoded in the byte array. See readProperties() for the format. Property keys and values must not contain '=' or ';', this is not checked and they are not escaped Keys and values must be 255 bytes or less, Formatted length must not exceed 65535 bytes Warning - confusing method name, Properties is the source.

Throws:
DataFormatException - if key, value, or total is too long

toString

public static String toString(Properties options)
Pretty print the mapping, unsorted (unless the options param is an OrderedProperties)


loadProps

public static void loadProps(Properties props,
                             File file)
                      throws IOException
A more efficient Properties.load Some of the other differences: - This does not process or drop backslashes - '#' or ';' starts a comment line, but '!' does not - Leading whitespace is not trimmed - '=' is the only key-termination character (not ':' or whitespace)

Throws:
IOException

loadProps

public static void loadProps(Properties props,
                             File file,
                             boolean forceLowerCase)
                      throws IOException
Parameters:
forceLowerCase - if true forces the keys to lower case (not the values)
Throws:
IOException

loadProps

public static void loadProps(Properties props,
                             InputStream inStr)
                      throws IOException
Throws:
IOException

loadProps

public static void loadProps(Properties props,
                             InputStream inStr,
                             boolean forceLowerCase)
                      throws IOException
Parameters:
forceLowerCase - if true forces the keys to lower case (not the values)
Throws:
IOException

storeProps

public static void storeProps(Properties props,
                              File file)
                       throws IOException
Writes the props to the file, unsorted (unless props is an OrderedProperties) Note that this does not escape the \r or \n that are unescaped in loadProps() above. As of 0.8.1, file will be mode 600. Leading or trailing whitespace in values is not checked but will be trimmed by loadProps()

Throws:
IllegalArgumentException - if a key contains any of "#=\n" or starts with ';', or a value contains '#' or '\n'
IOException

toString

public static String toString(Collection col)
Pretty print the collection


toString

public static String toString(byte[] buf)
Hex with leading zeros. Use toHexString(byte[]) to not get leading zeros

Parameters:
buf - may be null (returns "")
Returns:
String of length 2*buf.length

toString

public static String toString(byte[] buf,
                              int len)
Hex with leading zeros. Use toHexString(byte[]) to not get leading zeros

Parameters:
buf - may be null
len - number of bytes. If greater than buf.length, additional zeros will be prepended
Returns:
String of length 2*len

toDecimalString

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

toHexString

public static final String toHexString(byte[] data)
Hex without leading zeros. Use toString(byte[] to get leading zeros

Parameters:
data - may be null (returns "00")

fromHexString

public static final byte[] fromHexString(String val)

readLong

public static long readLong(InputStream rawStream,
                            int numBytes)
                     throws DataFormatException,
                            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, 1 to 8
Returns:
number
Throws:
DataFormatException - if negative (only possible if numBytes = 8) (since 0.8.12)
EOFException - since 0.8.2, if there aren't enough bytes to read the number
IOException - if there is an IO error reading the number

writeLong

public static void writeLong(OutputStream rawStream,
                             int numBytes,
                             long value)
                      throws DataFormatException,
                             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, non-negative
rawStream - stream to write to
numBytes - number of bytes to write the number into (padding as necessary)
Throws:
DataFormatException - if value is negative
IOException - if there is an IO error writing to the stream

toLong

public static byte[] toLong(int numBytes,
                            long value)
                     throws IllegalArgumentException
Parameters:
numBytes - 1-8
value - non-negative
Throws:
IllegalArgumentException

toLong

public static void toLong(byte[] target,
                          int offset,
                          int numBytes,
                          long value)
                   throws IllegalArgumentException
Parameters:
numBytes - 1-8
value - non-negative
Throws:
IllegalArgumentException

toLongLE

public static void toLongLE(byte[] target,
                            int offset,
                            int numBytes,
                            long value)
Little endian, i.e. backwards. Not for use in I2P protocols.

Parameters:
numBytes - 1-8
value - non-negative
Since:
0.8.12

fromLong

public static long fromLong(byte[] src,
                            int offset,
                            int numBytes)
Parameters:
src - if null returns 0
numBytes - 1-8
Returns:
non-negative
Throws:
AIOOBE
IllegalArgumentException - if negative (only possible if numBytes = 8)

fromLongLE

public static long fromLongLE(byte[] src,
                              int offset,
                              int numBytes)
Little endian, i.e. backwards. Not for use in I2P protocols.

Parameters:
numBytes - 1-8
Returns:
non-negative
Throws:
AIOOBE
IllegalArgumentException - if negative (only possible if numBytes = 8)
Since:
0.8.12

readDate

public static Date readDate(InputStream in)
                     throws DataFormatException,
                            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
IOException - if there is an IO error reading the date

writeDate

public static void writeDate(OutputStream out,
                             Date date)
                      throws DataFormatException,
                             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
IOException - if there is an IO error writing the date

toDate

@Deprecated
public static byte[] toDate(Date date)
                     throws IllegalArgumentException
Deprecated. unused

Throws:
IllegalArgumentException

toDate

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

fromDate

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

readString

public static String readString(InputStream in)
                         throws DataFormatException,
                                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
EOFException - since 0.8.2, if there aren't enough bytes to read the string
IOException - if there is an IO error reading the string

writeString

public static void writeString(OutputStream out,
                               String string)
                        throws DataFormatException,
                               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. WARNING - this method destroys the encoding, and therefore violates the data structure spec.

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
IOException - if there is an IO error writing the string

readBoolean

public static Boolean readBoolean(InputStream in)
                           throws DataFormatException,
                                  IOException
Deprecated. unused

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
IOException - if there is an IO error reading the boolean

writeBoolean

@Deprecated
public static void writeBoolean(OutputStream out,
                                           Boolean bool)
                         throws DataFormatException,
                                IOException
Deprecated. unused

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
IOException - if there is an IO error writing the boolean

fromBoolean

@Deprecated
public static Boolean fromBoolean(byte[] data,
                                             int offset)
Deprecated. unused


toBoolean

@Deprecated
public static void toBoolean(byte[] data,
                                        int offset,
                                        boolean value)
Deprecated. unused


toBoolean

@Deprecated
public static void toBoolean(byte[] data,
                                        int offset,
                                        Boolean value)
Deprecated. unused


eq

public static final boolean eq(Object lhs,
                               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(Collection lhs,
                               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.

Returns:
Arrays.equals(lhs, rhs)

eq

@Deprecated
public static final boolean eq(int lhs,
                                          int rhs)
Deprecated. inefficient

Compare two integers, really just for consistency.


eq

@Deprecated
public static final boolean eq(long lhs,
                                          long rhs)
Deprecated. inefficient

Compare two longs, really just for consistency.


eq

@Deprecated
public static final boolean eq(byte lhs,
                                          byte rhs)
Deprecated. inefficient

Compare two bytes, really just for consistency.


eq

public static final boolean eq(byte[] lhs,
                               int offsetLeft,
                               byte[] rhs,
                               int offsetRight,
                               int length)
Unlike eq(byte[], byte[]), this returns false if either lhs or rhs is null.

Throws:
AIOOBE - if either array isn't long enough

compareTo

public static final int compareTo(byte[] lhs,
                                  byte[] rhs)
Big endian compare, treats bytes as unsigned. Shorter arg is lesser. Args may be null, null is less than non-null.


xor

public static final byte[] xor(byte[] lhs,
                               byte[] rhs)
Returns:
null if either arg is null or the args are not equal length

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(Object obj)
Calculate the hashcode of the object, using 0 for null


hashCode

public static int hashCode(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(Collection col)
Calculate the hashcode of the collection, using 0 for null


read

public static int read(InputStream in,
                       byte[] target)
                throws IOException
This is different than InputStream.read(target), in that it does repeated reads until the full data is received.

Throws:
IOException

read

public static int read(InputStream in,
                       byte[] target,
                       int offset,
                       int length)
                throws IOException
This is different than InputStream.read(target, offset, length), in that it returns the new offset (== old offset + bytes read). It also does repeated reads until the full data is received.

Returns:
the new offset (== old offset + bytes read)
Throws:
IOException

readLine

public static String readLine(InputStream in)
                       throws 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 Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException

readLine

public static String readLine(InputStream in,
                              Sha256Standalone hash)
                       throws IOException
Deprecated. use MessageDigest version

update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException

readLine

public static String readLine(InputStream in,
                              MessageDigest hash)
                       throws IOException
update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException
Since:
0.8.8

readLine

@Deprecated
public static boolean readLine(InputStream in,
                                          StringBuffer buf)
                        throws IOException
Deprecated. use StringBuilder version

Read in a line, placing it into the buffer (excluding the newline). Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

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

readLine

@Deprecated
public static boolean readLine(InputStream in,
                                          StringBuffer buf,
                                          Sha256Standalone hash)
                        throws IOException
Deprecated. use StringBuilder / MessageDigest version

update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException

readLine

public static boolean readLine(InputStream in,
                               StringBuilder buf)
                        throws IOException
Read in a line, placing it into the buffer (excluding the newline). Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

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

readLine

public static boolean readLine(InputStream in,
                               StringBuilder buf,
                               Sha256Standalone hash)
                        throws IOException
Deprecated. use MessageDigest version

update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException

readLine

public static boolean readLine(InputStream in,
                               StringBuilder buf,
                               MessageDigest hash)
                        throws IOException
update the hash along the way Warning - strips \n but not \r Warning - 8KB line length limit as of 0.7.13, @throws IOException if exceeded Warning - not UTF-8

Throws:
IOException
Since:
0.8.8

write

public static void write(OutputStream out,
                         byte[] data,
                         Sha256Standalone hash)
                  throws IOException
Deprecated. use MessageDigest version

update the hash along the way

Throws:
IOException

write

public static void write(OutputStream out,
                         byte[] data,
                         MessageDigest hash)
                  throws IOException
update the hash along the way

Throws:
IOException
Since:
0.8.8

sortStructures

public static List<? extends DataStructure> sortStructures(Collection<? extends DataStructure> dataStructures)
Sort based on the Hash of the DataStructure. Warning - relatively slow. WARNING - this sort order must be consistent network-wide, so while the order is arbitrary, it cannot be changed. Why? Just because it has to be consistent so signing will work. How to spec as returning the same type as the param? DEPRECATED - Only used by RouterInfo.

Returns:
a new list

sortStructureList

static void sortStructureList(List<? extends DataStructure> dataStructures)
See above. DEPRECATED - Only used by RouterInfo.

Since:
0.9

formatDuration

public static String formatDuration(long ms)
NOTE: formatDuration2() recommended in most cases for readability


formatDuration2

public static String formatDuration2(long ms)
Like formatDuration but with a non-breaking space after the number, 0 is unitless, and the unit is translated. This seems consistent with most style guides out there. Use only in HTML. Thresholds are a little lower than in formatDuration() also, as precision is less important in the GUI than in logging. Negative numbers handled correctly.

Since:
0.8.2

formatSize

public static String formatSize(long bytes)
Caller should append 'B' or 'b' as appropriate NOTE: formatDuration2() recommended in most cases for readability


formatSize2

public static String formatSize2(long bytes)
Like formatSize but with a non-breaking space after the number This seems consistent with most style guides out there. Use only in HTML

Since:
0.7.14

stripHTML

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

Parameters:
orig - may be null, returns empty string if null

escapeHTML

public static String escapeHTML(String unescaped)
Escape a string for inclusion in HTML

Parameters:
unescaped - the unescaped string, may be null
Returns:
the escaped string, or null if null is passed in

compress

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

Throws:
IllegalArgumentException - if size is over 40KB

compress

public static byte[] compress(byte[] orig,
                              int offset,
                              int size)
Compress the data and return a new GZIP compressed byte array.

Throws:
IllegalArgumentException - if size is over 40KB

compress

public static byte[] compress(byte[] orig,
                              int offset,
                              int size,
                              int level)
Compress the data and return a new GZIP compressed byte array.

Throws:
IllegalArgumentException - if size is over 40KB

decompress

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

Throws:
IOE - if uncompressed is over 40 KB
IOException

decompress

public static byte[] decompress(byte[] orig,
                                int offset,
                                int length)
                         throws IOException
Decompress the GZIP compressed data (returning null on error).

Throws:
IOE - if uncompressed is over 40 KB
IOException

getUTF8

public static byte[] getUTF8(String orig)
Same as orig.getBytes("UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use.

Returns:
null if orig is null
Throws:
RuntimeException

getUTF8

public static byte[] getUTF8(StringBuffer orig)
Deprecated. unused

Same as orig.getBytes("UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use.

Returns:
null if orig is null
Throws:
RuntimeException

getUTF8

public static String getUTF8(byte[] orig)
Same as new String(orig, "UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use. Used by Syndie.

Returns:
null if orig is null
Throws:
RuntimeException

getUTF8

public static String getUTF8(byte[] orig,
                             int offset,
                             int len)
Deprecated. unused

Same as new String(orig, "UTF-8") but throws an unchecked RuntimeException instead of an UnsupportedEncodingException if no UTF-8, for ease of use.

Returns:
null if orig is null
Throws:
RuntimeException