Path: blob/master/test/jdk/javax/imageio/stream/ReadFullyTest.java
41149 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 442226326* @summary Checks that ImageInputStream.readFully(type[], int int) handles sign27* extension and byte ordering correctly28*/2930import java.io.ByteArrayInputStream;31import java.io.InputStream;32import java.nio.ByteOrder;3334import javax.imageio.stream.ImageInputStream;35import javax.imageio.stream.MemoryCacheImageInputStream;3637public class ReadFullyTest {3839static final ByteOrder bigEndian = ByteOrder.BIG_ENDIAN;40static final ByteOrder littleEndian = ByteOrder.LITTLE_ENDIAN;4142private static void expect(long e, long g) {43if (e != g) {44throw new RuntimeException("Expected " + e + ", got " + g);45}46}4748public static void main (String args[]) {49try {50byte[] b = {51(byte)0x11, (byte)0x22, // low low52(byte)0x44, (byte)0x99, // low high53(byte)0xAA, (byte)0x33, // high low54(byte)0xBB, (byte)0xCC // high high55};56InputStream in = new ByteArrayInputStream(b);57ImageInputStream iin = new MemoryCacheImageInputStream(in);5859short[] s = new short[b.length/2];60char[] c = new char[b.length/2];61int[] i = new int[b.length/4];62long[] l = new long[b.length/8];63float[] f = new float[b.length/4];64double[] d = new double[b.length/8];6566iin.seek(0L);67iin.setByteOrder(bigEndian);68iin.readFully(s, 0, s.length);69expect(s[0] & 0xffff, 0x1122);70expect(s[1] & 0xffff, 0x4499);71expect(s[2] & 0xffff, 0xAA33);72expect(s[3] & 0xffff, 0xBBCC);7374iin.seek(0L);75iin.setByteOrder(littleEndian);76iin.readFully(s, 0, s.length);77expect(s[0] & 0xffff, 0x2211);78expect(s[1] & 0xffff, 0x9944);79expect(s[2] & 0xffff, 0x33AA);80expect(s[3] & 0xffff, 0xCCBB);8182iin.seek(0L);83iin.setByteOrder(bigEndian);84iin.readFully(c, 0, c.length);85expect(c[0], 0x1122);86expect(c[1], 0x4499);87expect(c[2], 0xAA33);88expect(c[3], 0xBBCC);8990iin.seek(0L);91iin.setByteOrder(littleEndian);92iin.readFully(c, 0, c.length);93expect(c[0], 0x2211);94expect(c[1], 0x9944);95expect(c[2], 0x33AA);96expect(c[3], 0xCCBB);9798iin.seek(0L);99iin.setByteOrder(bigEndian);100iin.readFully(i, 0, i.length);101expect(i[0] & 0xffffffff, 0x11224499);102expect(i[1] & 0xffffffff, 0xAA33BBCC);103104iin.seek(0L);105iin.setByteOrder(littleEndian);106iin.readFully(i, 0, i.length);107expect(i[0] & 0xffffffff, 0x99442211);108expect(i[1] & 0xffffffff, 0xCCBB33AA);109110iin.seek(0L);111iin.setByteOrder(bigEndian);112iin.readFully(f, 0, f.length);113expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x11224499);114expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xAA33BBCC);115116iin.seek(0L);117iin.setByteOrder(littleEndian);118iin.readFully(f, 0, f.length);119expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x99442211);120expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xCCBB33AA);121122iin.seek(0L);123iin.setByteOrder(bigEndian);124iin.readFully(l, 0, l.length);125expect(l[0], 0x11224499AA33BBCCL);126127iin.seek(0L);128iin.setByteOrder(littleEndian);129iin.readFully(l, 0, l.length);130expect(l[0], 0xCCBB33AA99442211L);131132iin.seek(0L);133iin.setByteOrder(bigEndian);134iin.readFully(d, 0, d.length);135expect(Double.doubleToLongBits(d[0]), 0x11224499AA33BBCCL);136137iin.seek(0L);138iin.setByteOrder(littleEndian);139iin.readFully(d, 0, d.length);140expect(Double.doubleToLongBits(d[0]), 0xCCBB33AA99442211L);141} catch (Exception ex) {142throw new RuntimeException("Got exception " + ex);143}144}145}146147148