Main.java

import java.io.IOException;

public class Main
{

    private final CompressedOutputStream    m_cos;

    public static void main(String[] args)
    {
        try
        {
            final Main m = new Main(new CompressedOutputStream());
            m.run();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public Main(final CompressedOutputStream cos)
    {
        this.m_cos = cos;
    }

    private void trace(final String format, Object... args)
    {
        final String output = String.format(format, args);
        System.out.println(output);
    }

    public void run()
    {
        this.first_write();
        this.second_write();
        this.trace("CRC32 of data is %x", this.m_cos.get_crc32());
    }

    private void add_data(final byte[] data)
    {
        try
        {
            this.m_cos.write(data); // STEPPING INTO THIS IN DEBUG DOESN'T LEAD ANYWHERE!
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public void first_write()
    {
        final String[] text_lines = new String[] { "Hello world!",
                "My name is Joe.", "Nice to meet.", "Where are you from?",
                "I'm from Congo" };

        for (final String text_line : text_lines)
        {
            final byte[] data = text_line.getBytes();
            this.add_data(data);
        }

        final byte[] compressed_data = this.m_cos.get_compressed_data();
        trace("Compressed data length (so far) is %d bytes",
                compressed_data.length);
    }

    public void second_write()
    {
        final byte[] additional_data = new byte[] { 0x00, 0x01, 0x02, 0x03,
                0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
        this.add_data(additional_data);

        final byte[] total_compressed_data = this.m_cos.get_compressed_data();
        trace("Total compressed data length is %d bytes",
                total_compressed_data.length);
    }
}


CompressedOutputStream.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;

public class CompressedOutputStream extends GZIPOutputStream
{

    private final ByteArrayOutputStream m_out;

    public CompressedOutputStream() throws IOException
    {
        this(32);
    }

    public CompressedOutputStream(int size) throws IOException
    {
        super(new ByteArrayOutputStream(size), size);
        this.m_out = (ByteArrayOutputStream) this.out;
        this.def.setLevel(Deflater.BEST_COMPRESSION);
    }

    public long get_crc32()
    {
        return this.crc.getValue();
    }

    public byte[] get_compressed_data()
    {
        return this.m_out.toByteArray();
    }
}


所以我有这段代码尝试将GZIPOutputStream与ByteArrayOutputStream结合在一起。

由于某种原因,我得到的只是相同的10个字节。
获取此输出:


  压缩数据长度(到目前为止)为10个字节压缩数据总数
  长度为10个字节数据的CRC32为4550d94d


似乎对write()的调用最终带有一个抽象函数,即它什么也没得到,也没有写任何东西。
我希望它能够被动态地编写,压缩,并能够在以后使用压缩的字节。

我在这里想念什么?似乎微不足道,但事实并非如此。

编辑#1:我的最终目标
需要澄清的是:最后,我需要的是一个内存中的缓冲区,我将以块而不是序列的形式写入该缓冲区,并且在到达X字节的某个时刻,我将能够使用这些缓冲区,压缩后的&校验和,字节(将它们写入某个地方,而不是标准Java流)。

最佳答案

GZIPOutputStream在将数据传递到下一个之前进行一些内部缓冲
链中的流(在本例中为ByteArrayOutputStream)。因此,您必须先关闭流,然后才能读取回字节。例如,我对second_write()进行了以下修改:

this.m_cos.close();
final byte[] total_compressed_data = this.m_cos.get_compressed_data();
trace("Total compressed data length is %d bytes", total_compressed_data.length);


这给了我输出:


  压缩数据长度(到目前为止)为10个字节
  总压缩数据长度为98字节
  数据的CRC32为4550d94d


如果您无力关闭流,则可能有一种方法可以在不关闭的情况下强制刷新内部缓冲区。 (我尝试过flush()本身并没有这样做)。

10-08 00:50