如何解决Java 1.8:使用 DeflaterInputStream 获取压缩时压缩的大小
我正在尝试通过对附加函数的大量调用来逐渐填充压缩缓冲区。每次调用该函数后,我想知道当前压缩流有多大。这样,一旦达到某个压缩大小,我就可以停止放入新数据。如果我只是追加输入数据并在每次 append() 时压缩所有累积的数据,我会遇到性能问题,因为我每次都必须从一开始就压缩整个数据。
我不知道怎么做。我尝试使用 PipedInputStream 和 PipedOutputStream,但问题是对 DeflaterInputStream.read() 的调用会阻塞,而且我没有看到预测是否会读取一个字节的方法。这是因为 DeflaterInputStream.available() 似乎总是返回 1,无论我是否可以读取一个字节。由于压缩流是压缩的,并不是每次我放入一个字节时都会得到一个字节。毕竟,这就是重点。
你们知道我能做什么吗?示例伪代码:
protected DeflaterInputStream m_dis;
protected PipedInputStream m_is;
protected PipedOutputStream m_os;
protected ByteArrayOutputStream m_out;
protected void Init(int s32BufSize) {
try
{
m_os = new PipedOutputStream();
m_is = new PipedInputStream(m_os);
m_dis = new DeflaterInputStream(m_is);
m_out = new ByteArrayOutputStream();
}
catch (Exception ex)
{
}
}
public long append(byte[] appBytes)
{
try
{
m_os.write(appBytes);
m_os.flush();
while (m_dis.available() > 0) // will always return 1
{
m_out.write(m_dis.read()); // read() may block,preventing next call to append()
m_out.flush();
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
return m_out.size();
}
我还尝试将读取从 m_dis 卸载到另一个线程。但是,读取然后阻塞,直到我调用 m_os.close()。但是在这样一个电话之后,我不能再重新连接流,所以我可以继续。
有人有想法吗?感谢您的帮助!
最好的问候,
对等
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。