Path: blob/master/test/jdk/javax/imageio/ReadBitsTest.java
41144 views
/*1* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 441680026* @summary Checks that ImageInputStreamImpl.readBit and readBits handle the bit27* offset correctly28*/2930import java.io.ByteArrayInputStream;31import java.io.IOException;32import java.io.InputStream;3334import javax.imageio.stream.FileCacheImageInputStream;35import javax.imageio.stream.ImageInputStream;3637public class ReadBitsTest {38public static void main(String[] args) throws IOException {39byte[] buffer = new byte[] {(byte)169, (byte)85}; // 10101001 0101010140InputStream ins = new ByteArrayInputStream(buffer);41ImageInputStream in = new FileCacheImageInputStream(ins,null);4243if (in.getBitOffset() != 0) {44throw new RuntimeException("Initial bit offset != 0!");45}4647int bit0 = in.readBit(); // 148if (bit0 != 1) {49throw new RuntimeException("First bit != 1");50}51if (in.getBitOffset() != 1) {52throw new RuntimeException("Second bit offset != 1");53}5455long bits1 = in.readBits(5); // 01010 = 1056if (bits1 != 10) {57throw new RuntimeException("Bits 1-5 != 10 (= " + bits1 + ")");58}59if (in.getBitOffset() != 6) {60throw new RuntimeException("Third bit offset != 6");61}6263int bit1 = in.readBit(); // 064if (bit1 != 0) {65throw new RuntimeException("Bit 6 != 0");66}67if (in.getBitOffset() != 7) {68throw new RuntimeException("Third bit offset != 7");69}7071long bits2 = in.readBits(8); // 10101010 = 17072if (bits2 != 170) {73throw new RuntimeException("Bits 7-14 != 170 (= " + bits2 + ")");74}75if (in.getBitOffset() != 7) {76throw new RuntimeException("Fourth bit offset != 7");77}7879int bit2 = in.readBit(); // 180if (bit2 != 1) {81throw new RuntimeException("Bit 15 != 1");82}83if (in.getBitOffset() != 0) {84throw new RuntimeException("Fifth bit offset != 0");85}8687in.close();88}89}909192