I am trying to fill a compressed buffer gradually with a lot of calls to an append-function. After each call of the function, I want to know how big the compressed stream is currently. This way, I can stop putting in new data once a certain compressed size has been reached. If I just append the input data and compress all of the accumulated data at every append(), I will get performance problems, because I will always have to compress the whole data from the start each time.
I cannot find out how to do it. I tried using PipedInputStream and PipedOutputStream, but the problem is that a call to DeflaterInputStream.read() will block and I don't see a way of predicting if I will read a byte or not. This is, because DeflaterInputStream.available() seems to always return 1, regardless if I can read a byte or not. As the compressed stream is, well, compressed, not every time I put a byte in I will get a byte out. That's the point of it, after all.
Do you people have an idea what I could do? Example pseudo code:
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();
}
I also tried to offload the reading from m_dis to another thread. But, the read then blocks until I call m_os.close(). But after such a call I cannot reconnect the streams anymore so I could go on.
Does anyone have an idea? Thanks for any help!
Best regards,
Peer
question from:
https://stackoverflow.com/questions/65844896/java-1-8-use-deflaterinputstream-get-size-of-comressed-while-compressing 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…