Search in sources :

Example 1 with Encoder

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

Aggregations

Decoder (lzma.sdk.lzma.Decoder)1 Encoder (lzma.sdk.lzma.Encoder)1