use of java.util.zip.DataFormatException 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.DataFormatException in project AndroidAsync by koush.
the class HybiParser method emitFrame.
private void emitFrame() throws IOException {
byte[] payload = mask(mPayload, mMask, 0);
if (mDeflated) {
try {
payload = inflate(payload);
} catch (DataFormatException e) {
throw new IOException("Invalid deflated data");
}
}
int opcode = mOpcode;
if (opcode == OP_CONTINUATION) {
if (mMode == 0) {
throw new ProtocolError("Mode was not set.");
}
mBuffer.write(payload);
if (mFinal) {
byte[] message = mBuffer.toByteArray();
if (mMode == MODE_TEXT) {
onMessage(encode(message));
} else {
onMessage(message);
}
reset();
}
} else if (opcode == OP_TEXT) {
if (mFinal) {
String messageText = encode(payload);
onMessage(messageText);
} else {
mMode = MODE_TEXT;
mBuffer.write(payload);
}
} else if (opcode == OP_BINARY) {
if (mFinal) {
onMessage(payload);
} else {
mMode = MODE_BINARY;
mBuffer.write(payload);
}
} else if (opcode == OP_CLOSE) {
int code = (payload.length >= 2) ? 256 * (payload[0] & 0xFF) + (payload[1] & 0xFF) : 0;
String reason = (payload.length > 2) ? encode(slice(payload, 2)) : null;
// Log.d(TAG, "Got close op! " + code + " " + reason);
onDisconnect(code, reason);
} else if (opcode == OP_PING) {
if (payload.length > 125) {
throw new ProtocolError("Ping payload too large");
}
// Log.d(TAG, "Sending pong!!");
String message = encode(payload);
sendFrame(frame(OP_PONG, payload, -1));
onPing(message);
} else if (opcode == OP_PONG) {
String message = encode(payload);
onPong(message);
// Log.d(TAG, "Got pong! " + message);
}
}
use of java.util.zip.DataFormatException in project j2objc by google.
the class OldDataFormatExceptionTest method testDataFormatExceptionString.
public void testDataFormatExceptionString() {
DataFormatException dfe = new DataFormatException("Test");
assertEquals(dfe.getMessage(), "Test");
}
use of java.util.zip.DataFormatException in project robovm by robovm.
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());
}
}
use of java.util.zip.DataFormatException in project undertow by undertow-io.
the class PerMessageDeflateFunction method decompress.
private PooledByteBuffer decompress(WebSocketChannel channel, PooledByteBuffer pooled) throws IOException {
ByteBuffer buffer = pooled.getBuffer();
while (!decompress.needsInput() && !decompress.finished()) {
if (!buffer.hasRemaining()) {
pooled = largerBuffer(pooled, channel, buffer.capacity() * 2);
buffer = pooled.getBuffer();
}
int n;
try {
n = decompress.inflate(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
} catch (DataFormatException e) {
WebSocketLogger.EXTENSION_LOGGER.debug(e.getMessage(), e);
throw WebSocketMessages.MESSAGES.badCompressedPayload(e);
}
buffer.position(buffer.position() + n);
}
return pooled;
}
Aggregations