View Javadoc
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   *   https://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  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.ByteArrayInputStream;
25  
26  import org.apache.commons.compress.utils.ByteUtils;
27  import org.junit.jupiter.api.Test;
28  
29  class BitStreamTest {
30  
31      @Test
32      void testNextByte() throws Exception {
33          try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x35 }))) {
34              assertEquals(0, stream.readBit(), "bit 0");
35              assertEquals(1, stream.readBit(), "bit 1");
36              assertEquals(0, stream.readBit(), "bit 2");
37              assertEquals(1, stream.readBit(), "bit 3");
38  
39              assertEquals(0x5E, stream.nextByte(), "next byte");
40              assertEquals(-1, stream.nextByte(), "next byte"); // not enough bits left to read a byte
41          }
42      }
43  
44      @Test
45      void testNextByteFromEmptyStream() throws Exception {
46          try (BitStream stream = new BitStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
47              assertEquals(-1, stream.nextByte(), "next byte");
48              assertEquals(-1, stream.nextByte(), "next byte");
49          }
50      }
51  
52      @Test
53      void testReadAlignedBytes() throws Exception {
54          try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0xEA, 0x35 }))) {
55              assertEquals(0xEA, stream.nextByte(), "next byte");
56              assertEquals(0x35, stream.nextByte(), "next byte");
57              assertEquals(-1, stream.nextByte(), "next byte");
58          }
59      }
60  }