throw new MatlabIOException("Compressed buffer length miscalculated!");
}
//instead of standard Inflater class instance, use an inflater input
//stream... gives a great boost to the performance
InflaterInputStream iis = new InflaterInputStream( new InputStream() {
int limit = numOfBytes;
@Override
public synchronized int read() throws IOException
{
// TODO Auto-generated method stub
throw new RuntimeException("Not yet implemented");
}
public synchronized int read(byte[] bytes, int off, int len) throws IOException {
if ( !(limit > 0) )
{
return -1;
}
len = Math.min(len, limit);
// Read only what's left
buf.get(bytes, off, len);
limit -= len;
return len;
}
});
//process data decompression
byte[] result = new byte[128];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
try
{
do
{
i = iis.read(result, 0, result.length);
int len = Math.max(0, i);
baos.write(result, 0, len);
}
while ( i > 0 );
}
catch ( IOException e )
{
throw new MatlabIOException("Could not decompress data: " + e );
}
finally
{
iis.close();
}
//create a ByteBuffer from the deflated data
ByteBuffer out = ByteBuffer.wrap( baos.toByteArray() );
//with proper byte ordering
out.order( byteOrder );