Search in sources :

Example 11 with LZ4Exception

use of net.jpountz.lz4.LZ4Exception in project kafka by apache.

the class KafkaLZ4BlockInputStream method readBlock.

/**
 * Decompresses (if necessary) buffered data, optionally computes and validates a XXHash32 checksum, and writes the
 * result to a buffer.
 *
 * @throws IOException
 */
private void readBlock() throws IOException {
    if (in.remaining() < 4) {
        throw new IOException(PREMATURE_EOS);
    }
    int blockSize = in.getInt();
    boolean compressed = (blockSize & LZ4_FRAME_INCOMPRESSIBLE_MASK) == 0;
    blockSize &= ~LZ4_FRAME_INCOMPRESSIBLE_MASK;
    // Check for EndMark
    if (blockSize == 0) {
        finished = true;
        if (flg.isContentChecksumSet())
            // TODO: verify this content checksum
            in.getInt();
        return;
    } else if (blockSize > maxBlockSize) {
        throw new IOException(String.format("Block size %s exceeded max: %s", blockSize, maxBlockSize));
    }
    if (in.remaining() < blockSize) {
        throw new IOException(PREMATURE_EOS);
    }
    if (compressed) {
        try {
            final int bufferSize = DECOMPRESSOR.decompress(in, in.position(), blockSize, decompressionBuffer, 0, maxBlockSize);
            decompressionBuffer.position(0);
            decompressionBuffer.limit(bufferSize);
            decompressedBuffer = decompressionBuffer;
        } catch (LZ4Exception e) {
            throw new IOException(e);
        }
    } else {
        decompressedBuffer = in.slice();
        decompressedBuffer.limit(blockSize);
    }
    // verify checksum
    if (flg.isBlockChecksumSet()) {
        int hash = CHECKSUM.hash(in, in.position(), blockSize, 0);
        in.position(in.position() + blockSize);
        if (hash != in.getInt()) {
            throw new IOException(BLOCK_HASH_MISMATCH);
        }
    } else {
        in.position(in.position() + blockSize);
    }
}
Also used : LZ4Exception(net.jpountz.lz4.LZ4Exception) IOException(java.io.IOException)

Example 12 with LZ4Exception

use of net.jpountz.lz4.LZ4Exception in project netty by netty.

the class Lz4FrameEncoder method flushBufferedData.

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, buffer.readerIndex(), 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();
}
Also used : LZ4Exception(net.jpountz.lz4.LZ4Exception) ByteBuffer(java.nio.ByteBuffer)

Aggregations

LZ4Exception (net.jpountz.lz4.LZ4Exception)12 IOException (java.io.IOException)7 ByteBuffer (java.nio.ByteBuffer)2 Stopwatch (com.google.common.base.Stopwatch)1 ByteBuf (io.netty.buffer.ByteBuf)1 LZ4Compressor (net.jpountz.lz4.LZ4Compressor)1