Search in sources :

Example 71 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream 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 72 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class LzmaFrameEncoderTest method decompress.

@Override
protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
    InputStream is = new ByteBufInputStream(compressed, true);
    LzmaInputStream lzmaIs = null;
    byte[] decompressed = new byte[originalLength];
    try {
        lzmaIs = new LzmaInputStream(is, new Decoder());
        int remaining = originalLength;
        while (remaining > 0) {
            int read = lzmaIs.read(decompressed, originalLength - remaining, remaining);
            if (read > 0) {
                remaining -= read;
            } else {
                break;
            }
        }
        assertEquals(-1, lzmaIs.read());
    } finally {
        if (lzmaIs != null) {
            lzmaIs.close();
        }
        // https://github.com/jponge/lzma-java/issues/14
        if (is != null) {
            is.close();
        }
    }
    return Unpooled.wrappedBuffer(decompressed);
}
Also used : LzmaInputStream(lzma.streams.LzmaInputStream) LzmaInputStream(lzma.streams.LzmaInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(lzma.sdk.lzma.Decoder)

Example 73 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class Bzip2EncoderTest method decompress.

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

Example 74 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class LzmaFrameEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
    final int length = in.readableBytes();
    InputStream bbIn = null;
    ByteBufOutputStream bbOut = null;
    try {
        bbIn = new ByteBufInputStream(in);
        bbOut = new ByteBufOutputStream(out);
        bbOut.writeByte(properties);
        bbOut.writeInt(littleEndianDictionarySize);
        bbOut.writeLong(Long.reverseBytes(length));
        encoder.code(bbIn, bbOut, -1, -1, null);
    } finally {
        if (bbIn != null) {
            bbIn.close();
        }
        if (bbOut != null) {
            bbOut.close();
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 75 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class SslContext method getCertificatesFromBuffers.

private static X509Certificate[] getCertificatesFromBuffers(ByteBuf[] certs) throws CertificateException {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] x509Certs = new X509Certificate[certs.length];
    try {
        for (int i = 0; i < certs.length; i++) {
            InputStream is = new ByteBufInputStream(certs[i], false);
            try {
                x509Certs[i] = (X509Certificate) cf.generateCertificate(is);
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // This is not expected to happen, but re-throw in case it does.
                    throw new RuntimeException(e);
                }
            }
        }
    } finally {
        for (ByteBuf buf : certs) {
            buf.release();
        }
    }
    return x509Certs;
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)78 IOException (java.io.IOException)28 ByteBuf (io.netty.buffer.ByteBuf)27 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 InputStream (java.io.InputStream)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 DataInputStream (java.io.DataInputStream)7 POST (javax.ws.rs.POST)6 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3