Home | History | Annotate | Download | only in deflate
      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.deflate;
     20 
     21 import java.io.File;
     22 import java.io.FileInputStream;
     23 import java.io.IOException;
     24 import java.io.InputStream;
     25 
     26 import org.apache.commons.compress.AbstractTestCase;
     27 import org.apache.commons.compress.utils.IOUtils;
     28 import org.junit.Assert;
     29 import org.junit.Test;
     30 
     31 public class DeflateCompressorInputStreamTest {
     32 
     33     @Test
     34     public void availableShouldReturnNonZero() throws IOException {
     35         final File input = AbstractTestCase.getFile("bla.tar.deflatez");
     36         try (InputStream is = new FileInputStream(input)) {
     37             final DeflateCompressorInputStream in =
     38                     new DeflateCompressorInputStream(is);
     39             Assert.assertTrue(in.available() > 0);
     40             in.close();
     41         }
     42     }
     43 
     44     @Test
     45     public void shouldBeAbleToSkipAByte() throws IOException {
     46         final File input = AbstractTestCase.getFile("bla.tar.deflatez");
     47         try (InputStream is = new FileInputStream(input)) {
     48             final DeflateCompressorInputStream in =
     49                     new DeflateCompressorInputStream(is);
     50             Assert.assertEquals(1, in.skip(1));
     51             in.close();
     52         }
     53     }
     54 
     55     @Test
     56     public void singleByteReadWorksAsExpected() throws IOException {
     57         final File input = AbstractTestCase.getFile("bla.tar.deflatez");
     58         try (InputStream is = new FileInputStream(input)) {
     59             final DeflateCompressorInputStream in =
     60                     new DeflateCompressorInputStream(is);
     61             // tar header starts with filename "test1.xml"
     62             Assert.assertEquals('t', in.read());
     63             in.close();
     64         }
     65     }
     66 
     67     @Test
     68     public void singleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
     69         final File input = AbstractTestCase.getFile("bla.tar.deflatez");
     70         try (InputStream is = new FileInputStream(input)) {
     71             final DeflateCompressorInputStream in =
     72                     new DeflateCompressorInputStream(is);
     73             IOUtils.toByteArray(in);
     74             Assert.assertEquals(-1, in.read());
     75             Assert.assertEquals(-1, in.read());
     76             in.close();
     77         }
     78     }
     79 
     80     @Test
     81     public void multiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
     82         final File input = AbstractTestCase.getFile("bla.tar.deflatez");
     83         byte[] buf = new byte[2];
     84         try (InputStream is = new FileInputStream(input)) {
     85             final DeflateCompressorInputStream in =
     86                     new DeflateCompressorInputStream(is);
     87             IOUtils.toByteArray(in);
     88             Assert.assertEquals(-1, in.read(buf));
     89             Assert.assertEquals(-1, in.read(buf));
     90             in.close();
     91         }
     92     }
     93 
     94 }
     95