Solved

Huffman Compression

Posted on 2002-05-08
4
1,370 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

15 Experts available now in Live!

Get 1:1 Help Now