Search in sources :

Example 1 with LZ4BlockInputStream

use of net.jpountz.lz4.LZ4BlockInputStream in project sonarqube by SonarSource.

the class FileSourceDto method decodeHugeSourceData.

private static DbFileSources.Data decodeHugeSourceData(byte[] binaryData) throws IOException {
    try (LZ4BlockInputStream lz4Input = new LZ4BlockInputStream(new ByteArrayInputStream(binaryData))) {
        CodedInputStream input = CodedInputStream.newInstance(lz4Input);
        input.setSizeLimit(Integer.MAX_VALUE);
        return DbFileSources.Data.parseFrom(input);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CodedInputStream(com.google.protobuf.CodedInputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream)

Example 2 with LZ4BlockInputStream

use of net.jpountz.lz4.LZ4BlockInputStream in project cassandra by apache.

the class IncomingTcpConnection method receiveMessages.

// Not closing constructed DataInputPlus's as the stream needs to remain open.
@SuppressWarnings("resource")
private void receiveMessages() throws IOException {
    // handshake (true) endpoint versions
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // if this version is < the MS version the other node is trying
    // to connect with, the other node will disconnect
    out.writeInt(MessagingService.current_version);
    out.flush();
    DataInputPlus in = new DataInputStreamPlus(socket.getInputStream());
    int maxVersion = in.readInt();
    // outbound side will reconnect if necessary to upgrade version
    assert version <= MessagingService.current_version;
    from = CompactEndpointSerializationHelper.deserialize(in);
    // record the (true) version of the endpoint
    MessagingService.instance().setVersion(from, maxVersion);
    logger.trace("Set version for {} to {} (will use {})", from, maxVersion, MessagingService.instance().getVersion(from));
    if (compressed) {
        logger.trace("Upgrading incoming connection to be compressed");
        LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
        Checksum checksum = XXHashFactory.fastestInstance().newStreamingHash32(OutboundTcpConnection.LZ4_HASH_SEED).asChecksum();
        in = new DataInputStreamPlus(new LZ4BlockInputStream(socket.getInputStream(), decompressor, checksum));
    } else {
        ReadableByteChannel channel = socket.getChannel();
        in = new NIODataInputStream(channel != null ? channel : Channels.newChannel(socket.getInputStream()), BUFFER_SIZE);
    }
    while (true) {
        MessagingService.validateMagic(in.readInt());
        receiveMessage(in, version);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) LZ4FastDecompressor(net.jpountz.lz4.LZ4FastDecompressor) Checksum(java.util.zip.Checksum) DataInputStreamPlus(org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) NIODataInputStream(org.apache.cassandra.io.util.NIODataInputStream)

Example 3 with LZ4BlockInputStream

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

the class Lz4FrameEncoderTest method decompress.

@Override
protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
    InputStream is = new ByteBufInputStream(compressed, true);
    LZ4BlockInputStream lz4Is = null;
    byte[] decompressed = new byte[originalLength];
    try {
        lz4Is = new LZ4BlockInputStream(is);
        int remaining = originalLength;
        while (remaining > 0) {
            int read = lz4Is.read(decompressed, originalLength - remaining, remaining);
            if (read > 0) {
                remaining -= read;
            } else {
                break;
            }
        }
        assertEquals(-1, lz4Is.read());
    } finally {
        if (lz4Is != null) {
            lz4Is.close();
        } else {
            is.close();
        }
    }
    return Unpooled.wrappedBuffer(decompressed);
}
Also used : LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream)

Example 4 with LZ4BlockInputStream

use of net.jpountz.lz4.LZ4BlockInputStream in project sonarqube by SonarSource.

the class FileSourceDto method decodeTestData.

/**
   * Decompress and deserialize content of column FILE_SOURCES.BINARY_DATA.
   * The parameter "input" is always closed by this method.
   */
public static List<DbFileSources.Test> decodeTestData(InputStream binaryInput) {
    LZ4BlockInputStream lz4Input = null;
    List<DbFileSources.Test> tests = new ArrayList<>();
    try {
        lz4Input = new LZ4BlockInputStream(binaryInput);
        DbFileSources.Test currentTest;
        do {
            currentTest = DbFileSources.Test.parseDelimitedFrom(lz4Input);
            if (currentTest != null) {
                tests.add(currentTest);
            }
        } while (currentTest != null);
        return tests;
    } catch (IOException e) {
        throw new IllegalStateException("Fail to decompress and deserialize source data", e);
    } finally {
        IOUtils.closeQuietly(lz4Input);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) DbFileSources(org.sonar.db.protobuf.DbFileSources) LZ4BlockInputStream(net.jpountz.lz4.LZ4BlockInputStream)

Aggregations

LZ4BlockInputStream (net.jpountz.lz4.LZ4BlockInputStream)4 CodedInputStream (com.google.protobuf.CodedInputStream)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 ArrayList (java.util.ArrayList)1 Checksum (java.util.zip.Checksum)1 LZ4FastDecompressor (net.jpountz.lz4.LZ4FastDecompressor)1 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)1 DataInputStreamPlus (org.apache.cassandra.io.util.DataInputPlus.DataInputStreamPlus)1 NIODataInputStream (org.apache.cassandra.io.util.NIODataInputStream)1 DbFileSources (org.sonar.db.protobuf.DbFileSources)1