English 中文(简体)
Java 数据流解压缩
原标题:Java Data stream decompression

从套接字( 缓存) 获取数据

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);

Sending 1000 bytes, compressed ZLIB and turns (800-900 bytes) But I do not know what exactly the size of sending. I need to read from socket 1 byte at a time and immediately unpack it until the total size of decompressed data becomes equal to 1000 bytes.

读一个字节,我这样做的时候:

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);

How do I fill in this cycle unpackbuffer? and check its size? Sorry, I hope my question is clear.

最佳回答

OK,如果您能够将数据长度与您再次发送的数据预设为长度,然后在另一端读取(如果您能够设计允许此操作的协议,这显然是最简单的理想解决方案),那么您就可以用字节压缩流。关键是创建一字节缓冲作为输入缓冲。代码然后看以下:

    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
    }
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签