Reputation: 161
get data from socket (buffer)
BufferedReader is = new BufferedReader(new InputStreamReader(download.getInputStream()));
data decompression
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.
To read a byte at a time I do so:
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.
Upvotes: 1
Views: 584
Reputation: 21795
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
}
Upvotes: 1