1/*
2 * Copyright (c) 2002, 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 4507868
27 * @summary Checks that ImageOutputStreamImpl.writeBits() advances the stream
28 *          position and bit offset correctly. Also verifies that the
29 *          MemoryCacheImageOutputStream.read() variants reset the bitOffset
30 *          before the read actually occurs.
31 */
32
33import java.io.ByteArrayOutputStream;
34import java.io.IOException;
35
36import javax.imageio.stream.ImageOutputStream;
37import javax.imageio.stream.MemoryCacheImageOutputStream;
38
39public class WriteBitsTest {
40
41    private static void verify(ImageOutputStream ios,
42                               long expstreampos, int expbitoffset)
43        throws IOException, RuntimeException
44    {
45        long actstreampos = ios.getStreamPosition();
46        int actbitoffset = ios.getBitOffset();
47
48        if ((actstreampos != expstreampos) ||
49            (actbitoffset != expbitoffset))
50        {
51            System.err.println("Expected stream position: " + expstreampos +
52                               " Actual: " + actstreampos);
53            System.err.println("Expected bit offset: " + expbitoffset +
54                               " Actual: " + actbitoffset);
55            throw new RuntimeException("Test failed.");
56        }
57    }
58
59    public static void main(String argv[]) throws RuntimeException {
60        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
61        MemoryCacheImageOutputStream mcios = new
62            MemoryCacheImageOutputStream(ostream);
63
64        try {
65            // verify correct writeBits() functionality
66            long streampos = 0;
67            int bitoffset = 0;
68
69            mcios.setBitOffset(bitoffset);
70            verify(mcios, streampos, bitoffset);
71
72            bitoffset = 3;
73            mcios.setBitOffset(bitoffset);
74            verify(mcios, streampos, bitoffset);
75
76            for (int incr = 3; incr <= 15; incr += 12) {
77                for (int i = 0; i < 64; i += incr) {
78                    mcios.writeBits(10, incr);
79
80                    bitoffset += incr;
81
82                    if (bitoffset > 7) {
83                        int stroffset = bitoffset / 8;
84                        bitoffset = bitoffset % 8;
85                        streampos += stroffset;
86                    }
87
88                    verify(mcios, streampos, bitoffset);
89                }
90            }
91
92            // verify correct read(byte[], int, int) functionality
93            byte[] bytearr = new byte[2];
94            mcios.seek(2);
95            mcios.setBitOffset(3);
96            int numread = mcios.read(bytearr, 0, 2);
97            if (numread != 2) {
98                throw new RuntimeException("Error in mcios.read([BII)I");
99            }
100            verify(mcios, 4, 0);
101
102            // verify correct read() functionality
103            mcios.setBitOffset(3);
104            mcios.read();
105            verify(mcios, 5, 0);
106        } catch (IOException e) {
107            throw new RuntimeException("Unexpected IOException: " + e);
108        }
109    }
110}
111