1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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");
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 }