Howdy
I’ve been unsing the GZIP input and output streams and have noticed that there’s occasionally some bytes remaining in the underlying stream after the desired data has been read. For example:
byte[] data = /*some bytes generated by my code*/;
System.out.println( data.length + " source bytes" );
ByteArrayOutputStream bo = new ByteArrayOutputStream();
GZIPOutputStream gzo = new GZIPOutputStream( bo );
gzo.write( data );
gzo.flush();
gzo.close();
byte[] zipped = bo.toByteArray();
ByteArrayInputStream bi = new ByteArrayInputStream( zipped );
GZIPInputStream gzi = new GZIPInputStream( bi );
byte[] unzipped = new byte[ data.length ];
int read = 0;
int count = 0;
do
{
count += read;
read = gzi.read( unzipped, count, unzipped.length - count );
}
while( read > 0 );
System.out.println( "unzipped equals source data? " + Arrays.equals( data, unzipped ) );
System.out.print( bi.available() + " bytes left in bi [ " );
while( bi.available() > 0 )
{
System.out.print( ( byte ) bi.read() + ", " );
}
System.out.println( " ]" );
give the output
50293 source bytes
unzipped equals source data? true
6 bytes left in bi [ 36, -76, 117, -60, 0, 0, ]
This behaviour is triggered by the input data (it’s reproducible but rare) and I think should be regarded as a bug - intermittently leaving unexpected bytes in the stream is very naughty.
So, am I doing something wrong, or is this behaviour documented somewhere?