Skip to content

Commit 5c9dd69

Browse files
committed
Enhancements to the readme file before the 1.0 release.
1 parent 3ab3408 commit 5c9dd69

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

README.md

+64-6
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ This library provides LZMA compression for applications that run on the Java pla
77
## Background ##
88

99
This library is based on the [Java LZMA SDK](http://www.7-zip.org/sdk.html) by Igor Pavlov.
10+
It provides some deserved enhancements.
1011

11-
While the code works just fine, any potential user will quickly realize that:
12+
While the original code works just fine, it has some serious issues for Java developers:
1213

13-
* this is a straight port of non-object procedural code written in C to Java, and
14+
* this is a straight port of non-object, procedural code written in C to Java, and
1415
* the code does not follow Java conventions (e.g., methods names start by
1516
capital letters)
1617
* configuration of the LZMA encoder and decoders require passing around
1718
arrays and numbers for which no proper documentation or constants exists
1819
other than source code, and
19-
* ...there is no stream api to plug into java.io streams.
20+
* ...there is no stream api to plug into `java.io` streams.
2021

21-
There is unfortunately no public description of the LZMA algorithms, so a
22-
rewrite was clearly a hard task. I decided to create this library as follows.
22+
There is unfortunately no public description of the LZMA algorithms other than source code,
23+
so a rewrite was clearly a hard task. I decided to create this library using the following
24+
methodology.
2325

2426
1. Import the Java LZMA SDK code.
2527
2. Convert methods and package names to Java conventions.
@@ -28,13 +30,69 @@ rewrite was clearly a hard task. I decided to create this library as follows.
2830
5. Run static code analysis to clean the code (unused variables, unusued parameters,
2931
unused methods, expressions simplifications and more).
3032
6. Do some profiling.
31-
7. Build a streaming api that would fit into java.io streams.
33+
7. Build a streaming api that would fit into `java.io` streams.
34+
8. Provide some higher-level abstractions to the LZMA encoder / decoders configuration.
3235

3336
Although not a derivate work, the streaming api classes were inspired from
3437
[the work of Christopher League](http://contrapunctus.net/league/haques/lzmajio/). I reused
3538
his technique of fake streams and working threads to pass the data around between
3639
encoders/decoders and "normal" Java streams.
3740

41+
## Usage ##
42+
43+
There are two main Java package hierarchies:
44+
45+
* `lzma.sdk` is the (reworked) Java LZMA SDK code, and
46+
* `lzma.streams` contains the `LzmaInputStream` and `LzmaInputStream` classes.
47+
48+
You will probably only be interested in using the `lzma.streams` package. The two
49+
stream classes use the good practices of constructor dependency injection, and you will
50+
need to pass them the decorated streams and LZMA encoder / decoders from the SDK.
51+
52+
You can simply instanciate a `Decoder` and pass it to the constructor of `LzmaInputStream`
53+
without specifying further configuration: it will read it from the input stream.
54+
55+
The `Encoder` class that `LzmaOutputStream` depends on needs some configuration. You can
56+
either do it manually (checkout the `Encoder` class to guess what those integer values mean!),
57+
or you can use the `LzmaOutputStream.Builder` class which makes it much easier to configure.
58+
59+
The following code is from a unit test. It should make the basic usage of the library relatively
60+
obvious:
61+
62+
public void test_round_trip() throws IOException
63+
{
64+
final File sourceFile = new File("LICENSE");
65+
final File compressed = File.createTempFile("lzma-java", "compressed");
66+
final File unCompressed = File.createTempFile("lzma-java", "uncompressed");
67+
68+
final LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(
69+
new BufferedOutputStream(new FileOutputStream(compressed)))
70+
.useMaximalDictionarySize()
71+
.useEndMarkerMode(true)
72+
.useBT4MatchFinder()
73+
.build();
74+
75+
final InputStream sourceIn = new BufferedInputStream(new FileInputStream(sourceFile));
76+
77+
copy(sourceIn, compressedOut);
78+
sourceIn.close();
79+
compressedOut.close();
80+
81+
final LzmaInputStream compressedIn = new LzmaInputStream(
82+
new BufferedInputStream(new FileInputStream(compressed)),
83+
new Decoder());
84+
85+
final OutputStream uncompressedOut = new BufferedOutputStream(
86+
new FileOutputStream(unCompressed));
87+
88+
copy(compressedIn, uncompressedOut);
89+
compressedIn.close();
90+
uncompressedOut.close();
91+
92+
assertTrue(contentEquals(sourceFile, unCompressed));
93+
assertFalse(contentEquals(sourceFile, compressed));
94+
}
95+
3896
## License ##
3997

4098
The LZMA SDK is in the public domain. I relicensed the whole under the liberal

0 commit comments

Comments
 (0)