• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bzip2;
20 
21 import static org.apache.commons.compress.AbstractTestCase.getFile;
22 
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import org.apache.commons.compress.utils.IOUtils;
30 import org.junit.Assert;
31 import org.junit.Test;
32 
33 public class BZip2CompressorInputStreamTest {
34 
35     @Test(expected = IOException.class)
shouldThrowAnIOExceptionWhenAppliedToAZipFile()36     public void shouldThrowAnIOExceptionWhenAppliedToAZipFile() throws Exception {
37         try (FileInputStream in = new FileInputStream(getFile("bla.zip"))) {
38             BZip2CompressorInputStream bis = new BZip2CompressorInputStream(in);
39             bis.close();
40         }
41     }
42 
43     /**
44      * @see "https://issues.apache.org/jira/browse/COMPRESS-309"
45      */
46     @Test
readOfLength0ShouldReturn0()47     public void readOfLength0ShouldReturn0() throws Exception {
48         // Create a big random piece of data
49         final byte[] rawData = new byte[1048576];
50         for (int i=0; i < rawData.length; ++i) {
51             rawData[i] = (byte) Math.floor(Math.random()*256);
52         }
53 
54         // Compress it
55         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
56         final BZip2CompressorOutputStream bzipOut = new BZip2CompressorOutputStream(baos);
57         bzipOut.write(rawData);
58         bzipOut.flush();
59         bzipOut.close();
60         baos.flush();
61         baos.close();
62 
63         // Try to read it back in
64         final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
65         final BZip2CompressorInputStream bzipIn = new BZip2CompressorInputStream(bais);
66         final byte[] buffer = new byte[1024];
67         Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));
68         Assert.assertEquals(0, bzipIn.read(buffer, 1024, 0));
69         Assert.assertEquals(1024, bzipIn.read(buffer, 0, 1024));
70         bzipIn.close();
71     }
72 
73     @Test
singleByteReadConsistentlyReturnsMinusOneAtEof()74     public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
75         final File input = getFile("bla.txt.bz2");
76         try (InputStream is = new FileInputStream(input)) {
77             final BZip2CompressorInputStream in =
78                     new BZip2CompressorInputStream(is);
79             IOUtils.toByteArray(in);
80             Assert.assertEquals(-1, in.read());
81             Assert.assertEquals(-1, in.read());
82             in.close();
83         }
84     }
85 
86     @Test
multiByteReadConsistentlyReturnsMinusOneAtEof()87     public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
88         final File input = getFile("bla.txt.bz2");
89         byte[] buf = new byte[2];
90         try (InputStream is = new FileInputStream(input)) {
91             final BZip2CompressorInputStream in =
92                     new BZip2CompressorInputStream(is);
93             IOUtils.toByteArray(in);
94             Assert.assertEquals(-1, in.read(buf));
95             Assert.assertEquals(-1, in.read(buf));
96             in.close();
97         }
98     }
99 
100 }
101