diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2011-08-05 17:04:36 +0300 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2011-08-05 17:04:36 +0300 |
commit | 299248e618e41e2139d969c3dedaa560d2b8e362 (patch) | |
tree | c8f21b1871b92fbac2616dc2d9620103c101bd41 /src/XZSeekEncDemo.java | |
parent | 1e447d9634c15cf850faa6e6fbad231a1e82ae15 (diff) | |
download | xz-java-299248e618e41e2139d969c3dedaa560d2b8e362.tar.gz |
Add demo programs for random access mode.
Diffstat (limited to 'src/XZSeekEncDemo.java')
-rw-r--r-- | src/XZSeekEncDemo.java | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/XZSeekEncDemo.java b/src/XZSeekEncDemo.java new file mode 100644 index 0000000..785d234 --- /dev/null +++ b/src/XZSeekEncDemo.java @@ -0,0 +1,68 @@ +/* + * XZSeekEncDemo + * + * Author: Lasse Collin <lasse.collin@tukaani.org> + * + * This file has been put into the public domain. + * You can do whatever you want with this file. + */ + +import java.io.*; +import org.tukaani.xz.*; + +/** + * Compresses a single file from standard input to standard ouput into + * a random-accessible .xz file. + * <p> + * Arguments: [preset [block size]] + * <p> + * Preset is an LZMA2 preset level which is an integer in the range [0, 9]. + * The default is 6. + * <p> + * Block size specifies the amount of uncompressed data to store per + * XZ Block. The default is 1 MiB (1048576 bytes). Bigger means better + * compression ratio. Smaller means faster random access. + */ +class XZSeekEncDemo { + public static void main(String[] args) throws Exception { + LZMA2Options options = new LZMA2Options(); + + if (args.length >= 1) + options.setPreset(Integer.parseInt(args[0])); + + int blockSize = 1024 * 1024; + if (args.length >= 2) + blockSize = Integer.parseInt(args[1]); + + options.setDictSize(Math.min(options.getDictSize(), + Math.max(LZMA2Options.DICT_SIZE_MIN, + blockSize))); + + System.err.println("Encoder memory usage: " + + options.getEncoderMemoryUsage() + " KiB"); + System.err.println("Decoder memory usage: " + + options.getDecoderMemoryUsage() + " KiB"); + System.err.println("Block size: " + blockSize + " B"); + + XZOutputStream out = new XZOutputStream(System.out, options); + + byte[] buf = new byte[8192]; + int left = blockSize; + + while (true) { + int size = System.in.read(buf, 0, Math.min(buf.length, left)); + if (size == -1) + break; + + out.write(buf, 0, size); + left -= size; + + if (left == 0) { + out.flushBlock(); + left = blockSize; + } + } + + out.finish(); + } +} |