Path: blob/master/test/jdk/javax/imageio/stream/WriteBitsTest.java
41149 views
/*1* Copyright (c) 2002, 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 450786826* @summary Checks that ImageOutputStreamImpl.writeBits() advances the stream27* position and bit offset correctly. Also verifies that the28* MemoryCacheImageOutputStream.read() variants reset the bitOffset29* before the read actually occurs.30*/3132import java.io.ByteArrayOutputStream;33import java.io.IOException;3435import javax.imageio.stream.ImageOutputStream;36import javax.imageio.stream.MemoryCacheImageOutputStream;3738public class WriteBitsTest {3940private static void verify(ImageOutputStream ios,41long expstreampos, int expbitoffset)42throws IOException, RuntimeException43{44long actstreampos = ios.getStreamPosition();45int actbitoffset = ios.getBitOffset();4647if ((actstreampos != expstreampos) ||48(actbitoffset != expbitoffset))49{50System.err.println("Expected stream position: " + expstreampos +51" Actual: " + actstreampos);52System.err.println("Expected bit offset: " + expbitoffset +53" Actual: " + actbitoffset);54throw new RuntimeException("Test failed.");55}56}5758public static void main(String argv[]) throws RuntimeException {59ByteArrayOutputStream ostream = new ByteArrayOutputStream();60MemoryCacheImageOutputStream mcios = new61MemoryCacheImageOutputStream(ostream);6263try {64// verify correct writeBits() functionality65long streampos = 0;66int bitoffset = 0;6768mcios.setBitOffset(bitoffset);69verify(mcios, streampos, bitoffset);7071bitoffset = 3;72mcios.setBitOffset(bitoffset);73verify(mcios, streampos, bitoffset);7475for (int incr = 3; incr <= 15; incr += 12) {76for (int i = 0; i < 64; i += incr) {77mcios.writeBits(10, incr);7879bitoffset += incr;8081if (bitoffset > 7) {82int stroffset = bitoffset / 8;83bitoffset = bitoffset % 8;84streampos += stroffset;85}8687verify(mcios, streampos, bitoffset);88}89}9091// verify correct read(byte[], int, int) functionality92byte[] bytearr = new byte[2];93mcios.seek(2);94mcios.setBitOffset(3);95int numread = mcios.read(bytearr, 0, 2);96if (numread != 2) {97throw new RuntimeException("Error in mcios.read([BII)I");98}99verify(mcios, 4, 0);100101// verify correct read() functionality102mcios.setBitOffset(3);103mcios.read();104verify(mcios, 5, 0);105} catch (IOException e) {106throw new RuntimeException("Unexpected IOException: " + e);107}108}109}110111112