use of java.util.zip.DataFormatException in project tomcat by apache.
the class PerMessageDeflate method getMoreData.
@Override
public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest) throws IOException {
// a WebSocket method. Pass them straight through.
if (Util.isControl(opCode)) {
return next.getMoreData(opCode, fin, rsv, dest);
}
if (!Util.isContinuation(opCode)) {
// First frame in new message
skipDecompression = (rsv & RSV_BITMASK) == 0;
}
// Pass uncompressed frames straight through.
if (skipDecompression) {
return next.getMoreData(opCode, fin, rsv, dest);
}
int written;
boolean usedEomBytes = false;
while (dest.remaining() > 0) {
// Space available in destination. Try and fill it.
try {
written = inflater.inflate(dest.array(), dest.arrayOffset() + dest.position(), dest.remaining());
} catch (DataFormatException e) {
throw new IOException(sm.getString("perMessageDeflate.deflateFailed"), e);
}
dest.position(dest.position() + written);
if (inflater.needsInput() && !usedEomBytes) {
if (dest.hasRemaining()) {
readBuffer.clear();
TransformationResult nextResult = next.getMoreData(opCode, fin, (rsv ^ RSV_BITMASK), readBuffer);
inflater.setInput(readBuffer.array(), readBuffer.arrayOffset(), readBuffer.position());
if (TransformationResult.UNDERFLOW.equals(nextResult)) {
return nextResult;
} else if (TransformationResult.END_OF_FRAME.equals(nextResult) && readBuffer.position() == 0) {
if (fin) {
inflater.setInput(EOM_BYTES);
usedEomBytes = true;
} else {
return TransformationResult.END_OF_FRAME;
}
}
}
} else if (written == 0) {
if (fin && (isServer && !clientContextTakeover || !isServer && !serverContextTakeover)) {
inflater.reset();
}
return TransformationResult.END_OF_FRAME;
}
}
return TransformationResult.OVERFLOW;
}
use of java.util.zip.DataFormatException in project commoncrawl-examples by commoncrawl.
the class GzipCompressorInputStream method read.
/**
* {@inheritDoc}
*
* @since 1.1
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (stoppedForEndOfMember || endOfStream) {
return -1;
}
int size = 0;
while (len > 0) {
if (inf.needsInput()) {
// Remember the current position because we may need to
// rewind after reading too much input.
in.mark(buf.length);
bufUsed = in.read(buf);
if (bufUsed == -1) {
throw new EOFException();
}
inf.setInput(buf, 0, bufUsed);
}
int ret;
try {
ret = inf.inflate(b, off, len);
} catch (DataFormatException e) {
throw new IOException("Gzip-compressed data is corrupt");
}
crc.update(b, off, ret);
memberSize += ret;
off += ret;
len -= ret;
size += ret;
count(ret);
if (inf.finished()) {
// We may have read too many bytes. Rewind the read
// position to match the actual amount used.
//
// NOTE: The "if" is there just in case. Since we used
// in.mark earler, it should always skip enough.
in.reset();
int skipAmount = bufUsed - inf.getRemaining();
if (in.skip(skipAmount) != skipAmount) {
throw new IOException();
}
bufUsed = 0;
DataInputStream inData = new DataInputStream(in);
// CRC32
long crcStored = 0;
for (int i = 0; i < 4; ++i) {
crcStored |= (long) inData.readUnsignedByte() << (i * 8);
}
if (crcStored != crc.getValue()) {
throw new IOException("Gzip-compressed data is corrupt " + "(CRC32 error)");
}
// Uncompressed size modulo 2^32 (ISIZE in the spec)
int isize = 0;
for (int i = 0; i < 4; ++i) {
isize |= inData.readUnsignedByte() << (i * 8);
}
if (isize != memberSize) {
throw new IOException("Gzip-compressed data is corrupt" + "(uncompressed size mismatch)");
}
if (!decompressConcatenated) {
stoppedForEndOfMember = true;
}
// See if this is the end of the file.
endOfStream = !init(false);
if (stoppedForEndOfMember || endOfStream) {
return size == 0 ? -1 : size;
}
}
}
return size;
}
use of java.util.zip.DataFormatException in project jetty.project by eclipse.
the class DeflateFrameExtension method incomingFrame.
@Override
public void incomingFrame(Frame frame) {
if (frame.getType().isControl() || !frame.isRsv1() || !frame.hasPayload()) {
nextIncomingFrame(frame);
return;
}
try {
ByteAccumulator accumulator = newByteAccumulator();
decompress(accumulator, frame.getPayload());
decompress(accumulator, TAIL_BYTES_BUF.slice());
forwardIncoming(frame, accumulator);
} catch (DataFormatException e) {
throw new BadPayloadException(e);
}
}
use of java.util.zip.DataFormatException in project jetty.project by eclipse.
the class PerMessageDeflateExtension method incomingFrame.
@Override
public void incomingFrame(Frame frame) {
// Subsequent continuation frames don't have RSV1 set, but are compressed.
if (frame.getType().isData()) {
incomingCompressed = frame.isRsv1();
}
if (OpCode.isControlFrame(frame.getOpCode()) || !incomingCompressed) {
nextIncomingFrame(frame);
return;
}
ByteAccumulator accumulator = newByteAccumulator();
try {
ByteBuffer payload = frame.getPayload();
decompress(accumulator, payload);
if (frame.isFin()) {
decompress(accumulator, TAIL_BYTES_BUF.slice());
}
forwardIncoming(frame, accumulator);
} catch (DataFormatException e) {
throw new BadPayloadException(e);
}
if (frame.isFin())
incomingCompressed = false;
}
use of java.util.zip.DataFormatException in project phonegap-facebook-plugin by Wizcorp.
the class SpdyReader method readNameValueBlock.
private List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
Logger.getLogger(getClass().getName()).warning("numberOfPairs < 0: " + numberOfPairs);
throw ioException("numberOfPairs < 0");
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0)
throw ioException("name.length == 0");
if (values.length() == 0)
throw ioException("values.length == 0");
entries.add(name);
entries.add(values);
}
if (compressedLimit != 0) {
Logger.getLogger(getClass().getName()).warning("compressedLimit > 0: " + compressedLimit);
}
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
Aggregations