3

I was asked to use huffman code to compress an input file and write it to an output file. I have finished implementing the huffman tree structure and generating the huffman codes. But I dont know how to write those codes into a file so that the file is less in size than the original file.

Right now I have the codes in string representation (e.g huffman code for 'c' is "0100"). Someone please help me write those bits into a file.

adonayresom
  • 280
  • 3
  • 15

1 Answers1

3

Here a possible implementation to write stream of bits(output of Huffman coding) into file.

class BitOutputStream {

    private OutputStream out;
    private boolean[] buffer = new boolean[8];
    private int count = 0;

    public BitOutputStream(OutputStream out) {
        this.out = out;
    }

    public void write(boolean x) throws IOException {
        this.count++;
        this.buffer[8-this.count] = x;
        if (this.count == 8){
            int num = 0;
            for (int index = 0; index < 8; index++){
                num = 2*num + (this.buffer[index] ? 1 : 0);
            }

            this.out.write(num - 128);

            this.count = 0;
        }
    }

    public void close() throws IOException {
        int num = 0;
        for (int index = 0; index < 8; index++){
            num = 2*num + (this.buffer[index] ? 1 : 0);
        }

        this.out.write(num - 128);

        this.out.close();
    }

}

By calling write method you will able to write bit by bit in a file (OutputStream).

Edit

For your specific problem, to save each character's huffman code you can simply use this if you don't want to use some other fancy class -

String huffmanCode = "0100"; // lets say its huffman coding output for c

BitSet huffmanCodeBit = new BitSet(huffmanCode.length());

for (int i = 0; i < huffmanCode.length(); i++) {
    if(huffmanCode.charAt(i) == '1')
        huffmanCodeBit.set(i);
}
String path = Resources.getResource("myfile.out").getPath();
ObjectOutputStream outputStream = null;
try {
    outputStream = new ObjectOutputStream(new FileOutputStream(path));
    outputStream.writeObject(huffmanCodeBit);
} catch (IOException e) {
    e.printStackTrace();
}
Kaidul
  • 15,409
  • 15
  • 81
  • 150
  • Thank you! How can I decode it back to the string if I use the first method? @KaidulIslam – adonayresom Apr 15 '17 at 21:25
  • Huffman coding generates variable length codes for different characters. So there is no way to determine how many bits should you read to get the next character. You need to put some delimeter between two codes to depict end mark. I have a question - do you need to save in the files in bit format or in ASCII format is okay? – Kaidul Apr 16 '17 at 03:48