• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * XZSeekEncDemo
3  *
4  * Author: Lasse Collin <lasse.collin@tukaani.org>
5  *
6  * This file has been put into the public domain.
7  * You can do whatever you want with this file.
8  */
9 
10 import java.io.*;
11 import org.tukaani.xz.*;
12 
13 /**
14  * Compresses a single file from standard input to standard ouput into
15  * a random-accessible .xz file.
16  * <p>
17  * Arguments: [preset [block size]]
18  * <p>
19  * Preset is an LZMA2 preset level which is an integer in the range [0, 9].
20  * The default is 6.
21  * <p>
22  * Block size specifies the amount of uncompressed data to store per
23  * XZ Block. The default is 1 MiB (1048576 bytes). Bigger means better
24  * compression ratio. Smaller means faster random access.
25  */
26 class XZSeekEncDemo {
main(String[] args)27     public static void main(String[] args) throws Exception {
28         LZMA2Options options = new LZMA2Options();
29 
30         if (args.length >= 1)
31             options.setPreset(Integer.parseInt(args[0]));
32 
33         int blockSize = 1024 * 1024;
34         if (args.length >= 2)
35             blockSize = Integer.parseInt(args[1]);
36 
37         options.setDictSize(Math.min(options.getDictSize(),
38                                      Math.max(LZMA2Options.DICT_SIZE_MIN,
39                                               blockSize)));
40 
41         System.err.println("Encoder memory usage: "
42                            + options.getEncoderMemoryUsage() + " KiB");
43         System.err.println("Decoder memory usage: "
44                            + options.getDecoderMemoryUsage() + " KiB");
45         System.err.println("Block size:           " + blockSize + " B");
46 
47         XZOutputStream out = new XZOutputStream(System.out, options);
48 
49         byte[] buf = new byte[8192];
50         int left = blockSize;
51 
52         while (true) {
53             int size = System.in.read(buf, 0, Math.min(buf.length, left));
54             if (size == -1)
55                 break;
56 
57             out.write(buf, 0, size);
58             left -= size;
59 
60             if (left == 0) {
61                 out.endBlock();
62                 left = blockSize;
63             }
64         }
65 
66         out.finish();
67     }
68 }
69