Search in sources :

Example 21 with EncoderException

use of io.netty.handler.codec.EncoderException in project CodeChickenLib by Chicken-Bones.

the class PacketCustom method decompress.

/**
 * Decompresses the remaining ByteBuf (after type has been read) using Snappy
 */
private void decompress() {
    Inflater inflater = new Inflater();
    try {
        int len = readInt();
        byte[] out = new byte[len];
        inflater.setInput(array(), readerIndex(), readableBytes());
        inflater.inflate(out);
        clear();
        writeByteArray(out);
    } catch (Exception e) {
        throw new EncoderException(e);
    } finally {
        inflater.end();
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Inflater(java.util.zip.Inflater) EncoderException(io.netty.handler.codec.EncoderException) IOException(java.io.IOException)

Example 22 with EncoderException

use of io.netty.handler.codec.EncoderException in project CodeChickenLib by Chicken-Bones.

the class PacketCustom method do_compress.

/**
 * Compresses the payload ByteBuf after the type byte
 */
private void do_compress() {
    Deflater deflater = new Deflater();
    try {
        readerIndex(1);
        int len = readableBytes();
        deflater.setInput(array(), readerIndex(), len);
        deflater.finish();
        byte[] out = new byte[len];
        int clen = deflater.deflate(out);
        if (// not worth compressing, gets larger
        clen >= len - 5 || !deflater.finished())
            return;
        clear();
        writeByte(type | 0x80);
        writeVarInt(len);
        writeByteArray(out);
    } catch (Exception e) {
        throw new EncoderException(e);
    } finally {
        readerIndex(0);
        deflater.end();
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) Deflater(java.util.zip.Deflater) EncoderException(io.netty.handler.codec.EncoderException) IOException(java.io.IOException)

Example 23 with EncoderException

use of io.netty.handler.codec.EncoderException in project CodeChickenLib by Chicken-Bones.

the class MCDataIO method writeString.

/**
 * PacketBuffer.writeString
 */
public static void writeString(MCDataOutput out, String string) {
    byte[] abyte = string.getBytes(Charsets.UTF_8);
    if (abyte.length > 32767)
        throw new EncoderException("String too big (was " + string.length() + " bytes encoded, max " + 32767 + ")");
    out.writeVarInt(abyte.length);
    out.writeArray(abyte);
}
Also used : EncoderException(io.netty.handler.codec.EncoderException)

Example 24 with EncoderException

use of io.netty.handler.codec.EncoderException in project netty by netty.

the class LengthFieldPrependerTest method testAdjustedLengthLessThanZero.

@Test
public void testAdjustedLengthLessThanZero() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -2));
    try {
        ch.writeOutbound(msg);
        fail(EncoderException.class.getSimpleName() + " must be raised.");
    } catch (EncoderException e) {
    // Expected
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) Test(org.junit.jupiter.api.Test)

Example 25 with EncoderException

use of io.netty.handler.codec.EncoderException in project netty by netty.

the class HttpContentCompressorTest method testTooManyResponses.

@Test
public void testTooManyResponses() throws Exception {
    FullHttpRequest request = newRequest();
    EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
    ch.writeInbound(request);
    ch.writeOutbound(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER));
    try {
        ch.writeOutbound(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER));
        fail();
    } catch (EncoderException e) {
        assertTrue(e.getCause() instanceof IllegalStateException);
    }
    assertTrue(ch.finish());
    for (; ; ) {
        Object message = ch.readOutbound();
        if (message == null) {
            break;
        }
        ReferenceCountUtil.release(message);
    }
    for (; ; ) {
        Object message = ch.readInbound();
        if (message == null) {
            break;
        }
        ReferenceCountUtil.release(message);
    }
}
Also used : EncoderException(io.netty.handler.codec.EncoderException) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Aggregations

EncoderException (io.netty.handler.codec.EncoderException)26 ByteBuffer (org.lanternpowered.server.network.buffer.ByteBuffer)9 ByteBuf (io.netty.buffer.ByteBuf)6 IOException (java.io.IOException)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 Test (org.junit.jupiter.api.Test)3 Message (org.lanternpowered.server.network.message.Message)3 CodecException (io.netty.handler.codec.CodecException)2 Deflater (java.util.zip.Deflater)2 Inflater (java.util.zip.Inflater)2 MessageRegistration (org.lanternpowered.server.network.message.MessageRegistration)2 Codec (org.lanternpowered.server.network.message.codec.Codec)2 CodecRegistration (com.flowpowered.network.Codec.CodecRegistration)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 DecoderResult (io.netty.handler.codec.DecoderResult)1 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)1 MessageToMessageCodec (io.netty.handler.codec.MessageToMessageCodec)1 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)1