Skip to content

Commit 92fd0e3

Browse files
committed
Added a test case, and tweaked the encoder / decoder properties handling in the streams.
1 parent 6a5be32 commit 92fd0e3

File tree

3 files changed

+108
-11
lines changed

3 files changed

+108
-11
lines changed

pom.xml

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
<?xml version="1.0"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5-
5+
66
<modelVersion>4.0.0</modelVersion>
7-
7+
88
<groupId>lzma-java</groupId>
99
<artifactId>lzma-java</artifactId>
10-
10+
1111
<packaging>jar</packaging>
1212
<version>0.9-SNAPSHOT</version>
13-
14-
<name>LZMA library for Java</name>
13+
14+
<name>LZMA library for Java</name>
1515
<url>http://jponge.github.com/lzma-java</url>
16-
17-
<dependencies/>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.7</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>commons-io</groupId>
26+
<artifactId>commons-io</artifactId>
27+
<version>1.4</version>
28+
</dependency>
29+
</dependencies>
1830

1931
<build>
2032
<plugins>

src/main/java/lzma/streams/LzmaOutputStream.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ public void run()
9393
{
9494
if (!encoderConfigured)
9595
{
96-
encoder.setDictionarySize(28);
97-
encoder.setEndMarkerMode(true);
9896
encoder.writeCoderProperties(out);
9997
}
10098
encoder.code(fakeInputStream, out, -1, -1, null);
@@ -284,7 +282,7 @@ public LzmaOutputStream build()
284282
encoder.setMatchFinder(matchFinder);
285283
encoder.setNumFastBytes(numFastBytes);
286284

287-
return new LzmaOutputStream(out, encoder, true);
285+
return new LzmaOutputStream(out, encoder, false);
288286
}
289287
}
290288
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2009 Julien Ponge. All rights reserved.
3+
*
4+
5+
* http://julien.ponge.info/
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
* This work is based on the LZMA SDK by Igor Pavlov.
20+
* The LZMA SDK is placed under the public domain, and can be obtained from
21+
*
22+
* http://www.7-zip.org/sdk.html
23+
*
24+
* The LzmaInputStream and LzmaOutputStream classes were inspired by the
25+
* work of Christopher League, although they are not derivative works.
26+
*
27+
* http://contrapunctus.net/league/haques/lzmajio/
28+
*/
29+
30+
package lzma.streams;
31+
32+
import org.junit.Test;
33+
import org.apache.commons.io.FileUtils;
34+
import static org.apache.commons.io.FileUtils.*;
35+
import static org.apache.commons.io.FileUtils.contentEquals;
36+
37+
import java.io.*;
38+
39+
import lzma.sdk.lzma.Decoder;
40+
import junit.framework.TestCase;
41+
42+
public class RountTripTest extends TestCase
43+
{
44+
private void copy(InputStream in, OutputStream out) throws IOException
45+
{
46+
int nread = 0;
47+
final byte[] buffer = new byte[1024];
48+
while ((nread = in.read(buffer)) != -1)
49+
{
50+
out.write(buffer, 0, nread);
51+
}
52+
}
53+
54+
public void test_round_trip() throws IOException
55+
{
56+
final File sourceFile = new File("LICENSE");
57+
final File compressed = File.createTempFile("lzma-java", "compressed");
58+
final File unCompressed = File.createTempFile("lzma-java", "uncompressed");
59+
60+
final LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(
61+
new BufferedOutputStream(new FileOutputStream(compressed)))
62+
.useMaximalDictionarySize()
63+
.useEndMarkerMode(true)
64+
.useBT4MatchFinder()
65+
.build();
66+
67+
final InputStream sourceIn = new BufferedInputStream(new FileInputStream(sourceFile));
68+
69+
copy(sourceIn, compressedOut);
70+
sourceIn.close();
71+
compressedOut.close();
72+
73+
final LzmaInputStream compressedIn = new LzmaInputStream(
74+
new BufferedInputStream(new FileInputStream(compressed)),
75+
new Decoder());
76+
77+
final OutputStream uncompressedOut = new BufferedOutputStream(
78+
new FileOutputStream(unCompressed));
79+
80+
copy(compressedIn, uncompressedOut);
81+
compressedIn.close();
82+
uncompressedOut.close();
83+
84+
assertTrue(contentEquals(sourceFile, unCompressed));
85+
assertFalse(contentEquals(sourceFile, compressed));
86+
}
87+
}

0 commit comments

Comments
 (0)