List of usage examples for io.netty.buffer ByteBuf skipBytes
public abstract ByteBuf skipBytes(int length);
From source file:org.graylog2.inputs.transports.netty.LenientLineBasedFrameDecoder.java
License:Open Source License
/** * Create a frame out of the {@link ByteBuf} and return it. * * @param ctx the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to * @param buffer the {@link ByteBuf} from which to read data * @return frame the {@link ByteBuf} which represent the frame or {@code null} if no frame could * be created./*from w w w.j a va 2s .c o m*/ */ protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception { final int eol = findEndOfLine(buffer); if (!discarding) { if (eol < 0 && emitLastLineWithoutDelimiter && !ctx.channel().isActive()) { final ByteBuf frame; final int length = buffer.readableBytes(); if (length > maxLength) { buffer.readerIndex(length); fail(ctx, length); return null; } frame = buffer.readRetainedSlice(length); return frame; } else if (eol >= 0) { final ByteBuf frame; final int length = eol - buffer.readerIndex(); final int delimLength = buffer.getByte(eol) == '\r' ? 2 : 1; if (length > maxLength) { buffer.readerIndex(eol + delimLength); fail(ctx, length); return null; } if (stripDelimiter) { frame = buffer.readRetainedSlice(length); buffer.skipBytes(delimLength); } else { frame = buffer.readRetainedSlice(length + delimLength); } return frame; } else { final int length = buffer.readableBytes(); if (length > maxLength) { discardedBytes = length; buffer.readerIndex(buffer.writerIndex()); discarding = true; offset = 0; if (failFast) { fail(ctx, "over " + discardedBytes); } } return null; } } else { if (eol >= 0) { final int length = discardedBytes + eol - buffer.readerIndex(); final int delimLength = buffer.getByte(eol) == '\r' ? 2 : 1; buffer.readerIndex(eol + delimLength); discardedBytes = 0; discarding = false; if (!failFast) { fail(ctx, length); } } else { discardedBytes += buffer.readableBytes(); buffer.readerIndex(buffer.writerIndex()); } return null; } }
From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, DatagramPacket packet, List<Object> out) throws Exception { long start = System.currentTimeMillis(); ByteBuf content = packet.content(); List<Part> parts = new ArrayList<>(100); for (;;) {//from w w w . j a v a 2 s .c o m if (!hasReadableBytes(content, 4)) { break; } short partTypeId = content.readShort(); PartType partType = PartType.findById(partTypeId); int partLength = content.readUnsignedShort(); int valueLength = partLength - 4; if (!hasReadableBytes(content, valueLength)) { break; } if (partType == null) { content.skipBytes(valueLength); continue; } Part part; switch (partType) { case HOST: case PLUGIN: case PLUGIN_INSTANCE: case TYPE: case INSTANCE: part = new StringPart(partType, readStringPartContent(content, valueLength)); break; case TIME: case TIME_HIGH_RESOLUTION: case INTERVAL: case INTERVAL_HIGH_RESOLUTION: part = new NumericPart(partType, readNumericPartContent(content)); break; case VALUES: part = new ValuePart(partType, readValuePartContent(content, valueLength)); break; default: part = null; content.skipBytes(valueLength); } //noinspection ConstantConditions if (part != null) { logger.trace("Decoded part: {}", part); parts.add(part); } } if (logger.isTraceEnabled()) { long stop = System.currentTimeMillis(); logger.trace("Decoded datagram in {} ms", stop - start); } if (parts.size() > 0) { CollectdPacket collectdPacket = new CollectdPacket(parts); out.add(collectdPacket); } else { logger.debug("No parts decoded, no CollectdPacket output"); } }
From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java
License:Apache License
private String readStringPartContent(ByteBuf content, int length) { String string = content.toString(content.readerIndex(), length - 1 /* collectd strings are \0 terminated */, CharsetUtil.US_ASCII);//from w ww.j a v a 2 s.c om content.skipBytes(length); // the previous call does not move the readerIndex return string; }
From source file:org.hawkular.metrics.clients.ptrans.collectd.packet.CollectdPacketDecoder.java
License:Apache License
private Values readValuePartContent(ByteBuf content, int length) { int beginIndex = content.readerIndex(); int total = content.readUnsignedShort(); List<DataType> dataTypes = new ArrayList<>(total); for (int i = 0; i < total; i++) { byte sampleTypeId = content.readByte(); dataTypes.add(DataType.findById(sampleTypeId)); }/*from w w w . j av a2s. co m*/ List<Number> data = new ArrayList<>(total); for (DataType dataType : dataTypes) { switch (dataType) { case COUNTER: case ABSOLUTE: byte[] valueBytes = new byte[8]; content.readBytes(valueBytes); data.add(new BigInteger(1, valueBytes)); break; case DERIVE: data.add(content.readLong()); break; case GAUGE: data.add(Double.longBitsToDouble(ByteBufUtil.swapLong(content.readLong()))); break; default: logger.debug("Skipping unknown data type: {}", dataType); } } // Skip any additionnal bytes int readCount = content.readerIndex() - beginIndex; if (length > readCount) { content.skipBytes(readCount - length); } return new Values(dataTypes, data); }
From source file:org.hornetq.amqp.dealer.util.ProtonTrio.java
License:Apache License
/** * this method will change the readerIndex on bytes to the latest read position *//*from www . j a va 2s .c o m*/ public void pump(ByteBuf bytes) { if (bytes.readableBytes() < 8) { return; } try { synchronized (lock) { while (bytes.readableBytes() > 0) { int capacity = transport.capacity(); if (capacity > 0) { ByteBuffer tail = transport.tail(); int min = Math.min(capacity, bytes.readableBytes()); tail.limit(min); bytes.readBytes(tail); transport.process(); checkSASL(); dispatch(); } else { if (capacity == 0) { System.out.println("abandoning: " + bytes.readableBytes()); } else { System.out.println("transport closed, discarding: " + bytes.readableBytes()); } bytes.skipBytes(bytes.readableBytes()); } } } } finally { // After everything is processed we still need to check for more dispatches! dispatch(); } }
From source file:org.jfxvnc.net.rfb.codec.decoder.BellDecoder.java
License:Apache License
@Override public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable(1)) { return false; }/*from w w w.j av a 2s . c om*/ in.skipBytes(1); out.add(new BellEvent()); return true; }
From source file:org.jfxvnc.net.rfb.codec.decoder.ColourMapEntriesDecoder.java
License:Apache License
@Override public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (colorBuf == null) { if (!in.isReadable(12)) { return false; }//from w w w . j a va2 s .c om in.skipBytes(2); firstColor = in.readUnsignedShort(); numberOfColor = in.readUnsignedShort(); int size = numberOfColor - firstColor; colorBuf = Unpooled.buffer(size * 6, size * 6); } colorBuf.writeBytes(in); if (!colorBuf.isWritable()) { return out.add(new ColourMapEntriesEvent(firstColor, numberOfColor, colorBuf)); } return false; }
From source file:org.jfxvnc.net.rfb.codec.decoder.FramebufferUpdateRectDecoder.java
License:Apache License
@Override public boolean decode(ChannelHandlerContext ctx, ByteBuf m, List<Object> out) throws Exception { if (state == State.INIT) { logger.debug("init readable {} bytes", m.readableBytes()); if (!m.isReadable()) { return false; }/*from ww w . jav a 2 s . c o m*/ if (m.getByte(0) != ServerEvent.FRAMEBUFFER_UPDATE.getType()) { logger.error("no FBU type!!! {}", m.getByte(0)); ctx.pipeline().fireChannelReadComplete(); return false; } if (!m.isReadable(4)) { return false; } m.skipBytes(2); // padding numberRects = m.readUnsignedShort(); currentRect = 0; logger.debug("number of rectangles: {}", numberRects); if (numberRects < 1) { return true; } state = State.NEW_RECT; } if (state == State.NEW_RECT) { if (!readRect(ctx, m, out)) { return false; } state = State.READ_RECT; } FrameRectDecoder dec = frameRectDecoder.get(rect.getEncoding()); if (dec == null) { throw new ProtocolException("Encoding not supported: " + rect.getEncoding()); } dec.setRect(rect); if (!dec.decode(ctx, m, out)) { return false; } if (currentRect == numberRects) { state = State.INIT; ctx.pipeline().fireUserEventTriggered(ProtocolState.FBU_REQUEST); return true; } if (!readRect(ctx, m, out)) { state = State.NEW_RECT; } return false; }
From source file:org.jfxvnc.net.rfb.codec.decoder.FrameDecoderHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable()) { return;/*w w w .j a v a 2 s . c o m*/ } FrameDecoder decoder; switch (state) { case NEXT: serverEvent = ServerEvent.valueOf(in.getUnsignedByte(0)); decoder = frameDecoder.get(serverEvent); if (decoder == null) { logger.error("not handled server message type: {} ({})", serverEvent, in.getUnsignedByte(0)); in.skipBytes(in.readableBytes()); return; } if (!decoder.decode(ctx, in, out)) { state = State.FRAME; } case FRAME: decoder = frameDecoder.get(serverEvent); if (decoder == null) { logger.error("not handled server message type: {} ({})", serverEvent, in.getUnsignedByte(0)); in.skipBytes(in.readableBytes()); return; } if (decoder.decode(ctx, in, out)) { state = State.NEXT; } break; default: logger.warn("unknown state: {}", state); break; } }
From source file:org.jfxvnc.net.rfb.codec.decoder.ServerCutTextDecoder.java
License:Apache License
@Override public boolean decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (!in.isReadable(8)) { return false; }//from w w w.ja v a2 s .c om in.markReaderIndex(); in.skipBytes(4); int length = in.readInt(); if (!in.isReadable(length)) { in.resetReaderIndex(); return false; } byte[] text = new byte[length]; in.readBytes(text); out.add(new ServerCutTextEvent(new String(text, StandardCharsets.ISO_8859_1))); return true; }