List of usage examples for io.netty.buffer ByteBuf markReaderIndex
public abstract ByteBuf markReaderIndex();
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
private static ByteBuf serializeCommandSendWithSize(BaseCommand.Builder cmdBuilder, ChecksumType checksumType, MessageMetadata msgMetadata, ByteBuf payload) { // / Wire format // [TOTAL_SIZE] [CMD_SIZE][CMD] [MAGIC_NUMBER][CHECKSUM] [METADATA_SIZE][METADATA] [PAYLOAD] BaseCommand cmd = cmdBuilder.build(); int cmdSize = cmd.getSerializedSize(); int msgMetadataSize = msgMetadata.getSerializedSize(); int payloadSize = payload.readableBytes(); int magicAndChecksumLength = ChecksumType.Crc32c.equals(checksumType) ? (2 + 4 /* magic + checksumLength*/) : 0;/*w w w .j ava 2 s . c o m*/ boolean includeChecksum = magicAndChecksumLength > 0; int headerContentSize = 4 + cmdSize + magicAndChecksumLength + 4 + msgMetadataSize; // cmdLength + cmdSize + magicLength + // checksumSize + msgMetadataLength + // msgMetadataSize int totalSize = headerContentSize + payloadSize; int headersSize = 4 + headerContentSize; // totalSize + headerLength int checksumReaderIndex = -1; ByteBuf headers = PooledByteBufAllocator.DEFAULT.buffer(headersSize, headersSize); headers.writeInt(totalSize); // External frame try { // Write cmd headers.writeInt(cmdSize); ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers); cmd.writeTo(outStream); cmd.recycle(); cmdBuilder.recycle(); //Create checksum placeholder if (includeChecksum) { headers.writeShort(magicCrc32c); checksumReaderIndex = headers.writerIndex(); headers.writerIndex(headers.writerIndex() + checksumSize); //skip 4 bytes of checksum } // Write metadata headers.writeInt(msgMetadataSize); msgMetadata.writeTo(outStream); outStream.recycle(); } catch (IOException e) { // This is in-memory serialization, should not fail throw new RuntimeException(e); } ByteBuf command = DoubleByteBuf.get(headers, payload); // write checksum at created checksum-placeholder if (includeChecksum) { headers.markReaderIndex(); headers.readerIndex(checksumReaderIndex + checksumSize); int metadataChecksum = computeChecksum(headers); int computedChecksum = resumeChecksum(metadataChecksum, payload); // set computed checksum headers.setInt(checksumReaderIndex, computedChecksum); headers.resetReaderIndex(); } return command; }
From source file:com.yahoo.pulsar.common.api.Commands.java
License:Apache License
public static ByteBuf deSerializeSingleMessageInBatch(ByteBuf uncompressedPayload, PulsarApi.SingleMessageMetadata.Builder singleMessageMetadataBuilder, int index, int batchSize) throws IOException { int singleMetaSize = (int) uncompressedPayload.readUnsignedInt(); int writerIndex = uncompressedPayload.writerIndex(); int beginIndex = uncompressedPayload.readerIndex() + singleMetaSize; uncompressedPayload.writerIndex(beginIndex); ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(uncompressedPayload); PulsarApi.SingleMessageMetadata singleMessageMetadata = singleMessageMetadataBuilder.mergeFrom(stream, null) .build();//from w ww . j av a 2 s . c o m int singleMessagePayloadSize = singleMessageMetadata.getPayloadSize(); uncompressedPayload.markReaderIndex(); ByteBuf singleMessagePayload = uncompressedPayload.slice(uncompressedPayload.readerIndex(), singleMessagePayloadSize); singleMessagePayload.retain(); uncompressedPayload.writerIndex(writerIndex); uncompressedPayload.resetReaderIndex(); // reader now points to beginning of payload read; so move it past message payload just read if (index < batchSize) { uncompressedPayload.readerIndex(uncompressedPayload.readerIndex() + singleMessagePayloadSize); } return singleMessagePayload; }
From source file:com.yahoo.pulsar.common.api.PulsarDecoder.java
License:Apache License
@Override final public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // Get a buffer that contains the full frame ByteBuf buffer = (ByteBuf) msg; BaseCommand cmd = null;//from w w w . j ava2s. com BaseCommand.Builder cmdBuilder = null; try { // De-serialize the command int cmdSize = (int) buffer.readUnsignedInt(); int writerIndex = buffer.writerIndex(); buffer.writerIndex(buffer.readerIndex() + cmdSize); ByteBufCodedInputStream cmdInputStream = ByteBufCodedInputStream.get(buffer); cmdBuilder = BaseCommand.newBuilder(); cmd = cmdBuilder.mergeFrom(cmdInputStream, null).build(); buffer.writerIndex(writerIndex); cmdInputStream.recycle(); if (log.isDebugEnabled()) { log.debug("[{}] Received cmd {}", ctx.channel().remoteAddress(), cmd.getType()); } messageReceived(); switch (cmd.getType()) { case PARTITIONED_METADATA: checkArgument(cmd.hasPartitionMetadata()); handlePartitionMetadataRequest(cmd.getPartitionMetadata()); cmd.getPartitionMetadata().recycle(); break; case PARTITIONED_METADATA_RESPONSE: checkArgument(cmd.hasPartitionMetadataResponse()); handlePartitionResponse(cmd.getPartitionMetadataResponse()); cmd.getPartitionMetadataResponse().recycle(); break; case LOOKUP: checkArgument(cmd.hasLookupTopic()); handleLookup(cmd.getLookupTopic()); cmd.getLookupTopic().recycle(); break; case LOOKUP_RESPONSE: checkArgument(cmd.hasLookupTopicResponse()); handleLookupResponse(cmd.getLookupTopicResponse()); cmd.getLookupTopicResponse().recycle(); break; case ACK: checkArgument(cmd.hasAck()); handleAck(cmd.getAck()); cmd.getAck().getMessageId().recycle(); cmd.getAck().recycle(); break; case CLOSE_CONSUMER: checkArgument(cmd.hasCloseConsumer()); handleCloseConsumer(cmd.getCloseConsumer()); cmd.getCloseConsumer().recycle(); break; case CLOSE_PRODUCER: checkArgument(cmd.hasCloseProducer()); handleCloseProducer(cmd.getCloseProducer()); cmd.getCloseProducer().recycle(); break; case CONNECT: checkArgument(cmd.hasConnect()); handleConnect(cmd.getConnect()); cmd.getConnect().recycle(); break; case CONNECTED: checkArgument(cmd.hasConnected()); handleConnected(cmd.getConnected()); cmd.getConnected().recycle(); break; case ERROR: checkArgument(cmd.hasError()); handleError(cmd.getError()); cmd.getError().recycle(); break; case FLOW: checkArgument(cmd.hasFlow()); handleFlow(cmd.getFlow()); cmd.getFlow().recycle(); break; case MESSAGE: { checkArgument(cmd.hasMessage()); handleMessage(cmd.getMessage(), buffer); cmd.getMessage().recycle(); break; } case PRODUCER: checkArgument(cmd.hasProducer()); handleProducer(cmd.getProducer()); cmd.getProducer().recycle(); break; case SEND: { checkArgument(cmd.hasSend()); // Store a buffer marking the content + headers ByteBuf headersAndPayload = buffer.markReaderIndex(); handleSend(cmd.getSend(), headersAndPayload); cmd.getSend().recycle(); break; } case SEND_ERROR: checkArgument(cmd.hasSendError()); handleSendError(cmd.getSendError()); cmd.getSendError().recycle(); break; case SEND_RECEIPT: checkArgument(cmd.hasSendReceipt()); handleSendReceipt(cmd.getSendReceipt()); cmd.getSendReceipt().recycle(); break; case SUBSCRIBE: checkArgument(cmd.hasSubscribe()); handleSubscribe(cmd.getSubscribe()); cmd.getSubscribe().recycle(); break; case SUCCESS: checkArgument(cmd.hasSuccess()); handleSuccess(cmd.getSuccess()); cmd.getSuccess().recycle(); break; case PRODUCER_SUCCESS: checkArgument(cmd.hasProducerSuccess()); handleProducerSuccess(cmd.getProducerSuccess()); cmd.getProducerSuccess().recycle(); break; case UNSUBSCRIBE: checkArgument(cmd.hasUnsubscribe()); handleUnsubscribe(cmd.getUnsubscribe()); cmd.getUnsubscribe().recycle(); break; case PING: checkArgument(cmd.hasPing()); handlePing(cmd.getPing()); cmd.getPing().recycle(); break; case PONG: checkArgument(cmd.hasPong()); handlePong(cmd.getPong()); cmd.getPong().recycle(); break; case REDELIVER_UNACKNOWLEDGED_MESSAGES: checkArgument(cmd.hasRedeliverUnacknowledgedMessages()); handleRedeliverUnacknowledged(cmd.getRedeliverUnacknowledgedMessages()); cmd.getRedeliverUnacknowledgedMessages().recycle(); break; } } finally { if (cmdBuilder != null) { cmdBuilder.recycle(); } if (cmd != null) { cmd.recycle(); } buffer.release(); } }
From source file:com.zanclus.ssh.transport.SSHFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); if (in.readableBytes() > HEADER_LEN) { byte[] header = new byte[HEADER_LEN]; in.readBytes(header);//from w ww. j a va 2 s .co m int packetLen = shift(header[0], 24) | shift(header[1], 16) | shift(header[2], 8) | shift(header[3], 0); if (largePacketSupport || packetLen <= 35000) { decodePacket(header[4], packetLen, in); } else { // Packet length cannot be greater than 35000 bytes according to RFC 4253 Section 6.1 throw new PacketSizeException( String.format("Packet size of '%d' exceeds RFC 4253 recommendations and " + "large packet support was not expressly enabled.", packetLen)); } } else { LOG.info("Insufficient bytes to finish decoding Packet. Resetting input buffer."); in.resetReaderIndex(); } }
From source file:com.zxcc.socket.protobuf.ProtobufVarint32FrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex(); int bufLenght = 0; if (!in.isReadable()) { in.resetReaderIndex();/*from ww w . j a va2 s . c o m*/ return; } if (in.readableBytes() < 4) { in.resetReaderIndex(); return; } bufLenght = in.readInt(); if (bufLenght < 0) { throw new CorruptedFrameException("negative length: " + bufLenght); } if (in.readableBytes() < bufLenght) { in.resetReaderIndex(); return; } else { out.add(in.readBytes(bufLenght)); return; } }
From source file:dbseer.middleware.packet.MiddlewarePacketDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception { if (buf.readableBytes() < 8) { Log.debug(this.getClass().getCanonicalName(), "buf less than 8 bytes"); return;//from w w w . j a v a 2s. c om } buf.markReaderIndex(); int header = buf.readInt(); int length = buf.readInt(); if (buf.readableBytes() < length) { buf.resetReaderIndex(); Log.debug(this.getClass().getCanonicalName(), "readable bytes less than length = " + length + " and header = " + header); return; } String log = ""; Log.debug(String.format("len = %d, readable = %d", length, buf.readableBytes())); if (length > 0) { byte[] readBuf = new byte[length]; buf.readBytes(readBuf); log = new String(readBuf, "UTF-8"); } out.add(new MiddlewarePacket(header, length, log)); }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureGenerator.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { final int dataLength = msg.readableBytes(); msg.markReaderIndex(); out.writeInt(dataLength);/*from ww w.jav a2s . com*/ out.writeBytes(msg); msg.resetReaderIndex(); signSignature.update(msg.nioBuffer()); msg.readerIndex(msg.writerIndex()); final byte[] signature = signSignature.sign(); final int signatureLength = signature.length; out.writeInt(signatureLength); out.writeBytes(signature); //Log.v(TAG, "Signed " + dataLength + "b of data with " + signatureLength + "b signature" + // (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : "")); }
From source file:dorkbox.network.pipeline.tcp.KryoDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) throws Exception { // Make sure if the length field was received, // and read the length of the next object from the socket. int lengthLength = OptimizeUtilsByteBuf.canReadInt(in); int readableBytes = in.readableBytes(); // full length of available bytes. if (lengthLength == 0 || readableBytes < 2 || readableBytes < lengthLength) { // The length field was not fully received - do nothing (wait for more...) // This method will be invoked again when more packets are // received and appended to the buffer. return;// w w w . j a v a2 s .c o m } // The length field is in the buffer. // save the writerIndex for local access int writerIndex = in.writerIndex(); // Mark the current buffer position before reading the length fields // 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. in.markReaderIndex(); // Read the length field. int length = OptimizeUtilsByteBuf.readInt(in, true); readableBytes = in.readableBytes(); // have to adjust readable bytes, since we just read an int off the buffer. if (length == 0) { context.fireExceptionCaught(new IllegalStateException("KryoDecoder had a read length of 0")); return; } // we can't test against a single "max size", since objects can back-up on the buffer. // we must ABSOLUTELY follow a "max size" rule when encoding objects, however. final NetworkSerializationManager serializationManager = this.serializationManager; // Make sure if there's enough bytes in the buffer. if (length > readableBytes) { // 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. in.resetReaderIndex(); // wait for the rest of the object to come in. // System.err.println(Thread.currentThread().getName() + " waiting for more of the object to arrive"); } // how many objects are on this buffer? else if (readableBytes > length) { // more than one! // read the object off of the buffer. (save parts of the buffer so if it is too big, we can go back to it, and use it later on...) // we know we have at least one object int objectCount = 1; int endOfObjectPosition = in.readerIndex() + length; // set us up for the next object. in.readerIndex(endOfObjectPosition); // how many more objects?? The first time, it can be off, because we already KNOW it's > 0. // (That's how we got here to begin with) while (readableBytes > 0) { if (OptimizeUtilsByteBuf.canReadInt(in) > 0) { length = OptimizeUtilsByteBuf.readInt(in, true); if (length <= 0) { // throw new IllegalStateException("Kryo DecoderTCP had a read length of 0"); break; } endOfObjectPosition = in.readerIndex() + length; // position the reader to look for the NEXT object if (endOfObjectPosition <= writerIndex) { in.readerIndex(endOfObjectPosition); readableBytes = in.readableBytes(); objectCount++; } else { break; } } else { break; } } // readerIndex is currently at the MAX place it can read data off the buffer. // reset it to the spot BEFORE we started reading data from the buffer. in.resetReaderIndex(); // System.err.println("Found " + objectCount + " objects in this buffer."); // NOW add each one of the NEW objects to the array! for (int i = 0; i < objectCount; i++) { length = OptimizeUtilsByteBuf.readInt(in, true); // object LENGTH // however many we need to Object object; try { object = readObject(serializationManager, context, in, length); out.add(object); } catch (Exception ex) { context.fireExceptionCaught(new IOException("Unable to deserialize object!", ex)); } } // the buffer reader index will be at the correct location, since the read object method advances it. } else { // exactly one! Object object; try { object = readObject(serializationManager, context, in, length); out.add(object); } catch (Exception ex) { context.fireExceptionCaught( new IOException("Unable to deserialize object for " + this.getClass(), ex)); } } }
From source file:gedi.remote.codec.DefaultDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < Integer.BYTES) return;/*from ww w .j a va 2 s. c o m*/ in.markReaderIndex(); int size = in.readInt(); if (in.readableBytes() < size) { in.resetReaderIndex(); return; } // everything has arrived, decode char[] classname = new char[in.readInt()]; for (int i = 0; i < classname.length; i++) classname[i] = (char) (in.readByte() & 255); String clsName = String.valueOf(classname); if (clsName.length() == 1) { switch (clsName) { case "A": char[] re = new char[in.readInt()]; for (int i = 0; i < re.length; i++) re[i] = (char) (in.readByte() & 255); out.add(String.valueOf(re)); break; case "B": out.add(in.readByte()); break; case "S": out.add(in.readShort()); break; case "I": out.add(in.readInt()); break; case "L": out.add(in.readLong()); break; case "F": out.add(in.readFloat()); break; case "D": out.add(in.readDouble()); break; } } else { if (!ClassPathCache.getInstance().existsClass(clsName)) { in.resetReaderIndex(); return; } Class<?> cls = Class.forName(clsName); BinarySerializable re = (BinarySerializable) cls.newInstance(); BinaryBlob buff = new BinaryBlob(size - Integer.BYTES - classname.length); in.readBytes(buff.getBuffer()); buff.getBuffer().flip(); re.deserialize(buff); out.add(re); } }
From source file:gwlpr.protocol.NettyGWCodec.java
License:Open Source License
@Override public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> result) { ByteBuf buf = in.order(ByteOrder.LITTLE_ENDIAN); while (buf.isReadable()) { buf.markReaderIndex(); // get the header failsafe int header = buf.readableBytes() >= 2 ? buf.readShort() : -1; // failcheck if (header == -1) { return; }//from www . j ava2 s .c om // try get the message class Class<? extends GWMessage> messageClazz = getByHeader(header); // failcheck if (messageClazz == null) { buf.resetReaderIndex(); return; } // try retrieve the serialization filter NettySerializationFilter filter = GWMessageSerializationRegistry.getFilter(messageClazz); // failcheck if (filter == null) { buf.resetReaderIndex(); return; } // try create the message Message message; try { message = messageClazz.newInstance(); } catch (InstantiationException | IllegalAccessException ex) { LOGGER.error("Could not create an instance of an message.", ex); buf.resetReaderIndex(); return; } // dont forget to initialize the message message.init(ctx.channel()); // try serialize the message if (!filter.deserialize(buf, message)) { buf.resetReaderIndex(); return; } // finally add the message result.add(message); LOGGER.debug("Got: {}", message.toString()); } }