简体   繁体   中英

Huffman code decoder - send a dictionary of encoder

Hi I'm a computer science student - in my second year. During my studies, they let me do a project. Build the Hoffman code. Much of the code I built and it works well for me. But I barely got stuck, the encoder I built well, but the decoder is causing me problems.

The encoding works properly. Encoding is done according to a dictionary per character. Decoding also needs this dictionary. My question is how can I pass the dictionary to the decoder - without changing the signatures of the original functions.

My difficulty is to implement the decoder - without changing the 4 functions set by the lecturer. I can add as many functions as I want - as long as the structure remains the same - for the original 4 functions.

The encoder - receives a file - and converts it into bytes - in compression. It goes over the original file, counts the number of impressions per character, and then builds a tree with code for each character.

The decoder - need to get the tree - to convert the bytes to the original file - here I got stuck. How do I send from the encoder the tree for the decoder.

The code has predefined classes and functions - according to the lecturer's instructions. I will not be able to change the signatures to functions. However, I may create some and what functions I want.

Non-changeable functions:

public class HuffmanEncoderDecoder implements Compressor
{

    public HuffmanEncoderDecoder()
    {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Compress(String[] input_names, String[] output_names)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void Decompress(String[] input_names, String[] output_names)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public byte[] CompressWithArray(String[] input_names, String[] output_names)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public byte[] DecompressWithArray(String[] input_names, String[] output_names)
    {
        // TODO Auto-generated method stub
        return null;
    }

}

The code I already implemented

    public static void main(String[] args) {

        String[] input_names = {"C:\\Documents\\files-huffman\\check.txt"};
        String[] output_names = {"C:\\Documents\\files-huffman\\out.txt"};
        HuffmanEncoderDecoder objectHuffman = new HuffmanEncoderDecoder();
        objectHuffman.Compress(input_names, output_names);
        //how to send to the objectHuffman.Decompress the tree or the map of codes

the check.txt contain: abra cadabra the out.txt contain: 0110111010101011010001101110

I only built the first function that encodes the file. And auxiliary functions that help me encode - such as a function that counts a number of characters in the input file and a function that builds and returns a tree

My question is how after encoding a file - I can decode it according to project terms? Thanks for the help.

I only built the first function that encodes the file. And auxiliary functions that help me encode - such as a function that counts a number of characters in the input file and a function that builds and returns a tree

After the forum help, I was able to move the tree from the encoder to the decoder. I added the tree into a file, and then created a new tree inside the decoder.

I add the code I wrote: The code shows me that the tree I'm moving - is a blank tree - but it can't be - because the encoding works properly.

In order to move the map - I created 2 functions, readTreeFile and writeTreeFile. One works in encoder - the map is copied to a file. The second works the decoder - it pulls the map from the file.

public static Map<Character, String> readTreeFile (String[] output_names)
{
    Map<Character, String> codes = new HashMap<>();
    try {
        FileInputStream f = new FileInputStream(output_names[1]);
        ObjectInputStream s = new ObjectInputStream(f);
        codes = (HashMap<Character, String>) s.readObject();
        s.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return codes;
}
public static void writeTreeFile (Map<Character, String> codes , String[] output_names)
{
    try {
        FileOutputStream f = new FileOutputStream(output_names[1]);
        ObjectOutputStream s = new ObjectOutputStream(f);
        s.writeObject(codes);
        s.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM