Link to home
Start Free TrialLog in
Avatar of qdil
qdil

asked on

Huffman Compression

Is there a built in class in Java to carry out Huffman compression?
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Neutron
Neutron

Hi gdil, :-)

You could try this way, using java.util.zip package:

-------huffman.java-------cut here-------8<-------
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;


public class huffman
{
    private static final String INPUT = "this is the original. is this the original? this is the original!";
   
    public static void main( String[] args ) throws Throwable
    {
        Deflater deflater = new Deflater();
        deflater.setStrategy( Deflater.HUFFMAN_ONLY );
       
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        DeflaterOutputStream out = new DeflaterOutputStream( bytes, deflater );
       
        out.write( INPUT.getBytes(), 0, INPUT.length() );
        out.close();
        deflater.finish();
       
        byte[] compressed = bytes.toByteArray();
       
       
        System.out.println( "Input length:"+INPUT.length()+" Output length:"+compressed.length );
        System.out.println( "Compressed data: "+new String( compressed )+"\n-------" );
       
        ByteArrayInputStream compressedBytes = new ByteArrayInputStream( compressed );
        InflaterInputStream in = new InflaterInputStream( compressedBytes );
       
        ByteBuffer byteBuffer = new ByteBuffer();
        while (in.available()==1)
        {
            byteBuffer.append( (byte)in.read() );
        }
       
        in.close();
       
        String output = new String( byteBuffer.getBytes() );
       
        System.out.println( "Input string:  "+INPUT+"\nOutput string: "+output );
    }
   
} /* huffman */
-------huffman.java-------cut here-------8<-------

-------ByteBuffer.java-------cut here-------8<-------
public class ByteBuffer
{
    // Constants
    private static final int DEFAULT_CAPACITY = 32;
   
    // Properties
    private byte[] bytes;
    private int used;
   
    // Constructor
    public ByteBuffer()
    {
        bytes = new byte[DEFAULT_CAPACITY];
        used = 0;
    }
   
    // Methods
    public void append( byte b )
    {
        if (used < bytes.length)
        {
            bytes[used++] = b;
        }
        else
        {
            bytes = repack( bytes.length*2 );
            append( b );
        }
    }
   
    public byte[] getBytes()
    {
        return repack( used );
    }
   
    private byte[] repack( int newSize )
    {
        byte[] newBytes = new byte[newSize];
        System.arraycopy( bytes, 0, newBytes, 0, Math.min( used, newSize ) );
        return newBytes;
    }
   
} /* ByteBuffer */
-------ByteBuffer.java-------cut here-------8<-------

I haven't tried to decompress the codes generated by some other encoder, and also if some other huffman decoder would decompress codes generated by Deflator, but if you need that, you will hopefully test and post here your results :-)

Best wishes,
    </ntr> :)
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to girionis

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer