Example usage for io.netty.buffer ByteBuf skipBytes

List of usage examples for io.netty.buffer ByteBuf skipBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf skipBytes.

Prototype

public abstract ByteBuf skipBytes(int length);

Source Link

Document

Increases the current readerIndex by the specified length in this buffer.

Usage

From source file:divconq.ctp.stream.UntarStream.java

License:Open Source License

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL)
        return this.downstream.handle(file, data);

    ByteBuf in = data;

    if (in != null) {
        while (in.isReadable()) {
            switch (this.tstate) {
            case RECORD:
                // starting a new record
                if (in.readableBytes() < TarConstants.DEFAULT_RCDSIZE - this.partialLength) {
                    int offset = this.partialLength;

                    this.partialLength += in.readableBytes();

                    in.readBytes(this.header_buffer, offset, in.readableBytes());

                    continue;
                }/*  w  w w  . j  a v  a 2 s  . c o  m*/

                in.readBytes(this.header_buffer, this.partialLength,
                        TarConstants.DEFAULT_RCDSIZE - this.partialLength);

                this.partialLength = 0;

                //in.readBytes(this.header_buffer, 0, this.header_buffer.length);

                boolean hasHitEOF = this.isEOFRecord(this.header_buffer);

                // if we hit this twice in a row we are at the end - however, source will send FINAL anyway so we don't really care
                if (hasHitEOF) {
                    this.currEntry = null;
                    continue;
                }

                try {
                    this.currEntry = new TarArchiveEntry(this.header_buffer, this.encoding);
                } catch (Exception x) {
                    OperationContext.get().getTaskRun().kill("Error detected parsing the header: " + x);
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.XTRAS;
            case XTRAS:
                if (!in.isReadable())
                    continue;

                // TODO support long names and such - see org.apache.commons.compress.archivers.tar.TarArchiveInputStream
                if (this.currEntry.isGNULongLinkEntry()) {
                    /* 
                      byte[] longLinkData = getLongNameData();
                      if (longLinkData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long link entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setLinkName(encoding.decode(longLinkData));
                      */

                    OperationContext.get().getTaskRun().kill("long link currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNULongNameEntry()) {
                    /* 
                      byte[] longNameData = getLongNameData();
                      if (longNameData == null) {
                          // Bugzilla: 40334
                          // Malformed tar file - long entry name not followed by
                          // entry
                          return null;
                      }
                      currEntry.setName(encoding.decode(longNameData));
                      */

                    OperationContext.get().getTaskRun().kill("long name currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isPaxHeader()) {
                    // Process Pax headers
                    /* 
                      paxHeaders();
                      */

                    OperationContext.get().getTaskRun().kill("pax currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                if (this.currEntry.isGNUSparse()) {
                    // Process sparse files
                    /* 
                      readGNUSparse();
                      */

                    OperationContext.get().getTaskRun().kill("sparse currently not supported");
                    in.release();
                    return ReturnOption.DONE;
                }

                this.tstate = UntarState.PREP;
            case PREP:
                if (!in.isReadable())
                    continue;

                // TODO remove
                System.out.println("name: " + this.currEntry.getName());
                System.out.println("size: " + this.currEntry.getSize());
                System.out.println("modified: " + this.currEntry.getModTime());

                // If the size of the next element in the archive has changed
                // due to a new size being reported in the posix header
                // information, we update entrySize here so that it contains
                // the correct value.
                long entrySize = this.currEntry.getSize();
                this.remainContent = entrySize;

                long numRecords = (entrySize / this.header_buffer.length) + 1;
                this.remainSkip = (numRecords * this.header_buffer.length) - entrySize;

                // grab as much as we can from the current buffer
                int readSize = (int) Math.min(this.remainContent, in.readableBytes());
                this.remainContent -= readSize;

                // handle empty files too
                if ((readSize > 0) || (this.remainContent == 0)) {
                    System.out.println("reading content: " + readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                this.tstate = UntarState.CONTENT;
            case CONTENT:
                if (!in.isReadable())
                    continue;

                // check if there is still content left in the entry we were last reading from
                if (this.remainContent > 0) {
                    readSize = (int) Math.min(this.remainContent, in.readableBytes());
                    this.remainContent -= readSize;

                    //System.out.println("reading content: " + readSize);

                    //ByteBuf out = Hub.instance.getBufferAllocator().heapBuffer((int) readSize);

                    ByteBuf out = in.copy(in.readerIndex(), readSize);

                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize);
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes(readSize + skipSize);

                    this.nextMessage(out);
                }

                if (this.remainContent > 0)
                    continue;

                this.currEntry = null;

                this.tstate = UntarState.SKIP;
            case SKIP:
                if (!in.isReadable())
                    continue;

                // check if there is still padding left in the entry we were last reading from
                if (this.remainSkip > 0) {
                    int skipSize = (int) Math.min(this.remainSkip, in.readableBytes());
                    this.remainSkip -= skipSize;

                    //System.out.println("skipping content: " + skipSize);

                    in.skipBytes((int) skipSize);
                }

                if (this.remainSkip > 0)
                    continue;

                this.tstate = UntarState.RECORD;
            }
        }

        in.release();
    }

    // write all messages in the queue
    while (this.outlist.size() > 0) {
        ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0));

        if (ret != ReturnOption.CONTINUE)
            return ret;
    }

    return ReturnOption.CONTINUE;
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private SSLEngineResult wrap(SSLEngine engine, ByteBuf in, ByteBuf out) throws SSLException {
    ByteBuffer in0 = in.nioBuffer();
    if (!in0.isDirect()) {
        ByteBuffer newIn0 = ByteBuffer.allocateDirect(in0.remaining());
        newIn0.put(in0).flip();//from   w  ww  . ja v a2s.c  o m
        in0 = newIn0;
    }

    for (;;) {
        ByteBuffer out0 = out.nioBuffer(out.writerIndex(), out.writableBytes());
        SSLEngineResult result = engine.wrap(in0, out0);
        in.skipBytes(result.bytesConsumed());
        out.writerIndex(out.writerIndex() + result.bytesProduced());

        switch (result.getStatus()) {
        case BUFFER_OVERFLOW:
            out.ensureWritable(maxPacketBufferSize);
            break;
        default:
            return result;
        }
    }
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws SSLException {

    final int startOffset = in.readerIndex();
    final int endOffset = in.writerIndex();
    int offset = startOffset;
    int totalLength = 0;

    // If we calculated the length of the current SSL record before, use that information.
    if (packetLength > 0) {
        if (endOffset - startOffset < packetLength) {
            return;
        } else {/*from   w  w  w . j ava  2 s .com*/
            offset += packetLength;
            totalLength = packetLength;
            packetLength = 0;
        }
    }

    boolean nonSslRecord = false;

    while (totalLength < OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) {
        final int readableBytes = endOffset - offset;
        if (readableBytes < 5) {
            break;
        }

        final int packetLength = getEncryptedPacketLength(in, offset);
        if (packetLength == -1) {
            nonSslRecord = true;
            break;
        }

        assert packetLength > 0;

        if (packetLength > readableBytes) {
            // wait until the whole packet can be read
            this.packetLength = packetLength;
            break;
        }

        int newTotalLength = totalLength + packetLength;
        if (newTotalLength > OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) {
            // Don't read too much.
            break;
        }

        // We have a whole packet.
        // Increment the offset to handle the next packet.
        offset += packetLength;
        totalLength = newTotalLength;
    }

    if (totalLength > 0) {
        // The buffer contains one or more full SSL records.
        // Slice out the whole packet so unwrap will only be called with complete packets.
        // Also directly reset the packetLength. This is needed as unwrap(..) may trigger
        // decode(...) again via:
        // 1) unwrap(..) is called
        // 2) wrap(...) is called from within unwrap(...)
        // 3) wrap(...) calls unwrapLater(...)
        // 4) unwrapLater(...) calls decode(...)
        //
        // See https://github.com/netty/netty/issues/1534

        in.skipBytes(totalLength);
        final ByteBuffer inNetBuf = in.nioBuffer(startOffset, totalLength);
        unwrap(ctx, inNetBuf, totalLength);
        assert !inNetBuf.hasRemaining() || engine.isInboundDone();
    }

    if (nonSslRecord) {
        // Not an SSL/TLS packet
        NotSslRecordException e = new NotSslRecordException(
                "not an SSL/TLS record: " + ByteBufUtil.hexDump(in));
        in.skipBytes(in.readableBytes());
        ctx.fireExceptionCaught(e);
        setHandshakeFailure(e);
    }
}

From source file:divconq.pgp.EncryptedFileStream.java

License:Open Source License

public void writeData(byte[] bytes, int offset, int len) {
    // the first time this is called we need to write headers - those headers
    // call into this method so clear flag immediately
    if (!this.writeFirst) {
        this.writeFirst = true;
        this.writeFirstLiteral(len);
    }// ww w .  j  a  v  a2  s .c o m

    int remaining = len;
    int avail = this.packetsize - this.packetpos;

    // packetbuf may have data that has not yet been processed, so if we are doing any writes
    // we need to write the packet buffer first
    ByteBuf pbb = this.packetbuf;

    if (pbb != null) {
        int bbremaining = pbb.readableBytes();

        // only write if there is space available in current packet or if we have a total
        // amount of data larger than max packet size
        while ((bbremaining > 0) && ((avail > 0) || (bbremaining + remaining) >= MAX_PACKET_SIZE)) {
            // out of current packet space? create more packets
            if (avail == 0) {
                this.packetsize = MAX_PACKET_SIZE;
                this.packetpos = 0;

                this.writeDataInternal((byte) MAX_PARTIAL_LEN); // partial packet length

                avail = this.packetsize;
            }

            // figure out how much we can write to the current packet, write it, update indexes
            int alen = Math.min(avail, bbremaining);

            this.writeDataInternal(pbb.array(), pbb.arrayOffset() + pbb.readerIndex(), alen);

            pbb.skipBytes(alen);
            bbremaining = pbb.readableBytes();
            this.packetpos += alen;
            avail = this.packetsize - this.packetpos;

            // our formula always assumes that packetbuf starts at zero offset, anytime
            // we write out part of the packetbuf we either need to write it all and clear it
            // or we need to start with a new buffer with data starting at offset 0
            if (bbremaining == 0) {
                pbb.clear();
            } else {
                ByteBuf npb = Hub.instance.getBufferAllocator().heapBuffer(MAX_PACKET_SIZE);
                npb.writeBytes(pbb, bbremaining);
                this.packetbuf = npb;

                pbb.release();
                pbb = npb;
            }
        }
    }

    // only write if there is space available in current packet or if we have a total
    // amount of data larger than max packet size
    while ((remaining > 0) && ((avail > 0) || (remaining >= MAX_PACKET_SIZE))) {
        // out of current packet space? create more packets
        if (avail == 0) {
            this.packetsize = MAX_PACKET_SIZE;
            this.packetpos = 0;

            this.writeDataInternal((byte) MAX_PARTIAL_LEN); // partial packet length

            avail = this.packetsize;
        }

        // figure out how much we can write to the current packet, write it, update indexes
        int alen = Math.min(avail, remaining);

        this.writeDataInternal(bytes, offset, alen);

        remaining -= alen;
        offset += alen;
        this.packetpos += alen;
        avail = this.packetsize - this.packetpos;
    }

    // buffer remaining to build larger packet later
    if (remaining > 0) {
        if (this.packetbuf == null)
            this.packetbuf = Hub.instance.getBufferAllocator().heapBuffer(MAX_PACKET_SIZE);

        // add to new buffer or add to existing buffer, either way it should be less than max here
        this.packetbuf.writeBytes(bytes, offset, remaining);
    }
}

From source file:dorkbox.util.storage.DefaultStorageSerializationManager.java

License:Apache License

@Override
public Object read(final ByteBuf buffer, final int length) throws IOException {
    final Input input = new Input();
    buffer.readBytes(input.getBuffer());

    final Object o = readFullClassAndObject(input);
    buffer.skipBytes(input.position());

    return o;/*  w  ww .  j a  v  a  2s . c  o  m*/
}

From source file:herddb.utils.ByteBufUtils.java

License:Apache License

public static final void skipArray(ByteBuf buffer) {
    final int len = readVInt(buffer);
    buffer.skipBytes(len);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Retrieves a domain name given a buffer containing a DNS packet. If the
 * name contains a pointer, the position of the buffer will be set to
 * directly after the pointer's index after the name has been read.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return the domain name for an entry//from   w  w w.j  av a  2 s.c om
 */
public static String readName(ByteBuf buf) {
    int position = -1;
    StringBuilder name = new StringBuilder();
    for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) {
        boolean pointer = (len & 0xc0) == 0xc0;
        if (pointer) {
            if (position == -1) {
                position = buf.readerIndex() + 1;
            }
            buf.readerIndex((len & 0x3f) << 8 | buf.readUnsignedByte());
        } else {
            name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append(".");
            buf.skipBytes(len);
        }
    }
    if (position != -1) {
        buf.readerIndex(position);
    }
    if (name.length() == 0) {
        return null;
    }
    return name.substring(0, name.length() - 1);
}

From source file:io.airlift.drift.transport.netty.codec.HeaderTransport.java

License:Apache License

public static Optional<FrameInfo> tryDecodeFrameInfo(ByteBuf input) {
    ByteBuf buffer = input.retainedDuplicate();
    try {/*from  ww  w .  j a v  a  2 s .c o m*/
        if (buffer.readableBytes() < FRAME_HEADER_SIZE) {
            return Optional.empty();
        }
        // skip magic
        buffer.readShort();
        short flags = buffer.readShort();
        boolean outOfOrderResponse = (flags & FLAG_SUPPORT_OUT_OF_ORDER_MASK) == 1;
        int headerSequenceId = buffer.readInt();
        int headerSize = buffer.readShort() << 2;

        if (buffer.readableBytes() < headerSize) {
            return Optional.empty();
        }

        byte protocolId = buffer.getByte(buffer.readerIndex());
        Protocol protocol = Protocol.getProtocolByHeaderTransportId(protocolId);

        buffer.skipBytes(headerSize);
        SimpleFrameInfoDecoder simpleFrameInfoDecoder = new SimpleFrameInfoDecoder(HEADER, protocol,
                outOfOrderResponse);
        Optional<FrameInfo> frameInfo = simpleFrameInfoDecoder.tryDecodeFrameInfo(buffer);
        if (frameInfo.isPresent()) {
            int messageSequenceId = frameInfo.get().getSequenceId();
            checkArgument(headerSequenceId == messageSequenceId,
                    "Sequence ids don't match. headerSequenceId: %s. messageSequenceId: %s", headerSequenceId,
                    messageSequenceId);
        }
        return frameInfo;
    } finally {
        buffer.release();
    }
}

From source file:io.airlift.drift.transport.netty.codec.ThriftFramedDecoder.java

License:Apache License

private void discardTooLongFrame(ByteBuf buffer) {
    // readableBytes returns int, toIntExact must be safe
    int bytesToSkip = toIntExact(min(bytesToDiscard, buffer.readableBytes()));
    buffer.skipBytes(bytesToSkip);
    bytesToDiscard -= bytesToSkip;/*from  w w  w.j a  va2  s .  c  om*/

    if (bytesToDiscard == 0) {
        RuntimeException exception = new FrameTooLargeException(tooLongFrameInfo, tooLongFrameSizeInBytes,
                maxFrameSizeInBytes);
        tooLongFrameInfo = Optional.empty();
        tooLongFrameSizeInBytes = 0;
        throw exception;
    }
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java

License:Apache License

static String readString(ByteBuf buffer, int length, Charset charset) {
    if (buffer.isDirect()) {
        final String result = buffer.toString(buffer.readerIndex(), length, charset);
        buffer.skipBytes(length);
        return result;
    } else if (buffer.hasArray()) {
        final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length,
                charset);//  w ww.j av a2s.  c o m
        buffer.skipBytes(length);
        return result;
    } else {
        final byte[] array = new byte[length];
        buffer.readBytes(array);
        return new String(array, charset);
    }
}