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 4422263 27 * @summary Checks that ImageInputStream.readFully(type[], int int) handles sign 28 * extension and byte ordering correctly 29 */ 30 31 import java.io.ByteArrayInputStream; 32 import java.io.InputStream; 33 import java.nio.ByteOrder; 34 35 import javax.imageio.stream.ImageInputStream; 36 import javax.imageio.stream.MemoryCacheImageInputStream; 37 38 public class ReadFullyTest { 39 40 static final ByteOrder bigEndian = ByteOrder.BIG_ENDIAN; 41 static final ByteOrder littleEndian = ByteOrder.LITTLE_ENDIAN; 42 43 private static void expect(long e, long g) { 44 if (e != g) { 45 throw new RuntimeException("Expected " + e + ", got " + g); 46 } 47 } 48 49 public static void main (String args[]) { 50 try { 51 byte[] b = { 52 (byte)0x11, (byte)0x22, // low low 53 (byte)0x44, (byte)0x99, // low high 54 (byte)0xAA, (byte)0x33, // high low 55 (byte)0xBB, (byte)0xCC // high high 56 }; 57 InputStream in = new ByteArrayInputStream(b); 58 ImageInputStream iin = new MemoryCacheImageInputStream(in); 59 60 short[] s = new short[b.length/2]; 61 char[] c = new char[b.length/2]; 62 int[] i = new int[b.length/4]; 63 long[] l = new long[b.length/8]; 64 float[] f = new float[b.length/4]; 65 double[] d = new double[b.length/8]; 66 67 iin.seek(0L); 68 iin.setByteOrder(bigEndian); 69 iin.readFully(s, 0, s.length); 70 expect(s[0] & 0xffff, 0x1122); 71 expect(s[1] & 0xffff, 0x4499); 72 expect(s[2] & 0xffff, 0xAA33); 73 expect(s[3] & 0xffff, 0xBBCC); 74 75 iin.seek(0L); 76 iin.setByteOrder(littleEndian); 77 iin.readFully(s, 0, s.length); 78 expect(s[0] & 0xffff, 0x2211); 79 expect(s[1] & 0xffff, 0x9944); 80 expect(s[2] & 0xffff, 0x33AA); 81 expect(s[3] & 0xffff, 0xCCBB); 82 83 iin.seek(0L); 84 iin.setByteOrder(bigEndian); 85 iin.readFully(c, 0, c.length); 86 expect(c[0], 0x1122); 87 expect(c[1], 0x4499); 88 expect(c[2], 0xAA33); 89 expect(c[3], 0xBBCC); 90 91 iin.seek(0L); 92 iin.setByteOrder(littleEndian); 93 iin.readFully(c, 0, c.length); 94 expect(c[0], 0x2211); 95 expect(c[1], 0x9944); 96 expect(c[2], 0x33AA); 97 expect(c[3], 0xCCBB); 98 99 iin.seek(0L); 100 iin.setByteOrder(bigEndian); 101 iin.readFully(i, 0, i.length); 102 expect(i[0] & 0xffffffff, 0x11224499); 103 expect(i[1] & 0xffffffff, 0xAA33BBCC); 104 105 iin.seek(0L); 106 iin.setByteOrder(littleEndian); 107 iin.readFully(i, 0, i.length); 108 expect(i[0] & 0xffffffff, 0x99442211); 109 expect(i[1] & 0xffffffff, 0xCCBB33AA); 110 111 iin.seek(0L); 112 iin.setByteOrder(bigEndian); 113 iin.readFully(f, 0, f.length); 114 expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x11224499); 115 expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xAA33BBCC); 116 117 iin.seek(0L); 118 iin.setByteOrder(littleEndian); 119 iin.readFully(f, 0, f.length); 120 expect(Float.floatToIntBits(f[0]) & 0xffffffff, 0x99442211); 121 expect(Float.floatToIntBits(f[1]) & 0xffffffff, 0xCCBB33AA); 122 123 iin.seek(0L); 124 iin.setByteOrder(bigEndian); 125 iin.readFully(l, 0, l.length); 126 expect(l[0], 0x11224499AA33BBCCL); 127 128 iin.seek(0L); 129 iin.setByteOrder(littleEndian); 130 iin.readFully(l, 0, l.length); 131 expect(l[0], 0xCCBB33AA99442211L); 132 133 iin.seek(0L); 134 iin.setByteOrder(bigEndian); 135 iin.readFully(d, 0, d.length); 136 expect(Double.doubleToLongBits(d[0]), 0x11224499AA33BBCCL); 137 138 iin.seek(0L); 139 iin.setByteOrder(littleEndian); 140 iin.readFully(d, 0, d.length); 141 expect(Double.doubleToLongBits(d[0]), 0xCCBB33AA99442211L); 142 } catch (Exception ex) { 143 throw new RuntimeException("Got exception " + ex); 144 } 145 } 146 }