1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.commons.compress.compressors; 20 21 import static org.junit.Assert.*; 22 23 import java.io.BufferedInputStream; 24 import java.io.File; 25 import java.io.FileInputStream; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.util.Random; 30 31 import org.apache.commons.compress.AbstractTestCase; 32 import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream; 33 import org.apache.commons.compress.utils.IOUtils; 34 import org.junit.Test; 35 36 public final class FramedSnappyTestCase 37 extends AbstractTestCase { 38 39 @Test 40 public void testDefaultExtraction() throws Exception { 41 testUnarchive(new StreamWrapper<CompressorInputStream>() { 42 @Override 43 public CompressorInputStream wrap(final InputStream is) throws IOException { 44 return new FramedSnappyCompressorInputStream(is); 45 } 46 }); 47 } 48 49 @Test 50 public void testDefaultExtractionViaFactory() throws Exception { 51 testUnarchive(new StreamWrapper<CompressorInputStream>() { 52 @Override 53 public CompressorInputStream wrap(final InputStream is) throws Exception { 54 return new CompressorStreamFactory() 55 .createCompressorInputStream(CompressorStreamFactory.SNAPPY_FRAMED, 56 is); 57 } 58 }); 59 } 60 61 @Test 62 public void testDefaultExtractionViaFactoryAutodetection() throws Exception { 63 testUnarchive(new StreamWrapper<CompressorInputStream>() { 64 @Override 65 public CompressorInputStream wrap(final InputStream is) throws Exception { 66 return new CompressorStreamFactory().createCompressorInputStream(is); 67 } 68 }); 69 } 70 71 private void testUnarchive(final StreamWrapper<CompressorInputStream> wrapper) throws Exception { 72 final File input = getFile("bla.tar.sz"); 73 final File output = new File(dir, "bla.tar"); 74 try (FileInputStream is = new FileInputStream(input)) { 75 // the intermediate BufferedInputStream is there for mark 76 // support in the autodetection test 77 final CompressorInputStream in = wrapper.wrap(new BufferedInputStream(is)); 78 FileOutputStream out = null; 79 try { 80 out = new FileOutputStream(output); 81 IOUtils.copy(in, out); 82 assertEquals(995, in.getBytesRead()); 83 } finally { 84 if (out != null) { 85 out.close(); 86 } 87 in.close(); 88 } 89 } 90 final File original = getFile("bla.tar"); 91 try (FileInputStream written = new FileInputStream(output)) { 92 try (FileInputStream orig = new FileInputStream(original)) { 93 assertArrayEquals(IOUtils.toByteArray(written), 94 IOUtils.toByteArray(orig)); 95 } 96 } 97 } 98 99 @Test 100 public void testRoundtrip() throws Exception { 101 testRoundtrip(getFile("test.txt")); 102 testRoundtrip(getFile("bla.tar")); 103 testRoundtrip(getFile("COMPRESS-256.7z")); 104 } 105 106 @Test 107 public void testRoundtripWithOneBigWrite() throws Exception { 108 Random r = new Random(); 109 File input = new File(dir, "bigChunkTest"); 110 try (FileOutputStream fs = new FileOutputStream(input)) { 111 for (int i = 0 ; i < 1 << 17; i++) { 112 fs.write(r.nextInt(256)); 113 } 114 } 115 long start = System.currentTimeMillis(); 116 final File outputSz = new File(dir, input.getName() + ".sz"); 117 try (FileInputStream is = new FileInputStream(input); 118 FileOutputStream os = new FileOutputStream(outputSz); 119 CompressorOutputStream sos = new CompressorStreamFactory() 120 .createCompressorOutputStream("snappy-framed", os)) { 121 byte[] b = IOUtils.toByteArray(is); 122 sos.write(b[0]); 123 sos.write(b, 1, b.length - 1); // must be split into multiple compressed chunks 124 } 125 System.err.println(input.getName() + " written, uncompressed bytes: " + input.length() 126 + ", compressed bytes: " + outputSz.length() + " after " + (System.currentTimeMillis() - start) + "ms"); 127 try (FileInputStream is = new FileInputStream(input); 128 CompressorInputStream sis = new CompressorStreamFactory() 129 .createCompressorInputStream("snappy-framed", new FileInputStream(outputSz))) { 130 byte[] expected = IOUtils.toByteArray(is); 131 byte[] actual = IOUtils.toByteArray(sis); 132 assertArrayEquals(expected, actual); 133 } 134 } 135 136 private void testRoundtrip(File input) throws Exception { 137 long start = System.currentTimeMillis(); 138 final File outputSz = new File(dir, input.getName() + ".sz"); 139 try (FileInputStream is = new FileInputStream(input); 140 FileOutputStream os = new FileOutputStream(outputSz); 141 CompressorOutputStream sos = new CompressorStreamFactory() 142 .createCompressorOutputStream("snappy-framed", os)) { 143 IOUtils.copy(is, sos); 144 } 145 System.err.println(input.getName() + " written, uncompressed bytes: " + input.length() 146 + ", compressed bytes: " + outputSz.length() + " after " + (System.currentTimeMillis() - start) + "ms"); 147 try (FileInputStream is = new FileInputStream(input); 148 CompressorInputStream sis = new CompressorStreamFactory() 149 .createCompressorInputStream("snappy-framed", new FileInputStream(outputSz))) { 150 byte[] expected = IOUtils.toByteArray(is); 151 byte[] actual = IOUtils.toByteArray(sis); 152 assertArrayEquals(expected, actual); 153 } 154 } 155 } 156