List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice(int index, int length);
From source file:org.graylog.plugins.netflow.utils.ByteBufUtils.java
License:Apache License
public static InetAddress getInetAddress(final ByteBuf buf, final int offset, final int length) { final ByteBuf buffer = buf.slice(offset, length); final byte[] data = new byte[length]; for (int i = 1; i <= length; i++) { data[i - 1] = (byte) buffer.readUnsignedByte(); }/*from ww w.j a va 2 s .co m*/ InetAddress address; try { address = InetAddress.getByAddress(data); } catch (UnknownHostException e) { address = DEFAULT_INET_ADDRESS; } return address; }
From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception { final int frameSizeValueLength = findFrameSizeValueLength(buffer); // We have not found the frame length value byte size yet. if (frameSizeValueLength <= 0) { return;//from w w w. j av a2s . c om } // Convert the frame length value bytes into an integer without mutating the buffer reader index. final String lengthString = buffer.slice(buffer.readerIndex(), frameSizeValueLength) .toString(StandardCharsets.UTF_8); final int length = Integer.parseInt(lengthString); final int skipLength = frameSizeValueLength + 1; // Frame length value bytes and the whitespace that follows it. // We have to take the skipped bytes (frame size value length + whitespace) into account when checking if // the buffer has enough data to read the complete message. if (buffer.readableBytes() - skipLength < length) { // We cannot read the complete frame yet. return; } else { // Skip the frame length value bytes and the whitespace that follows it. buffer.skipBytes(skipLength); } final ByteBuf frame = buffer.readRetainedSlice(length); out.add(frame); }
From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java
License:Open Source License
/** * Creates a new instance./* w ww . j a v a 2 s.co m*/ * * @param maxFrameLength the maximum length of the decoded frame. * A {@link TooLongFrameException} is thrown if * the length of the frame exceeds this value. * @param stripDelimiter whether the decoded frame should strip out the * delimiter or not * @param failFast If <tt>true</tt>, a {@link TooLongFrameException} is * thrown as soon as the decoder notices the length of the * frame will exceed <tt>maxFrameLength</tt> regardless of * whether the entire frame has been read. * If <tt>false</tt>, a {@link TooLongFrameException} is * thrown after the entire frame that exceeds * <tt>maxFrameLength</tt> has been read. * @param delimiter the delimiter */ public LenientDelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, ByteBuf delimiter) { this(maxFrameLength, stripDelimiter, failFast, true, delimiter.slice(delimiter.readerIndex(), delimiter.readableBytes())); }
From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java
License:Open Source License
/** * Creates a new instance.// www . ja va 2 s.co m * * @param maxFrameLength the maximum length of the decoded frame. * A {@link TooLongFrameException} is thrown if * the length of the frame exceeds this value. * @param stripDelimiter whether the decoded frame should strip out the * delimiter or not * @param failFast If <tt>true</tt>, a {@link TooLongFrameException} is * thrown as soon as the decoder notices the length of the * frame will exceed <tt>maxFrameLength</tt> regardless of * whether the entire frame has been read. * If <tt>false</tt>, a {@link TooLongFrameException} is * thrown after the entire frame that exceeds * <tt>maxFrameLength</tt> has been read. * @param emitLastLineWithoutDelimiter emit the last line even if it doesn't * end with the delimiter * @param delimiters the delimiters */ public LenientDelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast, boolean emitLastLineWithoutDelimiter, ByteBuf... delimiters) { validateMaxFrameLength(maxFrameLength); if (delimiters == null) { throw new NullPointerException("delimiters"); } if (delimiters.length == 0) { throw new IllegalArgumentException("empty delimiters"); } if (isLineBased(delimiters) && !isSubclass()) { lineBasedDecoder = new LenientLineBasedFrameDecoder(maxFrameLength, stripDelimiter, failFast, emitLastLineWithoutDelimiter); this.delimiters = null; } else { this.delimiters = new ByteBuf[delimiters.length]; for (int i = 0; i < delimiters.length; i++) { ByteBuf d = delimiters[i]; validateDelimiter(d); this.delimiters[i] = d.slice(d.readerIndex(), d.readableBytes()); } lineBasedDecoder = null; } this.maxFrameLength = maxFrameLength; this.stripDelimiter = stripDelimiter; this.failFast = failFast; this.emitLastLineWithoutDelimiter = emitLastLineWithoutDelimiter; }
From source file:org.helios.octo.util.NettyUtil.java
License:Open Source License
public static String formatBuffer(ByteBuf buffer, int maxLength) { if (buffer == null || buffer.readableBytes() < 1) return ""; return formatBuffer( buffer.slice(0, buffer.readableBytes() > maxLength ? maxLength : buffer.readableBytes())); }
From source file:org.legacy.network.session.impl.OnDemandSession.java
License:Open Source License
public void processFileQueue() { OnDemandRequestMessage request;//from w w w. ja va 2 s .com synchronized (fileQueue) { request = fileQueue.pop(); if (fileQueue.isEmpty()) { idle = true; } else { filter.addPendingSession(this); idle = false; } } if (request != null) { int type = request.getType(); int file = request.getFile(); Cache cache = Legacy.getCache(); ByteBuf buffer; try { if (type == 0xFF && file == 0xFF) { buffer = Unpooled.wrappedBuffer(Legacy.getCache().getChecksumtable()); } else { buffer = Unpooled.wrappedBuffer(cache.getStore().read(type, file)); if (type != 255) buffer = buffer.slice(0, buffer.readableBytes() - 2); } channel.write(new OnDemandResponseMessage(request.isPriority(), type, file, buffer)); } catch (IOException ex) { LegacyLogger.fireErrorMessage(this, "Failed to service file request " + type + ", " + file + ".", ex); } } }
From source file:org.opendaylight.openflowjava.protocol.impl.clients.SimpleClientFramer.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception { if (bb.readableBytes() < LENGTH_OF_HEADER) { LOGGER.debug("skipping bb - too few data for header: " + bb.readableBytes()); return;/*from www. j av a 2 s.c o m*/ } int length = bb.getUnsignedShort(LENGTH_INDEX_IN_HEADER); if (bb.readableBytes() < length) { LOGGER.debug("skipping bb - too few data for msg: " + bb.readableBytes() + " < " + length); return; } LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(1)); ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length); list.add(messageBuffer); messageBuffer.retain(); bb.skipBytes(length); }
From source file:org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClientFramer.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext chc, DatagramPacket msg, List<Object> list) throws Exception { ByteBuf bb = msg.content(); if (bb.readableBytes() < LENGTH_OF_HEADER) { LOG.debug("skipping bb - too few data for header: {}", bb.readableBytes()); return;/* www. j av a 2 s . c o m*/ } int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER); if (bb.readableBytes() < length) { LOG.debug("skipping bb - too few data for msg: {} < {}", bb.readableBytes(), length); return; } LOG.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1)); ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length); list.add(messageBuffer); messageBuffer.retain(); bb.skipBytes(length); }
From source file:org.opendaylight.openflowjava.protocol.impl.core.OFFrameDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception { int readableBytes = bb.readableBytes(); if (readableBytes < LENGTH_OF_HEADER) { LOGGER.debug("skipping bb - too few data for header: " + readableBytes); return;//from ww w. j a v a 2 s . c o m } int length = bb.getUnsignedShort(bb.readerIndex() + LENGTH_INDEX_IN_HEADER); LOGGER.debug("length of actual message: {}", length); if (readableBytes < length) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("skipping bb - too few data for msg: " + readableBytes + " < " + length); LOGGER.debug("bb: " + ByteBufUtils.byteBufToHexString(bb)); } return; } LOGGER.debug("OF Protocol message received, type:{}", bb.getByte(bb.readerIndex() + 1)); ByteBuf messageBuffer = bb.slice(bb.readerIndex(), length); list.add(messageBuffer); messageBuffer.retain(); bb.skipBytes(length); }
From source file:org.opendaylight.protocol.pcep.ietf.PCEPObjectParserTest.java
License:Open Source License
@Test public void testOpenObjectWithTLV() throws PCEPDeserializerException, IOException { try (SyncOptimizationsActivator a = new SyncOptimizationsActivator()) { a.start(this.ctx); final SyncOptimizationsOpenObjectParser parser = new SyncOptimizationsOpenObjectParser( this.ctx.getTlvHandlerRegistry(), this.ctx.getVendorInformationTlvRegistry()); final ByteBuf result = Unpooled .wrappedBuffer(ByteArray.fileToBytes("src/test/resources/PCEPOpenObject1.bin")); final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder(); builder.setProcessingRule(false); builder.setIgnore(false);// w ww.j a v a 2 s .c om builder.setVersion(new ProtocolVersion((short) 1)); builder.setKeepalive((short) 30); builder.setDeadTimer((short) 120); builder.setSessionId((short) 1); final Stateful tlv1 = new StatefulBuilder().setLspUpdateCapability(Boolean.TRUE) .addAugmentation(Stateful1.class, new Stateful1Builder().build()).build(); final Tlvs1Builder statBuilder = new Tlvs1Builder(); statBuilder.setStateful(tlv1); final Tlvs3Builder syncOptBuilder = new Tlvs3Builder(); syncOptBuilder.setLspDbVersion( new LspDbVersionBuilder().setLspDbVersionValue(new BigInteger(DB_VERSION)).build()); syncOptBuilder .setSpeakerEntityId(new SpeakerEntityIdBuilder().setSpeakerEntityIdValue(SPEAKER_ID).build()); builder.setTlvs( new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder() .addAugmentation(Tlvs1.class, statBuilder.build()) .addAugmentation(Tlvs3.class, syncOptBuilder.build()).build()); assertEquals(builder.build(), parser.parseObject(new ObjectHeaderImpl(false, false), result.slice(4, result.readableBytes() - 4))); final ByteBuf buf = Unpooled.buffer(); parser.serializeObject(builder.build(), buf); assertArrayEquals(result.array(), ByteArray.getAllBytes(buf)); } }