Search in sources :

Example 46 with DataFormatException

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();
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with DataFormatException

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);
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException)

Example 48 with DataFormatException

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");
}
Also used : DataFormatException(java.util.zip.DataFormatException)

Example 49 with DataFormatException

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());
    }
}
Also used : DataFormatException(java.util.zip.DataFormatException) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 50 with DataFormatException

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;
}
Also used : DataFormatException(java.util.zip.DataFormatException) ByteBuffer(java.nio.ByteBuffer) ImmediatePooledByteBuffer(io.undertow.util.ImmediatePooledByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Aggregations

DataFormatException (java.util.zip.DataFormatException)71 IOException (java.io.IOException)32 Inflater (java.util.zip.Inflater)29 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 ByteBuffer (java.nio.ByteBuffer)9 ArrayList (java.util.ArrayList)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)5 File (java.io.File)4 BufferedOutputStream (java.io.BufferedOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 ImageException (cbit.image.ImageException)2 ImageDataset (cbit.vcell.VirtualMicroscopy.ImageDataset)2 AsynchClientTask (cbit.vcell.client.task.AsynchClientTask)2 BaseMediaHeader (cbit.vcell.export.gloworm.atoms.BaseMediaHeader)2 BaseMediaInformation (cbit.vcell.export.gloworm.atoms.BaseMediaInformation)2 HandlerReference (cbit.vcell.export.gloworm.atoms.HandlerReference)2 VideoMediaInformation (cbit.vcell.export.gloworm.atoms.VideoMediaInformation)2 GeometryException (cbit.vcell.geometry.GeometryException)2