List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:Http2ClientConnectionHandler.java
License:Apache License
@Override public void onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream, boolean endOfSegment) throws Http2Exception { // Copy the data into the buffer. int available = data.readableBytes(); if (collectedData == null) { collectedData = ctx().alloc().buffer(available); collectedData.writeBytes(data, data.readerIndex(), data.readableBytes()); } else {//from www . j av a 2s. c om // Expand the buffer ByteBuf newBuffer = ctx().alloc().buffer(collectedData.readableBytes() + available); newBuffer.writeBytes(collectedData); newBuffer.writeBytes(data); collectedData.release(); collectedData = newBuffer; } // If it's the last frame, print the complete message. if (endOfStream) { // System.out.println("Received message: " + collectedData.toString(CharsetUtil.UTF_8)); final OutstandingRequest outstandingRequest = outstanding.remove(streamId); outstandingRequest.finish(collectedData); // Free the data buffer. collectedData.release(); collectedData = null; } }
From source file:appeng.core.sync.packets.PacketJEIRecipe.java
License:Open Source License
public PacketJEIRecipe(final ByteBuf stream) throws IOException { final ByteArrayInputStream bytes = new ByteArrayInputStream(stream.array()); bytes.skip(stream.readerIndex()); final NBTTagCompound comp = CompressedStreamTools.readCompressed(bytes); if (comp != null) { this.recipe = new ItemStack[9][]; for (int x = 0; x < this.recipe.length; x++) { final NBTTagList list = comp.getTagList("#" + x, 10); if (list.tagCount() > 0) { this.recipe[x] = new ItemStack[list.tagCount()]; for (int y = 0; y < list.tagCount(); y++) { this.recipe[x][y] = ItemStack.loadItemStackFromNBT(list.getCompoundTagAt(y)); }/* www.j av a2 s . c o m*/ } } } }
From source file:appeng.core.sync.packets.PacketNEIRecipe.java
License:Open Source License
public PacketNEIRecipe(final ByteBuf stream) throws IOException { final ByteArrayInputStream bytes = new ByteArrayInputStream(stream.array()); bytes.skip(stream.readerIndex()); final NBTTagCompound comp = CompressedStreamTools.readCompressed(bytes); if (comp != null) { this.recipe = new ItemStack[9][]; for (int x = 0; x < this.recipe.length; x++) { final NBTTagList list = comp.getTagList("#" + x, 10); if (list.tagCount() > 0) { this.recipe[x] = new ItemStack[list.tagCount()]; for (int y = 0; y < list.tagCount(); y++) { this.recipe[x][y] = ItemStack.loadItemStackFromNBT(list.getCompoundTagAt(y)); }//from w ww. j a v a 2 s . c om } } } }
From source file:appeng.core.sync.packets.PacketValueConfig.java
License:Open Source License
public PacketValueConfig(final ByteBuf stream) throws IOException { final DataInputStream dis = new DataInputStream( new ByteArrayInputStream(stream.array(), stream.readerIndex(), stream.readableBytes())); this.Name = dis.readUTF(); this.Value = dis.readUTF(); // dis.close(); }
From source file:at.yawk.accordion.compression.SnappyCompressor.java
License:Mozilla Public License
private static byte[] toArray(ByteBuf raw) { return Arrays.copyOfRange(raw.array(), raw.arrayOffset() + raw.readerIndex(), raw.arrayOffset() + raw.readerIndex() + raw.readableBytes()); }
From source file:at.yawk.accordion.distributed.ConnectionManager.java
License:Mozilla Public License
/** * Handle a raw (encoded) message from the given connection. *//* w ww . j a va 2 s . c o m*/ private void handleRawMessage(Connection connection, ByteBuf message) { int startIndex = message.readerIndex(); receivedPacketCountIncludingDuplicates.incrementAndGet(); // read packet ID long packetId = message.readLong(); if (!packetDistinctionHandler.register(packetId)) { // already received, do not handle again Log.debug(logger, () -> "Duplicate packet " + packetId); return; } // mark heartbeat as alive // not doing this before distinction check should be enough because only duplicate packets over 3s is a problem // anyway heartbeatManager.markAlive(connection); receivedPacketCount.incrementAndGet(); Stream<Connection> forwards = handleDecodedMessage(connection, compressor.decode(message), packetId); // reset reader index so we can copy the message message.readerIndex(startIndex); // forward packet to other connections that listen to this channel forwards // except the origin of the packet (they already got it) .filter(other -> other != connection) // send .forEach(other -> copyAndSend(other, message)); }
From source file:at.yawk.dbus.protocol.auth.CommandCodec.java
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int crlfPos = in.forEachByte(new CRLFFinder()); if (crlfPos == -1) { return;/*from w w w . jav a2s .co m*/ } int start = in.readerIndex(); int end = crlfPos - 1; // remove trailing crlf String commandString = in.toString(start, end - start, CHARSET); log.trace("Received message {}", commandString); in.readerIndex(crlfPos + 1); int sep = commandString.indexOf(' '); String commandName = sep == -1 ? commandString : commandString.substring(0, sep); Function<List<String>, Command> factory = FACTORIES.get(commandName); if (factory != null) { List<String> args; if (sep == -1) { args = Collections.emptyList(); } else { args = Arrays.asList(commandString.substring(sep + 1).split(" ")); } out.add(factory.apply(args)); } }
From source file:at.yawk.dbus.protocol.codec.MessageHeaderCodec.java
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf rawBuf, List<Object> out) throws Exception { if (toRead != 0) { if (rawBuf.readableBytes() < toRead) { return; }//from ww w . j a v a2 s. c om ByteBuf slice = rawBuf.slice().order(byteOrder); slice.writerIndex(slice.readerIndex() + toRead); slice.retain(); AlignableByteBuf decoding = AlignableByteBuf.decoding(slice); log.trace("INBOUND {}", decoding); out.add(decoding); rawBuf.readerIndex(rawBuf.readerIndex() + toRead); toRead = 0; } if (rawBuf.readableBytes() < MIN_HEADER_LENGTH) { return; } rawBuf.markReaderIndex(); byte endianness = rawBuf.readByte(); ByteOrder order; switch (endianness) { case 'l': order = ByteOrder.LITTLE_ENDIAN; break; case 'B': order = ByteOrder.BIG_ENDIAN; break; default: throw new DecoderException("Unknown byte order byte " + endianness); } AlignableByteBuf buf = AlignableByteBuf.decoding(rawBuf.resetReaderIndex().order(order)); buf.getBuffer().markReaderIndex(); buf.readByte(); // skip endianness byte we read above @Nullable MessageType type = MessageType.byId(buf.readByte()); byte flags = buf.readByte(); byte majorProtocolVersion = buf.readByte(); if (majorProtocolVersion != PROTOCOL_VERSION) { throw new DecoderException("Unsupported major protocol version " + majorProtocolVersion); } long bodyLength = buf.readUnsignedInt(); int serial = buf.readInt(); MessageHeader header = new MessageHeader(); header.setByteOrder(order); header.setMessageType(type); header.setNoReplyExpected((flags & NO_REPLY_EXPECTED) != 0); header.setNoAutoStart((flags & NO_AUTO_START) != 0); header.setAllowInteractiveAuthorization((flags & ALLOW_INTERACTIVE_AUTHORIZATION) != 0); header.setMajorProtocolVersion(majorProtocolVersion); header.setMessageBodyLength(bodyLength); header.setSerial(serial); header.setHeaderFields(new EnumMap<>(HeaderField.class)); ArrayObject headers = (ArrayObject) tryDecode(HEADER_FIELD_LIST_TYPE, buf); if (headers == null) { // not enough data buf.getBuffer().resetReaderIndex(); return; } for (DbusObject struct : headers.getValues()) { HeaderField field = HeaderField.byId(struct.get(0).byteValue()); if (field != null) { DbusObject value = struct.get(1).getValue(); if (!value.getType().equals(field.getType())) { throw new DecoderException("Invalid header type on " + field + ": got " + value.getType() + " but expected " + field.getType()); } header.getHeaderFields().put(field, value); } } if (type != null) { checkRequiredHeaderFieldsPresent(header); } if (!buf.canAlignRead(8)) { buf.getBuffer().resetReaderIndex(); return; } buf.alignRead(8); toRead = Math.toIntExact(header.getMessageBodyLength()); byteOrder = order; out.add(header); }
From source file:at.yawk.dbus.protocol.DbusUtil.java
public static String printHex(ByteBuf buf) { byte[] bytes = new byte[buf.readableBytes()]; buf.getBytes(buf.readerIndex(), bytes); return printHex(bytes); }
From source file:at.yawk.dbus.protocol.object.AlignableByteBuf.java
public static AlignableByteBuf decoding(ByteBuf wrapping, int baseAlignment) { return new AlignableByteBuf(wrapping, -wrapping.readerIndex(), baseAlignment); }