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.eightkdata.nettybson.mongodriver.MongoBSONDocument.java

License:Open Source License

/**
 * Generates an instance reading from the ByteBuf. Advances the readerIndex of the buffer until the end of the bson
 * @param buffer/*from ww w  .j  a v a 2 s . c  o m*/
 */
public MongoBSONDocument(ByteBuf buffer) {
    buffer.markReaderIndex();
    int documentLength = buffer.readInt();
    buffer.resetReaderIndex();
    byte[] bsonBytes = new byte[documentLength];
    buffer.readBytes(bsonBytes);

    BSONDecoder bsonDecoder = new BasicBSONDecoder();
    bson = bsonDecoder.readObject(bsonBytes);
}

From source file:com.farsunset.cim.sdk.android.filter.ClientMessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {

    /**/*from  ww  w  . j  ava  2 s .  co m*/
     * ?3?
     */
    if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
        return;
    }

    buffer.markReaderIndex();

    buffer.markReaderIndex();

    byte conetnType = buffer.readByte();

    byte lv = buffer.readByte();// int ?
    byte hv = buffer.readByte();// int ?

    int conetnLength = getContentLength(lv, hv);

    // ?????
    if (conetnLength > buffer.readableBytes()) {
        buffer.resetReaderIndex();
        return;
    }

    byte[] dataBytes = new byte[conetnLength];
    buffer.readBytes(dataBytes);

    Object message = mappingMessageObject(dataBytes, conetnType);

    if (message != null) {
        queue.add(message);
    }

}

From source file:com.farsunset.cim.sdk.server.filter.decoder.AppMessageDecoder.java

License:Apache License

@Override
public void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception {

    /**/*w w w  .  j a va  2s  .  c  o  m*/
     * ?3?
     */
    if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) {
        return;
    }

    buffer.markReaderIndex();

    byte conetnType = buffer.readByte();

    byte lv = buffer.readByte();// int ?
    byte hv = buffer.readByte();// int ?

    int conetnLength = getContentLength(lv, hv);

    // ?????
    if (conetnLength <= buffer.readableBytes()) {
        byte[] dataBytes = new byte[conetnLength];
        buffer.readBytes(dataBytes);

        Object message = mappingMessageObject(dataBytes, conetnType);
        if (message != null) {
            arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).set(CIMSession.NATIVEAPP);
            queue.add(message);
            return;
        }
    }

    buffer.resetReaderIndex();
}

From source file:com.farsunset.cim.sdk.server.filter.decoder.WebMessageDecoder.java

License:Apache License

@Override
public void decode(ChannelHandlerContext arg0, ByteBuf iobuffer, List<Object> queue) throws Exception {

    iobuffer.markReaderIndex();/*from  w  w w .  j  av  a  2s.co  m*/

    /**
     * ?fin??1 0 ??
     */
    byte tag = iobuffer.readByte();
    int frameFin = tag > 0 ? 0 : 1; // ?byte ?1 ?0 fin 0  1
    if (frameFin == 0) {
        iobuffer.resetReaderIndex();
        return;
    }

    /**
     * ?protobuf?? OPCODE_BINARY? OPCODE_CLOSE
     */
    int frameOqcode = tag & TAG_MASK;

    if (OPCODE_BINARY == frameOqcode) {

        byte head = iobuffer.readByte();
        byte datalength = (byte) (head & PAYLOADLEN);
        int realLength = 0;

        /**
         * Payload len7?7+16?7+64????? 0-125payload
         * data 1267????2payload data
         * 1277????8payload data
         */
        if (datalength == HAS_EXTEND_DATA) {
            realLength = iobuffer.readShort();
        } else if (datalength == HAS_EXTEND_DATA_CONTINUE) {
            realLength = (int) iobuffer.readLong();
        } else {
            realLength = datalength;
        }

        boolean masked = (head >> 7 & MASK) == 1;
        if (masked) {// ?
            // ??
            byte[] mask = new byte[4];
            iobuffer.readBytes(mask);

            byte[] data = new byte[realLength];
            iobuffer.readBytes(data);
            for (int i = 0; i < realLength; i++) {
                // ??
                data[i] = (byte) (data[i] ^ mask[i % 4]);
            }

            handleMessage(data, queue);
        }

    } else if (OPCODE_CLOSE == frameOqcode) {
        handleClose(arg0);
    } else {
        // ?
        iobuffer.readBytes(new byte[iobuffer.readableBytes()]);
    }

}

From source file:com.farsunset.cim.sdk.server.filter.ServerMessageDecoder.java

License:Apache License

private boolean tryWebsocketHandleHandshake(ChannelHandlerContext arg0, ByteBuf iobuffer, List<Object> queue) {

    iobuffer.markReaderIndex();// w  ww. j  ava 2  s  .c  o  m

    byte[] data = new byte[iobuffer.readableBytes()];
    iobuffer.readBytes(data);

    String request = new String(data);
    String secKey = getSecWebSocketKey(request);
    boolean handShake = secKey != null && Objects.equals(getUpgradeProtocol(request), CIMSession.WEBSOCKET);
    if (handShake) {
        /**
         * ?????HANDSHAKE_FRAME,?session??websocket
         */
        arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).set(CIMSession.WEBSOCKET);

        SentBody body = new SentBody();
        body.setKey(CIMNioSocketAcceptor.WEBSOCKET_HANDLER_KEY);
        body.setTimestamp(System.currentTimeMillis());
        body.put("key", secKey);
        queue.add(body);

    } else {
        iobuffer.resetReaderIndex();
    }

    return handShake;
}

From source file:com.feihong.newzxclient.tcp.IntLengthDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    // Wait until the length prefix is available.
    if (in.readableBytes() < 4) {
        //String strError = String.format("Read length data error, in.readableBytes(): %d, length: %d", in.readableBytes(), 4);
        //writeLog(strError);
        return;/*from www. j a  v  a 2 s . co  m*/
    }

    in.markReaderIndex();

    // Wait until the whole data is available.
    int dataLength = in.readInt();
    if (in.readableBytes() < dataLength) {
        //String strError = String.format("Read length data error, in.readableBytes(): %d, length: %d", in.readableBytes(), dataLength + 4);
        //writeLog(strError);

        in.resetReaderIndex();
        return;
    }

    // Convert the received data into a new BigInteger.
    byte[] decoded = new byte[dataLength];
    in.readBytes(decoded);

    try {
        Msg.CommonMessage commonMessage = Msg.CommonMessage.parseFrom(decoded);
        out.add(commonMessage);
    } catch (InvalidProtocolBufferException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.gemstone.gemfire.internal.redis.ByteToCommandDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    Command c = null;//from   w ww.j  a va2 s  .c om
    do {
        in.markReaderIndex();
        c = parse(in);
        if (c == null) {
            in.resetReaderIndex();
            return;
        }
        out.add(c);
    } while (in.isReadable()); // Try to take advantage of pipelining if it is being used
}

From source file:com.github.jrialland.ajpclient.impl.handlers.AjpMessagesHandler.java

License:Apache License

protected Long readHeaders(final ChannelHandlerContext ctx, final ByteBuf in) throws Exception {
    final int statusCode = in.readUnsignedShort();
    final String statusMessage = readString(in);
    final int numHeaders = in.readUnsignedShort();

    if (getLog().isDebugEnabled()) {
        getLog().debug(" | HTTP/1.1 " + statusCode + " " + statusMessage);
    }//from w ww. j  a  v  a  2 s  . c o  m

    Long expected = null;
    final List<Header> headers = new ArrayList<Header>(numHeaders);

    for (int i = 0; i < numHeaders; i++) {
        in.markReaderIndex();
        final int code = in.readUnsignedShort();
        String headerName = ResponseHeader.getHeader(code);
        if (headerName == null) {
            in.resetReaderIndex();
            headerName = readString(in);
        }
        final String value = readString(in);
        if (getLog().isDebugEnabled()) {
            getLog().debug(" | " + headerName + ": " + value);
        }
        if (headerName.equalsIgnoreCase("Content-Length")) {
            expected = Long.parseLong(value);
        }
        headers.add(new Header(headerName, value));
    }
    getCallback(ctx.channel()).handleSendHeadersMessage(statusCode, statusMessage, headers);
    return expected;
}

From source file:com.github.jrialland.ajpclient.impl.handlers.AjpMessagesHandler.java

License:Apache License

protected String readString(final ByteBuf in) {
    in.markReaderIndex();/*  w w  w  . j  ava 2 s  .com*/
    final int b0 = in.readUnsignedByte();
    if (b0 == 0xff) {
        return null;
    }
    in.resetReaderIndex();
    final int length = in.readUnsignedShort();
    final byte[] data = new byte[length];
    in.readBytes(data);

    // skip trailing \0
    in.readByte();

    return new String(data);
}

From source file:com.github.jrialland.ajpclient.impl.handlers.OutgoingFramesLogger.java

License:Apache License

@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise)
        throws Exception {

    if (msg == null) {
        getLog().debug(">>> (null)");
    } else if (msg instanceof ByteBuf) {

        final ByteBuf buf = (ByteBuf) msg;
        buf.markReaderIndex();// w ww . j  ava2s.  c o  m

        final byte[] data = new byte[buf.readableBytes()];
        buf.readBytes(data);
        getLog().debug(">>> " + bytesToHex(data));

        buf.resetReaderIndex();

    } else {
        getLog().debug(">>> " + msg.toString());
    }

    // pass to the next handler in the chain
    super.write(ctx, msg, promise);
}