|
From: Daniel B. <dan...@in...> - 2005-08-03 13:29:49
|
Hi All,
I'm trying to get zlib to compress a data stream, which can then be sent
on to a server, because it is running on a small low power mobile
device, and J2ME doesn't know about files, I've had to try and make the
code as basic as possible.
The main problem I'm getting is that when ever I compress the stream I
only ever get 2 bytes returned. I've tried with Deflate and
DeflateOutputStream.
One of the code samples I've used which does this is as follows:
public static String compress(byte[] input) throws IOException{
//Create new Deflater object
Deflater def = new Deflater();
//Tell the Deflater what to compress
def.setInput(input);
//As we don't know the size of the array before hand we need to
create an array that will definatly be big enough
byte[] output = new byte[input.length*2];
//Run the compression and save the result in output
int len = def.deflate(output,0,output.length);
//return output as a string to be sent to server
return new String(output);
}
I've also tried looping checking def.needsInput(), I've tried using
DeflatorOutputStream with an underlying ByteArrayOutputStream, but I
only ever get back the bytes {0x79,0x9C} (or X and -100).
Another problem I have is, how do I know how big the output buffer
should be? I'm taking a guess that it will never be more than twice the
original size (after all it is possible for compression to actually make
something bigger!), but there must be a better way of doing it than this.
Any help that anyone can offer would be much appreicated.
Thanks
Daniel
|