Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonBinary readBinary(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    int length = byteBuf.readInt();
    byte subtype = byteBuf.readByte();
    byte[] content = new byte[length];

    byteBuf.readBytes(content);

    return new ByteArrayBsonBinary(ParsingTools.getBinarySubtype(subtype), subtype, content);
}

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonDbPointer readDbPointer(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    String str = getStringReader().readString(byteBuf, false);

    byte[] bytes = new byte[12];
    byteBuf.readBytes(bytes);

    return new DefaultBsonDbPointer(str, new ByteArrayBsonObjectId(bytes));
}

From source file:com.eightkdata.mongowp.bson.netty.DefaultNettyBsonLowLevelReader.java

License:Open Source License

@Override
BsonObjectId readObjectId(@Loose @ModifiesIndexes ByteBuf byteBuf) {
    byte[] bytes = new byte[12];
    byteBuf.readBytes(bytes);

    return new ByteArrayBsonObjectId(bytes);
}

From source file:com.eightkdata.mongowp.mongoserver.util.ByteBufUtil.java

License:Open Source License

/**
 * A method that reads a C-string from a ByteBuf.
 * This method modified the internal state of the ByteBuf, advancing the read pointer to the position after the
 * cstring.// w w  w.java 2 s  . co  m
 *
 * @param buffer
 * @return The C-String as a String object or null if there was no C-String in the ByteBuf
 */
public static String readCString(ByteBuf buffer) {
    int pos = buffer.bytesBefore(CSTRING_BYTE_TERMINATION);
    if (pos == -1) {
        return null;
    }
    byte[] bytes = new byte[pos];
    buffer.readBytes(bytes);
    buffer.readByte(); // Discard the termination byte

    return new String(bytes, Charsets.UTF_8);
}

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//w  w  w .j a  v  a2  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 w w w  .ja  v a2  s  . c  o  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 {

    /**//www . j ava2  s . c  om
     * ?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();/* w  w  w .  j  a  v a  2  s  .c o  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 a  v  a 2  s.  com*/

    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   ww w  .java  2  s .  c o 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();
    }

}