Pon*_*oni 2 java compression bytearrayoutputstream gzipoutputstream
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
所以我有这个代码试图将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);
Run Code Online (Sandbox Code Playgroud)
这给了我输出:
压缩数据长度(到目前为止)为10个字节.
总压缩数据长度为98个字节
CRC32数据为4550d94d
如果您无法关闭流,可能有一种方法可以在不关闭的情况下强制刷新内部缓冲区.(flush()本身不会这样做,但我试过).