List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:com.dempe.chat.common.mqtt.codec.MQTTDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (!Utils.checkHeaderAvailability(in)) { in.resetReaderIndex();// w w w . j av a 2s .com return; } in.resetReaderIndex(); byte messageType = Utils.readMessageType(in); DemuxDecoder decoder = m_decoderMap.get(messageType); if (decoder == null) { throw new CorruptedFrameException("Can't find any suitable decoder for message type: " + messageType); } decoder.decode(ctx, in, out); }
From source file:com.dingwang.rpc.decode.RpcDecoder.java
License:Open Source License
@Override public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return;// w w w. j a v a 2 s . c om } in.markReaderIndex(); int dataLength = in.readInt(); if (dataLength < 0) { ctx.close(); } if (in.readableBytes() < dataLength) { in.resetReaderIndex(); } byte[] data = new byte[dataLength]; in.readBytes(data); Object obj = SerializationUtil.deserialize(data, genericClass); out.add(obj); }
From source file:com.dinstone.jrpc.transport.netty4.TransportProtocolDecoder.java
License:Apache License
private byte[] readFrame(ByteBuf in) { if (in.readableBytes() > 4) { in.markReaderIndex(); int len = in.readInt(); if (len > maxObjectSize) { throw new IllegalStateException( "The encoded object is too big: " + len + " (> " + maxObjectSize + ")"); } else if (len < 1) { throw new IllegalStateException("The encoded object is too small: " + len + " (< 1)"); }// w ww . j ava2 s . c o m if (in.readableBytes() < len) { in.resetReaderIndex(); return null; } byte[] rpcBytes = new byte[len]; in.readBytes(rpcBytes); return rpcBytes; } return null; }
From source file:com.doctor.netty5.example.factorial_algorithm.BigIntegerDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { // ? ?F + + -> 1 + 4 + ?? // ??//* ww w. jav a 2s . c o m*/ if (in.readableBytes() < 5) { return; // F + } in.markReaderIndex(); short magicNumber = in.readUnsignedByte();// ?F if (magicNumber != 'F') { in.resetReaderIndex(); throw new RuntimeException("magicNumber must be 'F' "); } // ??? int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } byte[] data = new byte[dataLength]; in.readBytes(data); out.add(new BigInteger(data)); }
From source file:com.dwarf.netty.guide.factorial.BigIntegerDecoder.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() < 5) { return;//from ww w. ja va 2s .c om } in.markReaderIndex(); // Check the magic number. int magicNumber = in.readUnsignedByte(); if (magicNumber != 'F') { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid magic number: " + magicNumber); } // Wait until the whole data is available. int dataLength = in.readInt(); if (in.readableBytes() < dataLength) { in.resetReaderIndex(); return; } // Convert the received data into a new BigInteger. byte[] decoded = new byte[dataLength]; in.readBytes(decoded); out.add(new BigInteger(decoded)); }
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 . co 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 { /**// w w w. j a va 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 v a 2 s . 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 ww w.j a v a 2s . com*/ * ?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(); byte[] data = new byte[iobuffer.readableBytes()]; iobuffer.readBytes(data);/*w ww . jav a 2 s. co m*/ 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; }