com.iplanet.jato.util
Class Encoder

java.lang.Object
  |
  +--com.iplanet.jato.util.Encoder

public class Encoder
extends java.lang.Object

Static utility class for Base64 encoding/decoding a byte array or Serializable object with support for compression and escaped HTTP strings using a variant of Base64 encoding. The Base64 implementation relies on the sun.misc.* package included with Sun's implementation of the Java Platform; there is no guarantee that this implementation will be used in future versions of this class. The compression features use the java.util.zip package. This class provides static methods only and the default constructor is hidden. Base64 encoding essentially transforms binary data into ASCII; very helpful in cases where binary data needs to be stored in documents. In support of Html documents, a variant of Base64 encoding is used which ensures that encoded Strings can be stored in HTTP/HTML constructs; these strings are referred to as Http64 strings in this API.

Version:
JATO/1.2.2 $Id: Encoder.java,v 1.11 2002/03/16 03:26:38 todd Exp $
See Also:
Decoding Http64 strings, Encoding Http64 strings

Method Summary
static byte[] compress(byte[] in)
          Compresses array of bytes.
static byte[] decode(java.lang.String s)
          Decodes an Http64 string to an array of bytes using a variant of Base64 encoding.
static byte[] decodeBase64(java.lang.String s)
          Decodes a string using Base64 decoding into an array of bytes without compression.
static byte[] decodeHttp64(java.lang.String s)
          Decode an Http64 string to an array of bytes using a variant of Base64 encoding.
static byte[] decompress(byte[] in)
          Decompressed array of bytes using ZLIB.
static java.lang.Object deserialize(byte[] b, boolean compressed)
          Deserialize or 'reconstitute' an object from an array of bytes, which may be have been previously compressed.
static java.lang.String encode(byte[] bytes)
          Encodes an array of bytes into a Http64 string without compression using a variant of Base64 encoding.
static java.lang.String encodeBase64(byte[] bytes)
          Encodes an array of bytes into a Base64 string, without compression.
static java.lang.String encodeHttp64(byte[] bytes, int compressThreshold)
          Encodes an array of bytes into a compressed Http64 string using a variant of Base64 encoding, performing compression if the byte array exceeds the length specified by the threshold parameter.
static byte[] serialize(java.io.Serializable o, boolean compress)
          Serialize or 'flatten' an object to an array of bytes with optional compression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encode

public static java.lang.String encode(byte[] bytes)
Encodes an array of bytes into a Http64 string without compression using a variant of Base64 encoding. Shortcut for calling encodeHttp64(byte[],int)
Parameters:
bytes - binary data to be encoded, should not be null
Returns:
encoded Http64 string

decode

public static byte[] decode(java.lang.String s)
Decodes an Http64 string to an array of bytes using a variant of Base64 encoding. Shortcut for calling decodeHttp64(String)
Parameters:
s - Http64 string to be decoded
Returns:
decoded array of bytes or s.getBytes() on exception during execution

encodeBase64

public static java.lang.String encodeBase64(byte[] bytes)
Encodes an array of bytes into a Base64 string, without compression. Resulting string is uniform with no carriage returns.
Parameters:
bytes - binary data to be encoded
Returns:
Base64 encoded string

decodeBase64

public static byte[] decodeBase64(java.lang.String s)
Decodes a string using Base64 decoding into an array of bytes without compression. Strings not previously Base64 encoded will succeed.
Parameters:
s - string to be decoded (assumed to be Base64 encoded string)
Returns:
decoded array of bytes or s.getBytes() on exception during execution

encodeHttp64

public static java.lang.String encodeHttp64(byte[] bytes,
                                            int compressThreshold)
Encodes an array of bytes into a compressed Http64 string using a variant of Base64 encoding, performing compression if the byte array exceeds the length specified by the threshold parameter.
Parameters:
bytes - binary data to be encoded, should not be null
compressThreshold - compression peformed when byte array exceeds this value
Returns:
encoded Http64 string

decodeHttp64

public static byte[] decodeHttp64(java.lang.String s)
Decode an Http64 string to an array of bytes using a variant of Base64 encoding.
Parameters:
s - Http64 string to be decoded
Returns:
decoded array of bytes or s.getBytes() on exception during execution

compress

public static byte[] compress(byte[] in)
Compresses array of bytes. Handles cases where compression is counterproductive or when there is no compression yield.
Parameters:
in - array of bytes to be compressed (should not be null)
Returns:
array of bytes in ZLIB compression format

decompress

public static byte[] decompress(byte[] in)
Decompressed array of bytes using ZLIB.
Parameters:
in - array of bytes to be decompressed (should not be null)
Returns:
decompressed array of bytes

serialize

public static byte[] serialize(java.io.Serializable o,
                               boolean compress)
                        throws java.io.IOException
Serialize or 'flatten' an object to an array of bytes with optional compression.
Parameters:
o - object to be flattened (should be Serializable)
compress - flag indicating need to compress results
Returns:
serialized array of bytes from input object , optionally compressed,

deserialize

public static java.lang.Object deserialize(byte[] b,
                                           boolean compressed)
                                    throws java.io.IOException,
                                           java.lang.ClassNotFoundException
Deserialize or 'reconstitute' an object from an array of bytes, which may be have been previously compressed.
Parameters:
b - array of bytes representing previously serialized object
compressed - flag indicating need to decompress bytes first
Returns:
reconstituted object