|
| 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