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:org.apache.activemq.artemis.utils.UTF8Util.java

License:Apache License

public static String readUTF(final ActiveMQBuffer input) {
    StringUtilBuffer buffer = UTF8Util.getThreadLocalBuffer();

    final int size = input.readUnsignedShort();

    if (UTF8Util.isTrace) {
        // This message is too verbose for debug, that's why we are using trace here
        ActiveMQUtilLogger.LOGGER.trace("Reading string with utfSize=" + size);
    }/*from   ww  w.  j a va2s  .c o  m*/
    if (PlatformDependent.hasUnsafe() && input.byteBuf() != null && input.byteBuf().hasMemoryAddress()) {
        final ByteBuf byteBuf = input.byteBuf();
        final long addressBytes = byteBuf.memoryAddress();
        final int index = byteBuf.readerIndex();
        byteBuf.skipBytes(size);
        final char[] chars = buffer.borrowCharBuffer(size);
        return unsafeOffHeapReadUTF(addressBytes, index, chars, size);
    }
    final byte[] bytes;
    final int index;
    if (input.byteBuf() != null && input.byteBuf().hasArray()) {
        final ByteBuf byteBuf = input.byteBuf();
        bytes = byteBuf.array();
        index = byteBuf.arrayOffset() + byteBuf.readerIndex();
        byteBuf.skipBytes(size);
    } else {
        bytes = buffer.borrowByteBuffer(size);
        index = 0;
        input.readBytes(bytes, 0, size);
    }
    final char[] chars = buffer.borrowCharBuffer(size);
    if (PlatformDependent.hasUnsafe()) {
        return unsafeOnHeapReadUTF(bytes, index, chars, size);
    } else {
        return readUTF(bytes, index, chars, size);
    }
}

From source file:org.apache.bookkeeper.bookie.BookieShell.java

License:Apache License

/**
 * Format the message into a readable format.
 *
 * @param pos//from w ww  .j  av  a  2  s .  co  m
 *          File offset of the message stored in entry log file
 * @param recBuff
 *          Entry Data
 * @param printMsg
 *          Whether printing the message body
 */
private void formatEntry(long pos, ByteBuf recBuff, boolean printMsg) {
    int entrySize = recBuff.readableBytes();
    long ledgerId = recBuff.readLong();
    long entryId = recBuff.readLong();

    System.out.println("--------- Lid=" + ledgerIdFormatter.formatLedgerId(ledgerId) + ", Eid=" + entryId
            + ", ByteOffset=" + pos + ", EntrySize=" + entrySize + " ---------");
    if (entryId == Bookie.METAENTRY_ID_LEDGER_KEY) {
        int masterKeyLen = recBuff.readInt();
        byte[] masterKey = new byte[masterKeyLen];
        recBuff.readBytes(masterKey);
        System.out.println("Type:           META");
        System.out.println("MasterKey:      " + bytes2Hex(masterKey));
        System.out.println();
        return;
    }
    if (entryId == Bookie.METAENTRY_ID_FENCE_KEY) {
        System.out.println("Type:           META");
        System.out.println("Fenced");
        System.out.println();
        return;
    }
    // process a data entry
    long lastAddConfirmed = recBuff.readLong();
    System.out.println("Type:           DATA");
    System.out.println("LastConfirmed:  " + lastAddConfirmed);
    if (!printMsg) {
        System.out.println();
        return;
    }
    // skip digest checking
    recBuff.skipBytes(8);
    System.out.println("Data:");
    System.out.println();
    try {
        byte[] ret = new byte[recBuff.readableBytes()];
        recBuff.readBytes(ret);
        entryFormatter.formatEntry(ret);
    } catch (Exception e) {
        System.out.println("N/A. Corrupted.");
    }
    System.out.println();
}

From source file:org.apache.bookkeeper.bookie.InterleavedLedgerStorage.java

License:Apache License

@Override
public long getLastAddConfirmed(long ledgerId) throws IOException {
    Long lac = ledgerCache.getLastAddConfirmed(ledgerId);
    if (lac == null) {
        ByteBuf bb = getEntry(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED);
        if (null == bb) {
            return BookieProtocol.INVALID_ENTRY_ID;
        } else {/*from  www  .  jav  a2  s. c  o m*/
            try {
                bb.skipBytes(2 * Long.BYTES); // skip ledger & entry id
                lac = bb.readLong();
                lac = ledgerCache.updateLastAddConfirmed(ledgerId, lac);
            } finally {
                bb.release();
            }
        }
    }
    return lac;
}

From source file:org.apache.bookkeeper.bookie.storage.ldb.SingleDirectoryDbLedgerStorage.java

License:Apache License

@Override
public long getLastAddConfirmed(long ledgerId) throws IOException {
    TransientLedgerInfo ledgerInfo = transientLedgerInfoCache.get(ledgerId);
    long lac = null != ledgerInfo ? ledgerInfo.getLastAddConfirmed() : TransientLedgerInfo.NOT_ASSIGNED_LAC;
    if (lac == TransientLedgerInfo.NOT_ASSIGNED_LAC) {
        ByteBuf bb = getEntry(ledgerId, BookieProtocol.LAST_ADD_CONFIRMED);
        try {//from  w  w  w  .  j a v a  2 s.  c  o  m
            bb.skipBytes(2 * Long.BYTES); // skip ledger id and entry id
            lac = bb.readLong();
            lac = getOrAddLedgerInfo(ledgerId).setLastAddConfirmed(lac);
        } finally {
            bb.release();
        }
    }
    return lac;
}

From source file:org.apache.bookkeeper.statelib.impl.kv.KVUtils.java

License:Apache License

static <V> V deserialize(Coder<V> valCoder, ByteBuf valBuf) {
    valBuf.skipBytes(Long.BYTES);
    return valCoder.decode(valBuf);
}

From source file:org.apache.bookkeeper.statelib.impl.mvcc.MVCCRecordCoder.java

License:Apache License

@Override
public MVCCRecord decode(ByteBuf data) {
    ByteBuf copy = data.slice();

    int metaLen = copy.readInt();
    ByteBuffer metaBuf = copy.slice(copy.readerIndex(), metaLen).nioBuffer();
    KeyMeta meta;/*from   w w w  . j a v  a2 s .  c o  m*/
    try {
        meta = KeyMeta.parseFrom(metaBuf);
    } catch (InvalidProtocolBufferException e) {
        throw new StateStoreRuntimeException("Failed to deserialize key metadata", e);
    }
    copy.skipBytes(metaLen);
    int valLen = copy.readInt();
    ByteBuf valBuf = copy.retainedSlice(copy.readerIndex(), valLen);

    MVCCRecord record = MVCCRecord.newRecord();
    record.setCreateRev(meta.getCreateRevision());
    record.setModRev(meta.getModRevision());
    record.setVersion(meta.getVersion());
    record.setValue(valBuf, meta.getValueType());
    return record;
}

From source file:org.apache.bookkeeper.tools.cli.commands.bookie.FormatUtil.java

License:Apache License

/**
 * Format the message into a readable format.
 * @param pos//from w  w  w  . j  a v  a  2 s .c om
 *          File offset of the message stored in entry log file
 * @param recBuff
 *          Entry Data
 * @param printMsg
 *          Whether printing the message body
 * @param ledgerIdFormatter
 * @param entryFormatter
 */
public static void formatEntry(long pos, ByteBuf recBuff, boolean printMsg, LedgerIdFormatter ledgerIdFormatter,
        EntryFormatter entryFormatter) {
    int entrySize = recBuff.readableBytes();
    long ledgerId = recBuff.readLong();
    long entryId = recBuff.readLong();

    System.out.println("--------- Lid=" + ledgerIdFormatter.formatLedgerId(ledgerId) + ", Eid=" + entryId
            + ", ByteOffset=" + pos + ", EntrySize=" + entrySize + " ---------");
    if (entryId == Bookie.METAENTRY_ID_LEDGER_KEY) {
        int masterKeyLen = recBuff.readInt();
        byte[] masterKey = new byte[masterKeyLen];
        recBuff.readBytes(masterKey);
        System.out.println("Type:           META");
        System.out.println("MasterKey:      " + bytes2Hex(masterKey));
        System.out.println();
        return;
    }
    if (entryId == Bookie.METAENTRY_ID_FENCE_KEY) {
        System.out.println("Type:           META");
        System.out.println("Fenced");
        System.out.println();
        return;
    }
    // process a data entry
    long lastAddConfirmed = recBuff.readLong();
    System.out.println("Type:           DATA");
    System.out.println("LastConfirmed:  " + lastAddConfirmed);
    if (!printMsg) {
        System.out.println();
        return;
    }
    // skip digest checking
    recBuff.skipBytes(8);
    System.out.println("Data:");
    System.out.println();
    try {
        byte[] ret = new byte[recBuff.readableBytes()];
        recBuff.readBytes(ret);
        entryFormatter.formatEntry(ret);
    } catch (Exception e) {
        System.out.println("N/A. Corrupted.");
    }
    System.out.println();
}

From source file:org.apache.cassandra.transport.Frame.java

License:Apache License

private static long discard(ByteBuf buffer, long remainingToDiscard) {
    int availableToDiscard = (int) Math.min(remainingToDiscard, buffer.readableBytes());
    buffer.skipBytes(availableToDiscard);
    return remainingToDiscard - availableToDiscard;
}

From source file:org.apache.distributedlog.LogRecord.java

License:Apache License

protected void readPayload(ByteBuf in, boolean copyData) throws IOException {
    int length = in.readInt();
    if (length < 0) {
        throw new EOFException("Log Record is corrupt: Negative length " + length);
    }//  ww  w.ja va 2s.  c o  m
    if (copyData) {
        setPayloadBuf(in.slice(in.readerIndex(), length), true);
    } else {
        setPayloadBuf(in.retainedSlice(in.readerIndex(), length), false);
    }
    in.skipBytes(length);
}

From source file:org.apache.drill.exec.rpc.ChunkCreationHandler.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {

    if (RpcConstants.EXTRA_DEBUGGING) {
        logger.debug("ChunkCreationHandler called with msg {} of size {} with chunkSize {}", msg,
                msg.readableBytes(), chunkSize);
    }/* w  w w. j ava 2  s .  c  o  m*/

    if (!ctx.channel().isOpen()) {
        logger.debug("Channel closed, skipping encode inside {}.", RpcConstants.CHUNK_CREATION_HANDLER);
        msg.release();
        return;
    }

    // Calculate the number of chunks based on configured chunk size and input msg size
    int numChunks = (int) Math.ceil((double) msg.readableBytes() / chunkSize);

    // Initialize a composite buffer to hold numChunks chunk.
    final CompositeByteBuf cbb = ctx.alloc().compositeBuffer(numChunks);

    int cbbWriteIndex = 0;
    int currentChunkLen = min(msg.readableBytes(), chunkSize);

    // Create slices of chunkSize from input msg and add it to the composite buffer.
    while (numChunks > 0) {
        final ByteBuf chunkBuf = msg.slice(msg.readerIndex(), currentChunkLen);
        chunkBuf.retain();
        cbb.addComponent(chunkBuf);
        cbbWriteIndex += currentChunkLen;
        msg.skipBytes(currentChunkLen);
        --numChunks;
        currentChunkLen = min(msg.readableBytes(), chunkSize);
    }

    // Update the writerIndex of composite byte buffer. Netty doesn't do it automatically.
    cbb.writerIndex(cbbWriteIndex);

    // Add the final composite bytebuf into output buffer.
    out.add(cbb);
}