net.sourceforge.sqlunit.utils
Class DigestUtils

java.lang.Object
  extended by net.sourceforge.sqlunit.utils.DigestUtils

public final class DigestUtils
extends java.lang.Object

Provides utility methods needed for MD5 Digestion.

Version:
$Revision: 1.10 $
Author:
Sujit Pal (spal@users.sourceforge.net)

Method Summary
static byte[] getByteCodeForObject(java.lang.Object obj)
          Converts an object into an array of bytes.
static java.lang.String getMD5CheckSum(java.io.InputStream istream)
          Returns the MD5 Checksum for a file or memory buffer represented by the specified InputStream object.
static java.lang.String getMD5CheckSum(java.lang.String str)
          Takes a String and does the right thing to convert it to MD5 according to the following rules.
static java.lang.String getStringifiedObject(byte[] bytes)
          Returns the String representation of an Object which is supplied to the method as a byte array.
static java.lang.String getStringifiedObject(java.io.InputStream istream)
          Returns the String representation of an Object from an InputStream.
static java.util.Map getTempFileMappings()
          Gets the temporary file mappings from the Symbol Table.
static boolean isSerializedJavaObject(byte[] bytes)
          Checks the byte header for the presence of the magic number "aced" and returns true if it finds it.
static byte[] readBytesFromStream(java.io.InputStream istream)
          Reads an InputStream and returns an array of bytes.
static void writeTempFile(byte[] bytes)
          Writes the bytes into a temporary file and adds the mapping of the the location where it was written to the actual file name in the SymbolTable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMD5CheckSum

public static java.lang.String getMD5CheckSum(java.io.InputStream istream)
                                       throws SQLUnitException
Returns the MD5 Checksum for a file or memory buffer represented by the specified InputStream object. Creates a temporary file with the name pattern sqlunit-lob-*.dat as a side effect if the InputStream supplied to it is anything other than a FileInputStream. This is useful in case you have an error and you want to compare with diff or cmp.

Parameters:
istream - a File or Memory buffer containing data to be digested.
Returns:
the MD5 Checksum for the data.
Throws:
SQLUnitException - if there was a problem generating MD5.

getMD5CheckSum

public static java.lang.String getMD5CheckSum(java.lang.String str)
                                       throws SQLUnitException
Takes a String and does the right thing to convert it to MD5 according to the following rules. If it is prefixed with md5:, it assumes that this String has already been digested and returns it. If it is prefixed with file:, it assumes that it is a file whose contents are to be digested. If it is prefixed with obj:, it assumes that this is an object representatation and also lets it through. In all other cases, it will try to digest the String and return the MD5 checksum for it.

Parameters:
str - the String to convert to MD5.
Returns:
an MD5 digest String.
Throws:
SQLUnitException - if there was a problem with the conversion.

getStringifiedObject

public static java.lang.String getStringifiedObject(java.io.InputStream istream)
                                             throws SQLUnitException
Returns the String representation of an Object from an InputStream.

Parameters:
istream - the InputStream to read.
Returns:
the String representation of the object.
Throws:
SQLUnitException - if there was a problem with the conversion.

getStringifiedObject

public static java.lang.String getStringifiedObject(byte[] bytes)
                                             throws SQLUnitException
Returns the String representation of an Object which is supplied to the method as a byte array.

Parameters:
bytes - the bytes representing the Object to stringify.
Returns:
the String representation of the object.
Throws:
SQLUnitException - if there was a problem with the conversion.

readBytesFromStream

public static byte[] readBytesFromStream(java.io.InputStream istream)
                                  throws SQLUnitException
Reads an InputStream and returns an array of bytes.

Parameters:
istream - the InputStream to read.
Returns:
a byte array.
Throws:
SQLUnitException - if there was a problem with reading.

getByteCodeForObject

public static byte[] getByteCodeForObject(java.lang.Object obj)
                                   throws SQLUnitException
Converts an object into an array of bytes.

Parameters:
obj - the Object to get bytes for.
Returns:
an array of bytes.
Throws:
SQLUnitException - if there was a problem generating bytecode.

isSerializedJavaObject

public static boolean isSerializedJavaObject(byte[] bytes)
Checks the byte header for the presence of the magic number "aced" and returns true if it finds it. This is not totally reliable and depends on the convention that this is the magic number for a serialized Java object.

Parameters:
bytes - the byte code to check.
Returns:
true if the magic string is found, else false.

writeTempFile

public static void writeTempFile(byte[] bytes)
                          throws java.lang.Exception
Writes the bytes into a temporary file and adds the mapping of the the location where it was written to the actual file name in the SymbolTable.

Parameters:
bytes - the bytes to write to the temporary file.
Throws:
java.lang.Exception - if there was a problem writing the file.

getTempFileMappings

public static java.util.Map getTempFileMappings()
Gets the temporary file mappings from the Symbol Table.

Returns:
a Map of locations to the temporary file names.