1/*
2 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4416800
27 * @summary Checks that ImageInputStreamImpl.readBit and readBits handle the bit
28 *          offset correctly
29 */
30
31import java.io.ByteArrayInputStream;
32import java.io.IOException;
33import java.io.InputStream;
34
35import javax.imageio.stream.FileCacheImageInputStream;
36import javax.imageio.stream.ImageInputStream;
37
38public class ReadBitsTest {
39    public static void main(String[] args) throws IOException {
40        byte[] buffer = new byte[] {(byte)169, (byte)85}; // 10101001 01010101
41        InputStream ins = new ByteArrayInputStream(buffer);
42        ImageInputStream in = new FileCacheImageInputStream(ins,null);
43
44        if (in.getBitOffset() != 0) {
45            throw new RuntimeException("Initial bit offset != 0!");
46        }
47
48        int bit0 = in.readBit(); // 1
49        if (bit0 != 1) {
50            throw new RuntimeException("First bit != 1");
51        }
52        if (in.getBitOffset() != 1) {
53            throw new RuntimeException("Second bit offset != 1");
54        }
55
56        long bits1 = in.readBits(5); // 01010 = 10
57        if (bits1 != 10) {
58            throw new RuntimeException("Bits 1-5 != 10 (= " + bits1 + ")");
59        }
60        if (in.getBitOffset() != 6) {
61            throw new RuntimeException("Third bit offset != 6");
62        }
63
64        int bit1 = in.readBit(); // 0
65        if (bit1 != 0) {
66            throw new RuntimeException("Bit 6 != 0");
67        }
68        if (in.getBitOffset() != 7) {
69            throw new RuntimeException("Third bit offset != 7");
70        }
71
72        long bits2 = in.readBits(8); // 10101010 = 170
73        if (bits2 != 170) {
74            throw new RuntimeException("Bits 7-14 != 170 (= " + bits2 + ")");
75        }
76        if (in.getBitOffset() != 7) {
77            throw new RuntimeException("Fourth bit offset != 7");
78        }
79
80        int bit2 = in.readBit(); // 1
81        if (bit2 != 1) {
82            throw new RuntimeException("Bit 15 != 1");
83        }
84        if (in.getBitOffset() != 0) {
85            throw new RuntimeException("Fifth bit offset != 0");
86        }
87
88        in.close();
89    }
90}
91