List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract ByteBuf readerIndex(int readerIndex);
From source file:com.heliosapm.streams.metrics.aggregation.StreamedMetricAggregation.java
License:Apache License
/** * Creates a new StreamedMetricAggregation from the passed bytes * @param bytes The bytes to read from/*from w ww . ja v a 2 s .co m*/ */ private StreamedMetricAggregation(final byte[] bytes) { size = bytes.length; final ByteBuf b = BufferManager.getInstance().buffer(size).writeBytes(bytes); try { sticky = b.getByte(STICKY) == 1; doubleType = b.getByte(DOUBLE_TYPE) == 1; createTime = b.getLong(CREATE_TIME); period = b.getLong(PERIOD); periodUnit = TUNITS[b.getByte(PERIOD_UNIT)]; b.readerIndex(METRIC_VALUES); values.put(bytes, METRIC_VALUES, VALUE_SIZE); final byte tagCount = b.getByte(TAG_COUNT); b.readerIndex(METRIC_NAME); metricName = nextString(b); for (int i = 0; i < tagCount; i++) { tags.put(nextString(b), nextString(b)); i++; } } finally { try { b.release(); } catch (Exception x) { /* No Op */} } }
From source file:com.heliosapm.streams.tracing.AbstractMetricWriter.java
License:Apache License
/** * {@inheritDoc}//from w w w . ja v a 2 s . c om * @see com.heliosapm.streams.tracing.IMetricWriter#onMetrics(io.netty.buffer.ByteBuf) */ @Override public void onMetrics(final ByteBuf metrics) { metrics.readerIndex(0); final InputStream is = new ByteBufInputStream(metrics); try { for (StreamedMetric sm : StreamedMetric.streamedMetrics(is, true, false)) { onMetrics(sm); } //onMetrics(StreamedMetric.read(is)); } finally { try { is.close(); } catch (Exception x) { /* No Op */} } }
From source file:com.heliosapm.utils.buffer.BufferManager.java
License:Apache License
/** * Reads a UTF string from the passed ByteBuff * @param offset int The offset in the buffer to read from * @param in The ByteBuf to read from/*from w ww . ja v a2 s .c o m*/ * @return the read string */ public final static String readUTF(final int offset, final ByteBuf in) { try { in.markReaderIndex(); in.readerIndex(offset); return readUTF(in); } finally { in.resetReaderIndex(); } }
From source file:com.ibasco.agql.protocols.valve.source.query.SourcePacketBuilder.java
License:Open Source License
@Override public <T extends SourceServerPacket> T construct(ByteBuf data) { //Mark Index/*from ww w. ja va 2 s.co m*/ data.markReaderIndex(); try { //Reset the index data.readerIndex(0); //Verify size if (data.readableBytes() < 5) throw new IllegalStateException( "Cannot continue processing buffer with less than or equal to 4 bytes"); //Read protocol header int protocolHeader = data.readIntLE(); //Check if this is a split packet if (protocolHeader == 0xFFFFFFFE) throw new IllegalStateException("Cannot construct a response from a partial/split packet."); //Verify that we have a valid header if (protocolHeader != 0xFFFFFFFF) throw new IllegalStateException("Protocol header not supported."); //Read packet header byte packetHeader = data.readByte(); //Read payload byte[] payload = new byte[data.readableBytes()]; data.readBytes(payload); //Verify if packet header is valid SourceServerPacket packet = createResponsePacketFromHeader(packetHeader); //If packet is empty, means the supplied packet header is not supported if (packet == null) return null; packet.setProtocolHeader(ByteUtils.byteArrayFromInteger(protocolHeader)); packet.setHeader(packetHeader); packet.setPayload(payload); return (T) packet; } finally { data.resetReaderIndex(); } }
From source file:com.ibasco.agql.protocols.valve.source.query.SourceRconPacketBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//w ww. j av a 2 s. co m public <T extends SourceRconPacket> T construct(ByteBuf data) { try { if (data.readableBytes() < 14) { log.warn("Packet is less than 10 bytes. Unsupported packet."); if (log.isDebugEnabled()) log.debug("Unrecognized Packet: \n{}", ByteBufUtil.prettyHexDump(data)); return null; } //Remember the reader index data.markReaderIndex(); //Read from the listen data.readerIndex(0); int size = data.readIntLE(); int id = data.readIntLE(); int type = data.readIntLE(); String body = data.readCharSequence(data.readableBytes() - 2, StandardCharsets.UTF_8).toString(); SourceRconResponsePacket packet = getResponsePacket(type); if (packet != null) { //Ok, we have a valid response packet. Lets keep reading. packet.setId(id); packet.setSize(size); packet.setType(type); packet.setBody(body); return (T) packet; } } finally { //Reset the index data.resetReaderIndex(); } return null; }
From source file:com.ibasco.agql.protocols.valve.steam.master.MasterServerPacketBuilder.java
License:Open Source License
@Override public <T extends MasterServerPacket> T construct(ByteBuf data) { //Mark Index// w w w . j a va 2 s . c om data.markReaderIndex(); try { //Reset the index data.readerIndex(0); //Verify size if (data.readableBytes() <= 6) throw new IllegalStateException( "Cannot continue processing buffer with less than or equal to 6 bytes"); //Read header byte[] header = new byte[6]; data.readBytes(header); //Read payload byte[] payload = new byte[data.readableBytes()]; data.readBytes(payload); //Verify if packet header is valid MasterServerResponsePacket packet = new MasterServerResponsePacket(); packet.setHeader(header); packet.setPayload(payload); return (T) packet; } finally { data.resetReaderIndex(); } }
From source file:com.l2jmobius.commons.network.codecs.CryptCodec.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { in.resetReaderIndex();//from ww w . j a v a 2 s . c om _crypt.decrypt(in); in.readerIndex(in.writerIndex()); out.add(in.copy(0, in.writerIndex())); }
From source file:com.l2jmobius.commons.network.codecs.PacketDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { if ((in == null) || !in.isReadable()) { return;/*from w w w . ja v a 2s .co m*/ } if (in.order() != _byteOrder) { in = in.order(_byteOrder); } try { final short packetId = in.readUnsignedByte(); if (packetId >= _incomingPackets.length) { LOGGER.finer("Unknown packet: " + Integer.toHexString(packetId)); return; } final IIncomingPackets<T> incomingPacket = _incomingPackets[packetId]; if (incomingPacket == null) { LOGGER.finer("Unknown packet: " + Integer.toHexString(packetId)); return; } final IConnectionState connectionState = ctx.channel().attr(IConnectionState.ATTRIBUTE_KEY).get(); if ((connectionState == null) || !incomingPacket.getConnectionStates().contains(connectionState)) { // LOGGER.warning(incomingPacket + ": Connection at invalid state: " + connectionState + " Required States: " + incomingPacket.getConnectionStates()); return; } final IIncomingPacket<T> packet = incomingPacket.newIncomingPacket(); if ((packet != null) && packet.read(_client, new PacketReader(in))) { out.add(packet); } } finally { // We always consider that we read whole packet. in.readerIndex(in.writerIndex()); } }
From source file:com.mpush.netty.codec.PacketDecoder.java
License:Apache License
private void decodeHeartbeat(ByteBuf in, List<Object> out) { while (in.isReadable()) { if (in.readByte() == Packet.HB_PACKET_BYTE) { out.add(Packet.HB_PACKET);/* w w w. jav a 2s . c om*/ } else { in.readerIndex(in.readerIndex() - 1); break; } } }
From source file:com.navercorp.nbasearc.gcp.RedisDecoder.java
License:Apache License
private boolean hasFrame(ByteBuf in) { if (in.isReadable() == false) { return false; }/*from w w w .j a va 2 s . co m*/ lookasideBufferLength = Math.min(in.readableBytes(), lookasideBuffer.length); in.readBytes(lookasideBuffer, 0, lookasideBufferLength); lookasideBufferReaderIndex = 0; in.readerIndex(in.readerIndex() - lookasideBufferLength); byte b = lookasideBuffer[lookasideBufferReaderIndex++]; in.skipBytes(1); if (b == MINUS_BYTE) { return processError(in); } else if (b == ASTERISK_BYTE) { return processMultiBulkReply(in); } else if (b == COLON_BYTE) { return processInteger(in); } else if (b == DOLLAR_BYTE) { return processBulkReply(in); } else if (b == PLUS_BYTE) { return processStatusCodeReply(in); } else { throw new JedisConnectionException("Unknown reply: " + (char) b); } }