?
Solved

Huffman Compression

Posted on 2002-05-08
4
Medium Priority
?
1,377 Views
Last Modified: 2007-11-27
Is there a built in class in Java to carry out Huffman compression?
0
Comment
Question by:qdil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

770 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