Path: blob/master/test/jdk/javax/imageio/stream/MemoryCacheImageOutputStreamTest.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 4417672 442232826* @summary Checks the functionality of MemoryCacheImageOutputStream27* particularly with regard to seeking and flushing28*/2930import java.io.ByteArrayOutputStream;31import java.io.IOException;3233import javax.imageio.stream.ImageOutputStream;34import javax.imageio.stream.MemoryCacheImageOutputStream;3536public class MemoryCacheImageOutputStreamTest {3738public static void main(String[] args) throws IOException {39try {40MemoryCacheImageOutputStream stream =41new MemoryCacheImageOutputStream(new ByteArrayOutputStream());42stream.write(0); // or write anything, for that matter43stream.flush();44} catch (Exception e) {45throw new RuntimeException("Error flushing stream: " + e);46}4748ByteArrayOutputStream os = new ByteArrayOutputStream();49ImageOutputStream ios = new MemoryCacheImageOutputStream(os);5051byte[] b = new byte[30*256];52byte byteVal = (byte)0;53for (int i = 0; i < b.length; i++) {54b[i] = byteVal++;55}5657// Write 261,120 bytes58for (int i = 0; i < 34; i++) {59ios.write(b);60}61// Scatter 256 values at positions 1000, 2000, ...62// Using both write(int) and write(byte[])63byte[] buf = new byte[1];64for (int i = 0; i < 256; i += 2) {65ios.seek(1000*i);66ios.write(i);6768ios.seek(1000*(i + 1));69buf[0] = (byte)(i + 1);70ios.write(buf);71}7273// Re-read scattered values74for (int i = 0; i < 256; i++) {75ios.seek(1000*i);76int val = ios.read();77if (val != i) {78System.out.println("Got bad value (1) at pos = " + (1000*i));79}80}8182// Discard two buffers and re-read scattered values83ios.flushBefore(2*8192);8485for (int i = 0; i < 256; i++) {86long pos = 1000*i;87if (pos >= 2*8192) {88ios.seek(pos);89int val = ios.read();90if (val != i) {91System.out.println("Got bad value (2) at pos = " + (1000*i));92}93}94}95ios.close();9697byte[] data = os.toByteArray();98for (int i = 0; i < data.length; i++) {99byte val = data[i];100if ((i < 256000) && (i % 1000) == 0) {101if (val != (byte)(i/1000)) {102System.out.println("Got bad value (3) at pos = " + i);103}104} else {105byte gval = (byte)((i % (30*256)) % 256);106if (val != gval) {107System.out.println("Got bad value (4) at pos = " + i +108"(got " + (val & 0xff) +109" wanted " + (gval & 0xff) +")");110}111}112}113}114}115116117