Solved

Huffman Compression

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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