Example usage for io.netty.buffer ByteBuf resetReaderIndex

List of usage examples for io.netty.buffer ByteBuf resetReaderIndex

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf resetReaderIndex.

Prototype

public abstract ByteBuf resetReaderIndex();

Source Link

Document

Repositions the current readerIndex to the marked readerIndex in this buffer.

Usage

From source file:com.mastfrog.acteur.server.EventImpl.java

License:Open Source License

@Override
public <T> T getContentAsJSON(Class<T> type) throws IOException {
    // Special handling for strings
    if (type == String.class || type == CharSequence.class) {
        String result = Streams.readString(new ByteBufInputStream(getContent()));
        if (result.length() > 0 && result.charAt(0) == '"') {
            result = result.substring(1);
        }/*w  w w.j  a  v  a  2 s  . c o  m*/
        if (result.length() > 1 && result.charAt(result.length() - 1) == '"') {
            result = result.substring(0, result.length() - 2);
        }
        return (T) result;
    }
    ByteBuf content = getContent();
    try {
        return codec.readValue(new ByteBufInputStream(content), type);
    } finally {
        content.resetReaderIndex();
    }
}

From source file:com.mastfrog.netty.http.client.ResponseHandler.java

License:Open Source License

protected void internalReceive(HttpResponseStatus status, HttpHeaders headers, ByteBuf content) {
    try {//from w ww.j a  v a2  s.c  o m
        if (status.code() > 399) {
            byte[] b = new byte[content.readableBytes()];
            content.readBytes(b);
            onErrorResponse(status, headers, new String(b, CharsetUtil.UTF_8));
            return;
        }
        if (type == ByteBuf.class) {
            _doReceive(status, headers, type.cast(content));
        } else if (type == String.class || type == CharSequence.class) {
            byte[] b = new byte[content.readableBytes()];
            content.readBytes(b);
            _doReceive(status, headers, type.cast(new String(b, CharsetUtil.UTF_8)));
        } else if (type == byte[].class) {
            byte[] b = new byte[content.readableBytes()];
            content.readBytes(b);
            _doReceive(status, headers, type.cast(b));
        } else {
            byte[] b = new byte[content.readableBytes()];
            content.readBytes(b);
            try {
                Object o = mapper.readValue(b, type);
                _doReceive(status, headers, type.cast(o));
            } catch (JsonParseException ex) {
                content.resetReaderIndex();
                try {
                    String s = Streams.readString(new ByteBufInputStream(content), "UTF-8");
                    onErrorResponse(HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE, headers, s);
                } catch (IOException ex1) {
                    Exceptions.chuck(ex1);
                }
            } catch (Exception ex) {
                Exceptions.chuck(ex);
            }
        }
    } finally {
        latch.countDown();
    }
}

From source file:com.mastfrog.scamper.codec.RawMessageCodec.java

License:Open Source License

@Override
public MessageTypeAndBuffer decode(ByteBuf buf, ChannelHandlerContext ctx, int sctpChannel) {
    byte first = buf.readByte();
    if (first == magicNumber()) {
        MessageType messageType = messageTypes.forByteBuf(buf);
        return new MessageTypeAndBuffer(messageType, buf, sctpChannel);
    }/*from w w w. jav  a 2  s. c  o  m*/
    return new MessageTypeAndBuffer(MessageType.createUnknown(-1, -1), buf.resetReaderIndex(), sctpChannel);
}

From source file:com.mastfrog.scamper.compression.CompressingCodec.java

License:Open Source License

@Override
public MessageTypeAndBuffer decode(ByteBuf buf, ChannelHandlerContext ctx, int sctpChannel) {
    byte magic = buf.readByte();
    if (magic == magicNumber()) {
        try {/*from   w w w. j  a  v  a  2  s  . c  om*/
            return decodeImpl(buf, ctx, sctpChannel);
        } catch (Exception ex) {
            return Exceptions.chuck(ex);
        }
    } else {
        buf.resetReaderIndex();
        return raw.decode(buf, ctx, sctpChannel);
    }
}

From source file:com.mastfrog.scamper.MessageTypeRegistry.java

License:Open Source License

/**
 * Decode a message type from the <i>current position</i> of the
 * passed ByteBuf./*from   w w  w . j a  v  a 2  s .  c  om*/
 * @param buf
 * @return 
 */
public MessageType forByteBuf(ByteBuf buf) {
    if (buf.readableBytes() >= 2) {
        byte one = buf.readByte();
        byte two = buf.readByte();
        for (MessageType mt : types) {
            if (mt.match(one, two)) {
                buf.discardReadBytes();
                return mt;
            }
        }
        return MessageType.createUnknown(one, two);
    }
    buf.resetReaderIndex();
    return MessageType.createUnknown((byte) 0, (byte) 0);
}

From source file:com.mastfrog.scamper.password.crypto.EncryptingCodecTest.java

License:Open Source License

@Test
public void test(MessageCodec codec, MessageTypeRegistry reg) throws Throwable {
    for (int i = 0; i < 10; i++) {
        byte[] add = new byte[i + 1];
        Arrays.fill(add, (byte) 'a');
        String testData = "ABCD 123 Hello world I have some trees in my elbow and you are blue"
                + new String(add);
        ByteBuf toEncode = bufFor(testData);
        assertEquals(testData, dataFrom(toEncode));
        ByteBuf encoded = codec.encode(TYPE, toEncode, fakeChannel());
        String enc = dataFrom(encoded);
        encoded.resetReaderIndex();
        System.out.println("ENCODED TO " + enc + " bytes " + enc.length());
        assertEquals(0, encoded.readerIndex());

        MessageTypeAndBuffer result = codec.decode(encoded, fakeChannelContext(), 0);
        assertEquals(TYPE, result.messageType);
        assertEquals(testData, dataFrom(result.buf));
    }/*from  w w  w.j a v  a  2s  .  co  m*/
}

From source file:com.mobius.software.android.iotbroker.mqtt.net.MQDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    ByteBuf nextHeader = null;/* w w w .j  a va2s . co  m*/
    do {
        if (buf.readableBytes() > 1)
            nextHeader = MQParser.next(buf);

        if (nextHeader != null) {
            buf.readBytes(nextHeader, nextHeader.capacity());
            try {
                MQMessage header = MQParser.decode(nextHeader);
                out.add(header);
            } catch (Exception e) {
                buf.resetReaderIndex();
                ctx.channel().pipeline().remove(this);
                throw e;
            } finally {
                nextHeader.release();
            }
        }
    } while (buf.readableBytes() > 1 && nextHeader != null);
}

From source file:com.mobius.software.android.iotbroker.mqtt.parser.MQParser.java

License:Open Source License

public static ByteBuf next(ByteBuf buf) throws MalformedMessageException {
    buf.markReaderIndex();/*  w  w  w. ja v a 2s  .  c  om*/
    MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf));

    switch (type) {
    case PINGREQ:
    case PINGRESP:
    case DISCONNECT:
        buf.resetReaderIndex();
        return Unpooled.buffer(2);
    default:
        LengthDetails length = decodeLength(buf);
        buf.resetReaderIndex();
        if (length.getLength() == 0)
            return null;
        int result = length.getLength() + length.getSize() + 1;
        return result <= buf.readableBytes() ? Unpooled.buffer(result) : null;
    }
}

From source file:com.mobius.software.mqtt.parser.MQParser.java

License:Open Source License

public static ByteBuf next(ByteBuf buf, int maxMessageSize) throws MalformedMessageException {
    buf.markReaderIndex();/*from  w  w w . ja  v  a  2s.  c om*/
    MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf));

    if (type == null) {
        buf.resetReaderIndex();
        throw new MalformedMessageException("invalid message type decoding");
    }

    switch (type) {
    case PINGREQ:
    case PINGRESP:
    case DISCONNECT:
        buf.resetReaderIndex();
        return Unpooled.buffer(2);
    default:
        LengthDetails length = LengthDetails.decode(buf);
        buf.resetReaderIndex();
        if (length.getLength() == 0)
            return null;
        int result = length.getLength() + length.getSize() + 1;
        if (result > buf.readableBytes())
            throw new MalformedMessageException("invalid length decoding for " + type + " result length:"
                    + result + ", in buffer:" + buf.readableBytes());
        if (result > maxMessageSize)
            throw new MalformedMessageException("message length exceeds limit " + maxMessageSize);
        return Unpooled.buffer(result);
    }
}

From source file:com.mobius.software.mqtt.performance.controller.net.Decoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) {
    ByteBuf nextHeader = null;/*from w w  w . j  a v  a  2s.c  om*/
    do {
        if (buf.readableBytes() > 1) {
            try {
                nextHeader = MQParser.next(buf);
            } catch (MalformedMessageException | IndexOutOfBoundsException ex) {
                buf.resetReaderIndex();
                if (nextHeader != null)
                    nextHeader = null;
            }
        }

        if (nextHeader != null) {
            buf.readBytes(nextHeader, nextHeader.capacity());
            try {
                MQMessage header = MQParser.decode(nextHeader);
                out.add(header);
            } catch (Exception e) {
                buf.resetReaderIndex();
                ctx.channel().pipeline().remove(this);
                throw e;
            } finally {
                nextHeader.release();
            }
        }
    } while (buf.readableBytes() > 1 && nextHeader != null);
}