List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:org.starmod.net.pipeline.FramingHandler.java
License:MIT License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (in.readableBytes() < 4) return;/*from w w w. j a v a2 s.c o m*/ int size = in.readInt(); if (in.readableBytes() < size) { in.resetReaderIndex(); return; } out.add(in.readBytes(size)); }
From source file:org.waarp.ftp.core.data.handler.FtpDataModeCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // First test if the connection is fully ready (block might be // transfered // by client before connection is ready) if (!isReady) { if (!codecLocked.await(FtpConfiguration.DATATIMEOUTCON)) { throw new InvalidArgumentException("Codec not unlocked while should be"); }/*ww w. j a va 2 s . c om*/ isReady = true; } // If STREAM Mode, no task to do, just next filter if (mode == TransferMode.STREAM) { dataBlock = new DataBlock(); int length = buf.readableBytes(); // Except if RECORD Structure! if (structure == TransferStructure.RECORD) { out.add(decodeRecord(buf, length)); return; } dataBlock.setBlock(buf.readBytes(length)); out.add(dataBlock); return; } else if (mode == TransferMode.BLOCK) { // Now we are in BLOCK Mode // Make sure if the length field was received. if (buf.readableBytes() < 3) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return; } // The length field is in the buffer. // Mark the current buffer position before reading the length field // because the whole frame might not be in the buffer yet. // We will reset the buffer position to the marked position if // there's not enough bytes in the buffer. buf.markReaderIndex(); if (dataBlock == null) { dataBlock = new DataBlock(); } // Read the descriptor dataBlock.setDescriptor(buf.readByte()); // Read the length field. byte upper = buf.readByte(); byte lower = buf.readByte(); dataBlock.setByteCount(upper, lower); // Make sure if there's enough bytes in the buffer. if (buf.readableBytes() < dataBlock.getByteCount()) { // The whole bytes were not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. // Reset to the marked position to read the length field again // next time. buf.resetReaderIndex(); return; } if (dataBlock.getByteCount() > 0) { // There's enough bytes in the buffer. Read it. dataBlock.setBlock(buf.readBytes(dataBlock.getByteCount())); } DataBlock returnDataBlock = dataBlock; // Free the datablock for next frame dataBlock = null; // Successfully decoded a frame. Return the decoded frame. out.add(returnDataBlock); return; } // Type unimplemented throw new InvalidArgumentException("Mode unimplemented: " + mode.name()); }
From source file:org.waarp.openr66.protocol.localhandler.packet.LocalPacketCodec.java
License:Open Source License
public static AbstractLocalPacket decodeNetworkPacket(ByteBuf buf) throws OpenR66ProtocolPacketException { // Mark the current buffer position buf.markReaderIndex(); // Read the length field final int length = buf.readInt(); if (buf.readableBytes() < length) { buf.resetReaderIndex();//w w w . ja va 2 s .c om return null; } // Now we can read the header // Header: Header length field (4 bytes) = Middle length field (4 // bytes), End length field (4 bytes), type field (1 byte), ... final int middleLength = buf.readInt(); final int endLength = buf.readInt(); // check if the packet is complete if (middleLength + endLength + length - 8 > buf.readableBytes()) { buf.resetReaderIndex(); return null; } // createPacketFromByteBuf read the buffer return LocalPacketFactory.createPacketFromByteBuf(length - 8, middleLength, endLength, buf); }
From source file:org.waarp.openr66.protocol.networkhandler.packet.NetworkPacketCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // Make sure if the length field was received. if (buf.readableBytes() < 4) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return;//from ww w . j a v a2 s.c o m } // Mark the current buffer position buf.markReaderIndex(); // Read the length field final int length = buf.readInt(); if (length < 9) { throw new OpenR66ProtocolPacketException( "Incorrect decode first field in Network Packet: " + length + " < 9"); } if (buf.readableBytes() < length) { buf.resetReaderIndex(); return; } // Now we can read the two Ids final int localId = buf.readInt(); final int remoteId = buf.readInt(); final byte code = buf.readByte(); int readerInder = buf.readerIndex(); ByteBuf buffer = buf.slice(readerInder, length - 9); buffer.retain(); buf.skipBytes(length - 9); NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer); if (code == LocalPacketFactory.KEEPALIVEPACKET) { KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec .decodeNetworkPacket(networkPacket.getBuffer()); if (keepAlivePacket.isToValidate()) { keepAlivePacket.validate(); NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL, keepAlivePacket, null); NetworkChannelReference nc = NetworkTransaction.getImmediateNetworkChannel(ctx.channel()); if (nc != null) { nc.useIfUsed(); } ctx.writeAndFlush(response.getNetworkPacket()); buffer.release(); } // Replaced by a NoOp packet networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null); NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last(); nsh.setKeepAlivedSent(); } out.add(networkPacket); }
From source file:org.waarp.openr66.proxy.network.NetworkPacketCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { // Make sure if the length field was received. if (buf.readableBytes() < 4) { // The length field was not received yet - return null. // This method will be invoked again when more packets are // received and appended to the buffer. return;/*from w w w .j a v a 2 s . c om*/ } // Mark the current buffer position buf.markReaderIndex(); // Read the length field final int length = buf.readInt(); if (buf.readableBytes() < length) { buf.resetReaderIndex(); return; } // Now we can read the two Ids // Slight change in Proxy = first is remote and second is local! final int remoteId = buf.readInt(); final int localId = buf.readInt(); final byte code = buf.readByte(); int readerInder = buf.readerIndex(); ByteBuf buffer = buf.slice(readerInder, length - 9); buffer.retain(); buf.skipBytes(length - 9); NetworkPacket networkPacket = new NetworkPacket(localId, remoteId, code, buffer); if (code == LocalPacketFactory.KEEPALIVEPACKET) { KeepAlivePacket keepAlivePacket = (KeepAlivePacket) LocalPacketCodec .decodeNetworkPacket(networkPacket.getBuffer()); if (keepAlivePacket.isToValidate()) { keepAlivePacket.validate(); NetworkPacket response = new NetworkPacket(ChannelUtils.NOCHANNEL, ChannelUtils.NOCHANNEL, keepAlivePacket, null); ctx.writeAndFlush(response.getNetworkPacket()); } // Replaced by a NoOp packet networkPacket = new NetworkPacket(localId, remoteId, new NoOpPacket(), null); NetworkServerHandler nsh = (NetworkServerHandler) ctx.pipeline().last(); nsh.setKeepAlivedSent(); } out.add(networkPacket); }
From source file:org.wso2.carbon.mss.internal.router.InternalHttpResponder.java
License:Open Source License
private InputSupplier<InputStream> createContentSupplier(ByteBuf content) { final ByteBuf responseContent = content.duplicate(); // Have independent pointers. responseContent.markReaderIndex(); return new HttpResponderInputSupplier(responseContent).invoke(); }
From source file:org.wso2.carbon.stream.processor.core.ha.transport.handlers.MessageDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 5) { return;// w ww.ja v a 2 s.c om } int protocol = in.readByte(); int messageSize = in.readInt(); if (protocol != 2 || messageSize > in.readableBytes()) { in.resetReaderIndex(); return; } byte[] bytes = new byte[messageSize]; in.readBytes(bytes); in.markReaderIndex(); in.resetReaderIndex(); try { byteBufferQueue.put(ByteBuffer.wrap(bytes)); } catch (InterruptedException e) { log.error("Error while waiting for the insertion of ByteBufferQueue " + e.getMessage(), e); } if (log.isDebugEnabled()) { synchronized (this) { if (startTime == 0L) { startTime = new Date().getTime(); } count++; if (count % TPS_EVENT_BATCH_THRESHOLD == 0) { endTime = new Date().getTime(); log.info("Server Event Batch TPS: " + (((TPS_EVENT_BATCH_THRESHOLD * 1000) / (endTime - startTime)))); startTime = new Date().getTime(); } } } in.markReaderIndex(); }
From source file:org.wso2.carbon.tcp.transport.util.SiddhiEventConverter.java
License:Open Source License
@Override public List<SiddhiEventComposite> toEventList(Object eventBundle, StreamTypeHolder streamTypeHolder) { ByteBuf byteBuffer = (ByteBuf) eventBundle; int sessionIdSize = byteBuffer.readInt(); byteBuffer.readBytes(new byte[sessionIdSize]); int events = byteBuffer.readInt(); List<SiddhiEventComposite> eventList = new ArrayList<SiddhiEventComposite>(); for (int i = 0; i < events; i++) { int eventSize = byteBuffer.readInt(); byte[] bytes = new byte[eventSize]; byteBuffer.readBytes(bytes);//from ww w. j a v a2 s .c om ByteBuffer eventByteBuffer = ByteBuffer.wrap(bytes); eventList.add(getEvent(eventByteBuffer, streamTypeHolder)); } byteBuffer.markReaderIndex(); return eventList; }
From source file:org.wso2.extension.siddhi.io.tcp.transport.handlers.MessageDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if (in.readableBytes() < 5) { return;/*w w w . j a v a2 s. c om*/ } try { int protocol = in.readByte(); int messageSize = in.readInt(); if (protocol != 2 || messageSize > in.readableBytes()) { in.resetReaderIndex(); return; } flowController.barrier(); int sessionIdSize = in.readInt(); BinaryMessageConverterUtil.getString(in, sessionIdSize); int channelIdSize = in.readInt(); String channelId = BinaryMessageConverterUtil.getString(in, channelIdSize); int dataLength = in.readInt(); byte[] bytes = new byte[dataLength]; in.readBytes(bytes); StreamListener streamListener = streamInfoHolder.getStreamListener(channelId); if (streamListener == null) { in.markReaderIndex(); LOG.error("Events with unknown channelId : '" + channelId + "' hence dropping the events!"); return; } streamListener.onMessage(bytes); } catch (UnsupportedEncodingException e) { LOG.error(e.getMessage(), e); } finally { in.markReaderIndex(); } }
From source file:org.wso2.siddhi.tcp.transport.converter.SiddhiEventConverter.java
License:Open Source License
public static void toConvertToSiddhiEvents(Object eventBundle, StreamTypeHolder streamTypeHolder) { ByteBuf byteBuffer = (ByteBuf) eventBundle; int protocol = byteBuffer.readByte(); int messageSize = byteBuffer.readInt(); if (protocol != 2 || messageSize > byteBuffer.readableBytes()) { byteBuffer.resetReaderIndex();//ww w. ja va 2 s .com return; } int sessionIdSize = byteBuffer.readInt(); String sessionId = BinaryMessageConverterUtil.getString(byteBuffer, sessionIdSize); int streamIdSize = byteBuffer.readInt(); String streamId = BinaryMessageConverterUtil.getString(byteBuffer, streamIdSize); StreamInfo streamInfo = streamTypeHolder.getStreamInfo(streamId); int numberOfEvents = byteBuffer.readInt(); Event[] events = new Event[numberOfEvents]; for (int i = 0; i < numberOfEvents; i++) { int eventSize = byteBuffer.readInt(); byte[] bytes = new byte[eventSize]; byteBuffer.readBytes(bytes); ByteBuffer eventByteBuffer = ByteBuffer.wrap(bytes); events[i] = getEvent(eventByteBuffer, streamInfo.getAttributeTypes()); } byteBuffer.markReaderIndex(); streamInfo.getStreamListener().onEvents(events); }