superwaba.ext.xplat.util
Class Compression

java.lang.Object
  |
  +--superwaba.ext.xplat.util.Compression

public class Compression
extends Object

Compression - a free java compression class based on a static/predefined Huffmann type scheme. Compresses text to on average 60%-70% of original size and works with byte[] on input and output. Uses waba.sys.Vm.copyArray() to redimension the output byte[]. This is the compression class used in my Dic program to compress the data and index dbs.

Has 2 public methods to shrink and to grow the data in a byte[] of len bytes: public static byte[] shrink(byte[] in, int len) public static byte[] grow(byte[] in, int len)

The routine was thought to be used as a text compressor so it's optimized to common european language text, but it can be optimized anyhow. It's a kind of Huffman encoding but it's statically predefined so it requires only one pass, it compresses on the fly using the tables that defined which characters are expected to be more commonly found in a text (with in addition the caracters ",", ";" and " "). I also used it with the Dic indexes forcing the index data to be text characters by means of using byte data offsetted to start with "a".

The first 4 characters ('e', ',', 'a', 'r') are reduced to 3 bits;
The next 8 characters ('i', 'n', 't', 'o', 's', 'l', ';', 'c') are reduced to 5 bits;
The next 8 characters ('u', 'd', 'm', 'h', 'p', 'g', 'b', ' ') are reducedto 6 bits;
The next 8 characters ('f', 'v', 'k', 'y', 'w', 'z', 'A', 'q', '-', 'x', 'S', 'B', 'j', 'G', 'E', 'M') are keeped at 8 bits (no gain);
All other characters are expanded to 12 bits (loss);

Normally a text can be reduced to 60% of original size.

Here is an example of how to use it:

byte []in = "If you clicked in the SuperWaba icon|and have no idea of what it is... This is|the Java Virtual Machine (JVM) for a|program that you maybe had|installed in your device.".getBytes();
byte []out = waba.util.Compression.shrink(in,in.length);
Vm.debug(in.length+" -> "+out.length+" - "+new String(out));
byte []def = waba.util.Compression.grow(out,out.length);
String ss = new String(def);


Constructor Summary
Compression()
           
 
Method Summary
static byte[] grow(byte[] in, int len)
          Grows the given in array with length len compressed with the shrink method.
static byte[] shrink(byte[] in, int len)
          Shrinks the given in array with length len
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, toString, wait, wait
 

Constructor Detail

Compression

public Compression()
Method Detail

shrink

public static byte[] shrink(byte[] in,
                            int len)
Shrinks the given in array with length len
Returns:
a newly created array with the compressed data.

grow

public static byte[] grow(byte[] in,
                          int len)
Grows the given in array with length len compressed with the shrink method.
Returns:
a newly created array with the expanded data.