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バイトになるまですぐに解凍する必要があります。

一度に1バイトを読み取るには、次のようにします。

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

このサイクルのunpackbufferに入力するにはどうすればよいですか?サイズを確認しますか?申し訳ありませんが、私の質問が明確であることを願っています。

4

1 に答える 1

1

OK, if you can't prepend the length to the data you're sending and then read in at the other end (which would obviously be the simplest ideal solution if you were able to design the protocol to allow this), then you can compress the stream 'byte by byte'. The trick is to create a 1-byte buffer as the input buffer. The code then looks as follows:

    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 に答える