Java Code Examples for io.netty.buffer.ByteBuf#setLong()

The following examples show how to use io.netty.buffer.ByteBuf#setLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) {
    if (finished) {
        promise.setSuccess();
        return promise;
    }
    finished = true;

    final ByteBuf footer = ctx.alloc().heapBuffer(
            compressor.maxCompressedLength(buffer.readableBytes()) + HEADER_LENGTH);
    flushBufferedData(footer);

    final int idx = footer.writerIndex();
    footer.setLong(idx, MAGIC_NUMBER);
    footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel));
    footer.setInt(idx + COMPRESSED_LENGTH_OFFSET, 0);
    footer.setInt(idx + DECOMPRESSED_LENGTH_OFFSET, 0);
    footer.setInt(idx + CHECKSUM_OFFSET, 0);

    footer.writerIndex(idx + HEADER_LENGTH);

    return ctx.writeAndFlush(footer, promise);
}
 
Example 2
Source File: DirectMemoryBuffer.java    From pravega with Apache License 2.0 6 votes vote down vote up
/**
 * Rolls back a partially executed call to {@link #write} that failed while being written to the buffer. This walks
 * back the chain of blocks that were written, marks them as free and re-chains them into the free block chain. This
 * method is a simpler version of {@link #deallocateBlocks} that does not attempt to merge two sorted lists and does
 * not require the root metadata to have been properly updated (since it likely wasn't).
 * @param lastWrittenBlockId The id of the last Block id that has been written and had its metadata updated.
 * @param nextFreeBlockId    The id of the next free Block id after `lastWrittenBlockId`. Usually this is the id of
 *                           the block for which the write failed but for which we couldn't update the metadata yet.
 */
@GuardedBy("this")
private void rollbackWrite(int lastWrittenBlockId, int nextFreeBlockId) {
    ByteBuf metadataBuf = getMetadataBlock();
    int blockId = lastWrittenBlockId;
    while (blockId != CacheLayout.NO_BLOCK_ID) {
        // Get block metadata and fetch the predecessor address, before we reset it.
        int bufIndex = blockId * this.layout.blockMetadataSize();
        long blockMetadata = metadataBuf.getLong(bufIndex);
        int predecessorAddress = this.layout.getPredecessorAddress(blockMetadata);

        // Reset the block metadata.
        blockMetadata = this.layout.setNextFreeBlockId(this.layout.emptyBlockMetadata(), nextFreeBlockId);
        metadataBuf.setLong(bufIndex, blockMetadata);
        nextFreeBlockId = blockId;
        this.usedBlockCount--;

        // Determine the previous block to rollback.
        blockId = this.layout.getBlockId(predecessorAddress);
        if (this.layout.getBufferId(predecessorAddress) != this.id) {
            break;
        }
    }
}
 
Example 3
Source File: Lz4FrameEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void flushBufferedData(ByteBuf out) {
    int flushableBytes = buffer.readableBytes();
    if (flushableBytes == 0) {
        return;
    }
    checksum.reset();
    checksum.update(buffer, buffer.readerIndex(), flushableBytes);
    final int check = (int) checksum.getValue();

    final int bufSize = compressor.maxCompressedLength(flushableBytes) + HEADER_LENGTH;
    out.ensureWritable(bufSize);
    final int idx = out.writerIndex();
    int compressedLength;
    try {
        ByteBuffer outNioBuffer = out.internalNioBuffer(idx + HEADER_LENGTH, out.writableBytes() - HEADER_LENGTH);
        int pos = outNioBuffer.position();
        // We always want to start at position 0 as we take care of reusing the buffer in the encode(...) loop.
        compressor.compress(buffer.internalNioBuffer(buffer.readerIndex(), flushableBytes), outNioBuffer);
        compressedLength = outNioBuffer.position() - pos;
    } catch (LZ4Exception e) {
        throw new CompressionException(e);
    }
    final int blockType;
    if (compressedLength >= flushableBytes) {
        blockType = BLOCK_TYPE_NON_COMPRESSED;
        compressedLength = flushableBytes;
        out.setBytes(idx + HEADER_LENGTH, buffer, 0, flushableBytes);
    } else {
        blockType = BLOCK_TYPE_COMPRESSED;
    }

    out.setLong(idx, MAGIC_NUMBER);
    out.setByte(idx + TOKEN_OFFSET, (byte) (blockType | compressionLevel));
    out.setIntLE(idx + COMPRESSED_LENGTH_OFFSET, compressedLength);
    out.setIntLE(idx + DECOMPRESSED_LENGTH_OFFSET, flushableBytes);
    out.setIntLE(idx + CHECKSUM_OFFSET, check);
    out.writerIndex(idx + HEADER_LENGTH + compressedLength);
    buffer.clear();
}
 
Example 4
Source File: DirectMemoryBuffer.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to append some data to an entry.
 *
 * @param blockId        The id of the Buffer-Block to append that resides in this buffer.
 * @param expectedLength The expected length of the Buffer-Block to append to. If this value does not match what is
 *                       stored in that Buffer-Block metdata, the append will not execute.
 * @param data           A {@link BufferView} representing the data to append. The {@link BufferView#copyTo(ByteBuffer)}
 *                       method will be used to transfer the data to the appropriate Buffer Block.
 * @return The number of bytes appended.
 * @throws IncorrectCacheEntryLengthException If `expectedLastBlockLength` differs from the last block's length.
 * @throws IllegalArgumentException If blockId does not point to an allocated Block.
 */
synchronized int tryAppend(int blockId, int expectedLength, BufferView data) {
    validateBlockId(blockId, false);

    ByteBuf metadataBuf = getMetadataBlock();
    int bufIndex = blockId * this.layout.blockMetadataSize();
    long blockMetadata = metadataBuf.getLong(bufIndex);
    Preconditions.checkArgument(blockId != CacheLayout.NO_BLOCK_ID && this.layout.isUsedBlock(blockMetadata),
            "Given blockId is not allocated.");

    // Validate that the given length matches the actual one.
    int blockLength = this.layout.getLength(blockMetadata);
    if (blockLength != expectedLength) {
        throw new IncorrectCacheEntryLengthException(String.format(
                "Incorrect last block length. Expected %s, given %s.", blockLength, expectedLength));
    }

    // Adjust the length, if we were given more than we can fit.
    int maxLength = this.layout.blockSize() - blockLength;
    if (maxLength < data.getLength()) {
        data = data.slice(0, maxLength);
    }

    // Copy the data.
    data.copyTo(getWriteableBlock(blockId, blockLength));
    blockLength += data.getLength();

    // Update metadata.
    blockMetadata = this.layout.setLength(blockMetadata, blockLength);
    metadataBuf.setLong(bufIndex, blockMetadata);
    return data.getLength();
}
 
Example 5
Source File: DirectMemoryBuffer.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the given Buffer-Blocks as free and makes them available for re-writing.
 *
 * @param blocks      A {@link Stack} of Buffer-Block ids. When invoking {@link Stack#pop()}, the blocks should be
 *                    returned in ascending order.
 * @param metadataBuf A {@link ByteBuf} representing the metadata buffer.
 */
@GuardedBy("this")
private void deallocateBlocks(Stack<Integer> blocks, ByteBuf metadataBuf) {
    if (blocks.size() == 0) {
        return;
    }

    // Find the highest empty block that is before the first block (P).
    int freedBlockId = blocks.pop();
    int prevBlockId = freedBlockId;
    long prevMetadata;
    do {
        prevBlockId--;
        prevMetadata = metadataBuf.getLong(prevBlockId * this.layout.blockMetadataSize());
    } while (prevBlockId > 0 && this.layout.isUsedBlock(prevMetadata));

    // Loop as long as there are no more freed blocks (F). At each iteration:
    // If P.Next < F, update F.next=P.next, P.next=F
    while (freedBlockId != CacheLayout.NO_BLOCK_ID) {
        // If the current free block (prevBlockId) has a free successor that is smaller than freedBlockId, then
        // follow the free block list until we find a successor that is after freedBlockId or reach the end.
        int prevNextFreeBlockId = this.layout.getNextFreeBlockId(prevMetadata);
        while (prevNextFreeBlockId != CacheLayout.NO_BLOCK_ID && prevNextFreeBlockId < freedBlockId) {
            prevBlockId = prevNextFreeBlockId;
            prevMetadata = metadataBuf.getLong(prevBlockId * this.layout.blockMetadataSize());
            prevNextFreeBlockId = this.layout.getNextFreeBlockId(prevMetadata);
        }

        // Point our newly freed block to this previous block's next free block id.
        long blockMetadata = this.layout.setNextFreeBlockId(this.layout.emptyBlockMetadata(), prevNextFreeBlockId);

        // Point this previous block to our newly freed block as the next free block in the chain.
        prevMetadata = this.layout.setNextFreeBlockId(prevMetadata, freedBlockId);
        metadataBuf.setLong(prevBlockId * this.layout.blockMetadataSize(), prevMetadata);
        metadataBuf.setLong(freedBlockId * this.layout.blockMetadataSize(), blockMetadata);

        freedBlockId = blocks.size() == 0 ? CacheLayout.NO_BLOCK_ID : blocks.pop();
        this.usedBlockCount--;
    }
}
 
Example 6
Source File: AuthPacket.java    From Mycat-Balance with Apache License 2.0 5 votes vote down vote up
@Override
public void encodeBody(ByteBuf byteBuf)
{
	int index = byteBuf.readerIndex();
	String xx = Long.toBinaryString(clientFlags);
	byteBuf.setLong(index, clientFlags);
	index += 4;

	byteBuf.setLong(index, maxPacketSize);
	index += 4;

	byteBuf.setByte(index, charsetIndex);
	index++;

	byteBuf.setBytes(index, extra);
	index += extra.length;

	byteBuf.setBytes(index, user);
	index += user.length;

	byteBuf.setByte(index, 0);
	index++;

	byteBuf.setByte(index, passwordLen);
	index++;

	byteBuf.setBytes(index, password);
	index += password.length;

	byteBuf.setBytes(index, database);
	index += database.length;

	byteBuf.setByte(index, 0);
	index++;
}