Solved

Huffman Compression

Posted on 2002-05-08
4
1,371 Views
Last Modified: 2007-11-27
Is there a built in class in Java to carry out Huffman compression?
0
Comment
Question by:qdil
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 6996630
0
 
LVL 4

Expert Comment

by:Neutron
ID: 6996902
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> :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 8740776
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now