1

从套接字(缓冲区)获取数据

BufferedReader is = new BufferedReader(new InputStreamReader(download.getInputStream()));

数据解压

Inflater decompresser = new Inflater();
decompresser.setInput(buffer.toString().getBytes());
byte[] dataBytes = new byte[8388608];
int resultLength = decompresser.inflate(dataBytes);
decompresser.end();
System.out.println("decompressed" + new String(dataBytes, 0, resultLength)+ " RESULTLENGHT " +resultLength);

发送 1000 字节,压缩 ZLIB 和轮流(800-900 字节)但我不知道发送的确切大小。我需要一次从套接字读取 1 个字节并立即解压缩它,直到解压缩数据的总大小等于 1000 个字节。

要一次读取一个字节,我这样做:

StringBuilder buffer = new StringBuilder();
StringBuilder unpackbuffer = new StringBuilder();
do buffer.append((char)is.read());
while(buffer.charAt(buffer.length()-1) != '|' && (byte)buffer.charAt(buffer.length()-1) != -1);

我如何填写这个循环解包缓冲区?并检查它的大小?对不起,我希望我的问题很清楚。

4

1 回答 1

1

好的,如果您不能将长度添加到要发送的数据中,然后在另一端读入(如果您能够设计协议以允许这样做,这显然是最简单的理想解决方案),那么您可以'逐字节'压缩流。诀窍是创建一个 1 字节的缓冲区作为输入缓冲区。然后代码如下所示:

    Inflater infl = new Inflater();
    byte[] buf = new byte[1];
    byte[] outputBuf = new byte[512];
    while (!infl.finished()) {
        while (infl.needsInput()) {
            buf[0] = ...next byte from stream...
            infl.setInput(buf);
        }
        int noUnc = infl.inflate(outputBuf);
       // the first "noUnc" bytes of outputBuf contain the next bit of data
    }
于 2012-05-24T00:42:52.650 回答