SolarMetric Kodo JDO 2.5.8 generated on January 11 2004

serp.util
Class CompressedString

java.lang.Object
  |
  +--serp.util.CompressedString
All Implemented Interfaces:
Cloneable, Comparable, Serializable

public class CompressedString
extends Object
implements Serializable, Cloneable, Comparable

A CompressedString holds on to a compressed form of a String, as well as a Reference that caches the uncompressed version. The toString() method will transparently decompress the String if it has not been cached. A CompressedString is useful for maintaining caches of very large Strings, such as a cache of the complete contents of a set or text files. Note that decompressing the Strings can be a very slow operation.

By default, a GZIPOutputStream is used to compress the String using gzip compression, which can achieve very high compression ratios (between 50% and 95%). More information on gzip can be found in the RFC description.

See Also:
Serialized Form

Constructor Summary
CompressedString(String str)
          Construct a new CompressedString that holds a compressed version of the specified String.
CompressedString(String str, boolean useWeakReference)
          Construct a new CompressedString that holds a compressed version of the specified String.
 
Method Summary
 void clearCache()
          Clear out the contents of the String cache.
 int compareTo(Object other)
          Compares the String forms.
protected  byte[] compressString(String str)
          Compress the specified String into an array of bytes.
(package private)  Reference createReference(Object forObject)
          Create a new reference for the specified object.
 boolean equals(Object other)
          Two CompressedStrings are equal if they both use the same reference type and all the compressed bytes are the same.
 int getCompressedSize()
          Returns the size of the compressed bytes.
 int hashCode()
          hashCode is calculated by summing up all the individual bytes.
 boolean isCompressed()
          Returns true of the String cache has been lost, and only the compressed form is being held.
 String toString()
          Return the raw String form.
protected  String uncompressString(byte[] compressed)
          Uncompress the specified String from an array of bytes.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CompressedString

public CompressedString(String str)
Construct a new CompressedString that holds a compressed version of the specified String. By default, the String will be cached using a WeakReference.

CompressedString

public CompressedString(String str,
                        boolean useWeakReference)
Construct a new CompressedString that holds a compressed version of the specified String. By default, the String will be cached using a WeakReference.
Parameters:
str - the String to compress
useWeakReference - if true, then a WeakReference will be used to cache the String. If false, a SoftReference will be used.
Method Detail

compareTo

public int compareTo(Object other)
Compares the String forms.
Specified by:
compareTo in interface Comparable

equals

public boolean equals(Object other)
Two CompressedStrings are equal if they both use the same reference type and all the compressed bytes are the same.
Overrides:
equals in class Object

hashCode

public int hashCode()
hashCode is calculated by summing up all the individual bytes. This is much faster than decompressing it to a String and using the String's hashCode.
Overrides:
hashCode in class Object

createReference

Reference createReference(Object forObject)
Create a new reference for the specified object. This will create a new WeakReference or SoftReference, depending on how the CompressedString was created.

toString

public String toString()
Return the raw String form. If the String value is cached in the internal reference cache, then that will be returned. Otherwise, the String will be decompressed and returned.
Overrides:
toString in class Object

getCompressedSize

public int getCompressedSize()
Returns the size of the compressed bytes.

isCompressed

public boolean isCompressed()
Returns true of the String cache has been lost, and only the compressed form is being held.

clearCache

public void clearCache()
Clear out the contents of the String cache.

compressString

protected byte[] compressString(String str)
Compress the specified String into an array of bytes. By default, this uses a GZIPOutputStream in order to use gzip compression. This method can be overridden in order to use a different compression algorithm.
Throws:
IllegalStateException - if comprssion problems occur

uncompressString

protected String uncompressString(byte[] compressed)
Uncompress the specified String from an array of bytes. By default, this uses a GZIPInputStream in order to use gzip compression. This method can be overridden in order to use a different compression algorithm.
Throws:
IllegalStateException - if decomprssion problems occur

SolarMetric Kodo JDO 2.5.8 generated on January 11 2004

Copyright 2001,2002,2003 SolarMetric, Inc. All Rights Reserved.