Search in sources :

Example 1 with Decoder

use of lzma.sdk.lzma.Decoder in project lzma-java by jponge.

the class DecodingTest method testDecodeFileWithoutEndOfStreamMarker.

public void testDecodeFileWithoutEndOfStreamMarker() throws IOException {
    final InputStream compressedFileStream = FileUtils.openInputStream(new File("target/test-classes/hello.txt.lzma"));
    @SuppressWarnings("unchecked") final List<String> lines = IOUtils.readLines(new LzmaInputStream(compressedFileStream, new Decoder()));
    assertEquals(1, lines.size());
    assertEquals("Hello World !", lines.get(0));
}
Also used : InputStream(java.io.InputStream) Decoder(lzma.sdk.lzma.Decoder) File(java.io.File)

Example 2 with Decoder

use of lzma.sdk.lzma.Decoder in project lzma-java by jponge.

the class RoundtripTest method performRoundtrip.

public void performRoundtrip(final File sourceFile) throws IOException {
    final File compressedFile = new File(sourceFile.getParentFile(), sourceFile.getName() + ".coder.lzma");
    final File decompressedFile = new File(sourceFile.getParentFile(), sourceFile.getName() + ".coder.unlzma");
    InputStream in = null;
    OutputStream out = null;
    // compressing
    in = new BufferedInputStream(new FileInputStream(sourceFile));
    out = new BufferedOutputStream(new FileOutputStream(compressedFile));
    final Encoder encoder = new Encoder();
    encoder.setDictionarySize(1 << 23);
    encoder.setEndMarkerMode(true);
    encoder.setMatchFinder(Encoder.EMatchFinderTypeBT4);
    encoder.setNumFastBytes(0x20);
    encoder.writeCoderProperties(out);
    long fileSize = sourceFile.length();
    for (int i = 0; i < 8; i++) {
        out.write((int) (fileSize >>> (8 * i)) & 0xFF);
    }
    encoder.code(in, out, -1, -1, null);
    out.flush();
    out.close();
    in.close();
    // decompressing
    in = new BufferedInputStream(new FileInputStream(compressedFile));
    out = new BufferedOutputStream(new FileOutputStream(decompressedFile));
    int propertiesSize = 5;
    byte[] properties = new byte[propertiesSize];
    if (in.read(properties, 0, propertiesSize) != propertiesSize) {
        throw new IOException("input .lzma file is too short");
    }
    Decoder decoder = new Decoder();
    if (!decoder.setDecoderProperties(properties)) {
        throw new IOException("Incorrect stream properties");
    }
    long outSize = 0;
    for (int i = 0; i < 8; i++) {
        int v = in.read();
        if (v < 0) {
            throw new IOException("Can't read stream size");
        }
        outSize |= ((long) v) << (8 * i);
    }
    if (!decoder.code(in, out, outSize)) {
        throw new IOException("Error in data stream");
    }
    out.flush();
    out.close();
    in.close();
    assertTrue("Source and uncompressed content does not equals!", contentEquals(sourceFile, decompressedFile));
    assertFalse("Source and compressed content equals!", contentEquals(sourceFile, compressedFile));
}
Also used : Decoder(lzma.sdk.lzma.Decoder) Encoder(lzma.sdk.lzma.Encoder)

Example 3 with Decoder

use of lzma.sdk.lzma.Decoder 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)

Aggregations

Decoder (lzma.sdk.lzma.Decoder)3 InputStream (java.io.InputStream)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 File (java.io.File)1 Encoder (lzma.sdk.lzma.Encoder)1 LzmaInputStream (lzma.streams.LzmaInputStream)1