简体   繁体   English

霍夫曼编码解码器 - 发送编码器字典

[英]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.我的困难是实现解码器 - 不改变讲师设置的 4 个功能。 I can add as many functions as I want - as long as the structure remains the same - for the original 4 functions.只要结构保持不变,我可以为原来的 4 个函数添加任意数量的函数。

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 check.txt 包含:abra cadabra out.txt 包含:0110111010101011010001101110

I only built the first function that encodes the file.我只构建了第一个编码文件的 function。 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以及帮助我编码的辅助函数 - 例如计算输入文件中字符数的 function 和构建并返回树的 function

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.我只构建了第一个编码文件的 function。 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以及帮助我编码的辅助函数 - 例如计算输入文件中字符数的 function 和构建并返回树的 function

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.为了移动 map - 我创建了 2 个函数,readTreeFile 和 writeTreeFile。 One works in encoder - the map is copied to a file.一个在编码器中工作 - map 被复制到一个文件中。 The second works the decoder - it pulls the map from the file.第二个工作解码器 - 它从文件中提取 map。

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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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