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 4417672 4422328 27 * @summary Checks the functionality of MemoryCacheImageOutputStream 28 * particularly with regard to seeking and flushing 29 */ 30 31import java.io.ByteArrayOutputStream; 32import java.io.IOException; 33 34import javax.imageio.stream.ImageOutputStream; 35import javax.imageio.stream.MemoryCacheImageOutputStream; 36 37public class MemoryCacheImageOutputStreamTest { 38 39 public static void main(String[] args) throws IOException { 40 try { 41 MemoryCacheImageOutputStream stream = 42 new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); 43 stream.write(0); // or write anything, for that matter 44 stream.flush(); 45 } catch (Exception e) { 46 throw new RuntimeException("Error flushing stream: " + e); 47 } 48 49 ByteArrayOutputStream os = new ByteArrayOutputStream(); 50 ImageOutputStream ios = new MemoryCacheImageOutputStream(os); 51 52 byte[] b = new byte[30*256]; 53 byte byteVal = (byte)0; 54 for (int i = 0; i < b.length; i++) { 55 b[i] = byteVal++; 56 } 57 58 // Write 261,120 bytes 59 for (int i = 0; i < 34; i++) { 60 ios.write(b); 61 } 62 // Scatter 256 values at positions 1000, 2000, ... 63 // Using both write(int) and write(byte[]) 64 byte[] buf = new byte[1]; 65 for (int i = 0; i < 256; i += 2) { 66 ios.seek(1000*i); 67 ios.write(i); 68 69 ios.seek(1000*(i + 1)); 70 buf[0] = (byte)(i + 1); 71 ios.write(buf); 72 } 73 74 // Re-read scattered values 75 for (int i = 0; i < 256; i++) { 76 ios.seek(1000*i); 77 int val = ios.read(); 78 if (val != i) { 79 System.out.println("Got bad value (1) at pos = " + (1000*i)); 80 } 81 } 82 83 // Discard two buffers and re-read scattered values 84 ios.flushBefore(2*8192); 85 86 for (int i = 0; i < 256; i++) { 87 long pos = 1000*i; 88 if (pos >= 2*8192) { 89 ios.seek(pos); 90 int val = ios.read(); 91 if (val != i) { 92 System.out.println("Got bad value (2) at pos = " + (1000*i)); 93 } 94 } 95 } 96 ios.close(); 97 98 byte[] data = os.toByteArray(); 99 for (int i = 0; i < data.length; i++) { 100 byte val = data[i]; 101 if ((i < 256000) && (i % 1000) == 0) { 102 if (val != (byte)(i/1000)) { 103 System.out.println("Got bad value (3) at pos = " + i); 104 } 105 } else { 106 byte gval = (byte)((i % (30*256)) % 256); 107 if (val != gval) { 108 System.out.println("Got bad value (4) at pos = " + i + 109 "(got " + (val & 0xff) + 110 " wanted " + (gval & 0xff) +")"); 111 } 112 } 113 } 114 } 115} 116