use of java.util.zip.Inflater in project jetty.project by eclipse.
the class CompressExtension method decompress.
protected void decompress(ByteAccumulator accumulator, ByteBuffer buf) throws DataFormatException {
if ((buf == null) || (!buf.hasRemaining())) {
return;
}
byte[] output = new byte[DECOMPRESS_BUF_SIZE];
Inflater inflater = getInflater();
while (buf.hasRemaining() && inflater.needsInput()) {
if (!supplyInput(inflater, buf)) {
LOG.debug("Needed input, but no buffer could supply input");
return;
}
int read;
while ((read = inflater.inflate(output)) >= 0) {
if (read == 0) {
LOG.debug("Decompress: read 0 {}", toDetail(inflater));
break;
} else {
// do something with output
if (LOG.isDebugEnabled()) {
LOG.debug("Decompressed {} bytes: {}", read, toDetail(inflater));
}
accumulator.copyChunk(output, 0, read);
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Decompress: exiting {}", toDetail(inflater));
}
}
use of java.util.zip.Inflater in project MusicDNA by harjot-oberai.
the class ID3Compression method uncompress.
/**
* Decompress realFrameSize bytes to decompressedFrameSize bytes and return as ByteBuffer
*
* @param byteBuffer
* @param decompressedFrameSize
* @param realFrameSize
* @return
* @throws org.jaudiotagger.tag.InvalidFrameException
*
*/
protected static ByteBuffer uncompress(String identifier, String filename, ByteBuffer byteBuffer, int decompressedFrameSize, int realFrameSize) throws InvalidFrameException {
logger.config(filename + ":About to decompress " + realFrameSize + " bytes, expect result to be:" + decompressedFrameSize + " bytes");
// Decompress the bytes into this buffer, size initialized from header field
byte[] result = new byte[decompressedFrameSize];
byte[] input = new byte[realFrameSize];
//Store position ( just after frame header and any extra bits)
//Read frame data into array, and then put buffer back to where it was
int position = byteBuffer.position();
byteBuffer.get(input, 0, realFrameSize);
byteBuffer.position(position);
Inflater decompresser = new Inflater();
decompresser.setInput(input);
try {
int inflatedTo = decompresser.inflate(result);
logger.config(filename + ":Decompressed to " + inflatedTo + " bytes");
} catch (DataFormatException dfe) {
logger.log(Level.CONFIG, "Unable to decompress this frame:" + identifier, dfe);
//Update position of main buffer, so no attempt is made to reread these bytes
byteBuffer.position(byteBuffer.position() + realFrameSize);
throw new InvalidFrameException(ErrorMessage.ID3_UNABLE_TO_DECOMPRESS_FRAME.getMsg(identifier, filename, dfe.getMessage()));
}
decompresser.end();
return ByteBuffer.wrap(result);
}
use of java.util.zip.Inflater in project cassandra by apache.
the class DeflateCompressor method uncompressBuffer.
public void uncompressBuffer(ByteBuffer input, ByteBuffer output) throws IOException {
try {
Inflater inf = inflater.get();
inf.reset();
byte[] buffer = getThreadLocalScratchBuffer();
// Use half the buffer for input, half for output.
int chunkLen = buffer.length / 2;
while (input.remaining() > chunkLen) {
input.get(buffer, 0, chunkLen);
inf.setInput(buffer, 0, chunkLen);
while (!inf.needsInput()) {
int len = inf.inflate(buffer, chunkLen, chunkLen);
output.put(buffer, chunkLen, len);
}
}
int inputLength = input.remaining();
input.get(buffer, 0, inputLength);
inf.setInput(buffer, 0, inputLength);
while (!inf.needsInput()) {
int len = inf.inflate(buffer, chunkLen, chunkLen);
output.put(buffer, chunkLen, len);
}
} catch (DataFormatException e) {
throw new IOException(e);
}
}
use of java.util.zip.Inflater in project rest.li by linkedin.
the class DeflateCompressor method inflate.
@Override
public byte[] inflate(InputStream data) throws CompressionException {
byte[] input;
try {
input = IOUtils.toByteArray(data);
} catch (IOException e) {
throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM, e);
}
Inflater zlib = new Inflater();
zlib.setInput(input);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] temp = new byte[CompressionConstants.BUFFER_SIZE];
int bytesRead;
while (!zlib.finished()) {
try {
bytesRead = zlib.inflate(temp);
} catch (DataFormatException e) {
throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
}
if (bytesRead == 0) {
if (!zlib.needsInput()) {
throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName());
} else {
break;
}
}
if (bytesRead > 0) {
output.write(temp, 0, bytesRead);
}
}
zlib.end();
return output.toByteArray();
}
use of java.util.zip.Inflater in project AndroidAsync by koush.
the class HybiParser method finalize.
@Override
protected void finalize() throws Throwable {
Inflater inflater = mInflater;
if (inflater != null) {
try {
inflater.end();
} catch (Exception e) {
Log.e(TAG, "inflater.end failed", e);
}
}
super.finalize();
}
Aggregations