List of usage examples for io.netty.buffer ByteBuf resetReaderIndex
public abstract ByteBuf resetReaderIndex();
From source file:com.yahoo.pulsar.client.impl.ProducerImpl.java
License:Apache License
/** * Strips checksum from {@link OpSendMsg} command if present else ignore it. * /*from w w w . j a v a 2 s.com*/ * @param op */ private void stripChecksum(OpSendMsg op) { op.cmd.markReaderIndex(); int totalMsgBufSize = op.cmd.readableBytes(); DoubleByteBuf msg = getDoubleByteBuf(op.cmd); if (msg != null) { ByteBuf headerFrame = msg.getFirst(); msg.markReaderIndex(); headerFrame.markReaderIndex(); try { headerFrame.skipBytes(4); // skip [total-size] int cmdSize = (int) headerFrame.readUnsignedInt(); // verify if checksum present headerFrame.skipBytes(cmdSize); if (!hasChecksum(headerFrame)) { headerFrame.resetReaderIndex(); return; } int headerSize = 4 + 4 + cmdSize; // [total-size] [cmd-length] [cmd-size] int checksumSize = 4 + 2; // [magic-number] [checksum-size] int checksumMark = (headerSize + checksumSize); // [header-size] [checksum-size] int metaPayloadSize = (totalMsgBufSize - checksumMark); // metadataPayload = totalSize - checksumMark int newTotalFrameSizeLength = 4 + cmdSize + metaPayloadSize; // new total-size without checksum headerFrame.resetReaderIndex(); int headerFrameSize = headerFrame.readableBytes(); headerFrame.setInt(0, newTotalFrameSizeLength); // rewrite new [total-size] ByteBuf metadata = headerFrame.slice(checksumMark, headerFrameSize - checksumMark); // sliced only // metadata headerFrame.writerIndex(headerSize); // set headerFrame write-index to overwrite metadata over checksum metadata.readBytes(headerFrame, metadata.readableBytes()); headerFrame.capacity(headerFrameSize - checksumSize); // reduce capacity by removed checksum bytes headerFrame.resetReaderIndex(); } finally { op.cmd.resetReaderIndex(); } } else { log.warn("[{}] Failed while casting {} into DoubleByteBuf", producerName, op.cmd.getClass().getName()); } }
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;/*from w w w.j ava 2 s . co 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 a v a 2 s. c om 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.zanclus.ssh.transport.SSHFrameDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { in.markReaderIndex();//from w w w. ja va2 s .co m if (in.readableBytes() > HEADER_LEN) { byte[] header = new byte[HEADER_LEN]; in.readBytes(header); 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.zanclus.ssh.transport.SSHFrameDecoder.java
License:Apache License
/** * Decode the SSH Packet// w w w . j a va2 s . c o m * @param paddingLen The length (in bytes) of the random padding. * @param packetLen The length of the encrypted payload (in bytes) * @param in The {@link ByteBuf} from which to read the payload data * @throws IllegalAccessException Thrown by the {@link SSHFrameDecoder#decodePayload(int, io.netty.buffer.ByteBuf, int)} method * @throws InvalidMACException Thrown by the {@link SSHFrameDecoder#decodePayload(int, io.netty.buffer.ByteBuf, int)} method * @throws InvalidCipherTextException Thrown by the {@link SSHFrameDecoder#decodePayload(int, io.netty.buffer.ByteBuf, int)} method * @throws InstantiationException Thrown by the {@link SSHFrameDecoder#decodePayload(int, io.netty.buffer.ByteBuf, int)} method */ private void decodePacket(int paddingLen, int packetLen, ByteBuf in) throws IllegalAccessException, InvalidMACException, InvalidCipherTextException, InstantiationException { int payloadLen = packetLen - paddingLen - HEADER_LEN; if (algorithm.equals(HMAC.NONE)) { LOG.warn("HMAC algorithm set to NONE, this SHOULD only happen during inital key exchange." + "See https://tools.ietf.org/html/rfc4253#section-6"); } if (in.readableBytes() >= (payloadLen + paddingLen + algorithm.digestLen())) { decodePayload(payloadLen, in, paddingLen); } 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();// w w w . j a va 2s .c o m int bufLenght = 0; if (!in.isReadable()) { in.resetReaderIndex(); 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;// ww w . j a va2s .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();// ww w. ja v a 2 s. c o m out.writeInt(dataLength); 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;/*from w w w.j av a2 s. co 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:eu.jangos.realm.network.encoder.RealmPacketEncoder.java
License:Open Source License
private void reverseHeader(ChannelHandlerContext ctx, ByteBuf out) { // Retaining the actual index. int index = out.writerIndex(); out.resetReaderIndex(); // Reading the header. byte[] header = out.readBytes(4).array(); // Resetting the reader index to send it. out.resetReaderIndex();//from ww w .j av a 2 s . c o m // Swapping the header. byte temp = header[2]; header[2] = header[3]; header[3] = temp; // Encrypting the header (if applicable). header = ctx.channel().attr(CRYPT).get().encrypt(header); // Reset the writer index. out.resetWriterIndex(); // Write the header. out.writeBytes(header); // Reset the writer index to the default one. out.writerIndex(index); }