1/* 2 * Copyright (c) 2007, 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 6547241 27 * @summary Test verifies that concurrent usage of jpeg writer instance 28 * by number of threads does not cause crash in jpeg library. 29 * @run main ConcurrentWritingTest 30 */ 31 32import java.awt.Color; 33import java.awt.Graphics2D; 34import java.awt.RadialGradientPaint; 35import java.awt.geom.Point2D; 36import java.awt.image.BufferedImage; 37import java.io.File; 38import java.io.IOException; 39import javax.imageio.ImageIO; 40import javax.imageio.ImageWriter; 41import javax.imageio.stream.ImageOutputStream; 42 43public class ConcurrentWritingTest extends Thread { 44 45 static ImageWriter w = null; 46 static File pwd = new File("."); 47 static BufferedImage img; 48 49 private static int MAX_THREADS = 50; 50 private static int completeCount = 0; 51 private static Object lock = new Object(); 52 53 public static void main(String[] args) throws Exception { 54 img = createTestImage(); 55 56 w = ImageIO.getImageWritersByFormatName("JPEG").next(); 57 58 for (int i = 0; i < MAX_THREADS; i++) { 59 (new ConcurrentWritingTest()).start(); 60 } 61 62 // wait for threads 63 boolean needWait = true; 64 while(needWait) { 65 synchronized(lock) { 66 needWait = completeCount < MAX_THREADS; 67 } 68 } 69 System.out.println("Test PASSED"); 70 } 71 72 public void run() { 73 try { 74 File f = File.createTempFile("writer_", ".jpg", pwd); 75 ImageOutputStream ios = ImageIO.createImageOutputStream(f); 76 w.setOutput(ios); 77 Thread.sleep(70); 78 w.write(img); 79 Thread.sleep(70); 80 w.reset(); 81 } catch (IllegalStateException e) { 82 System.out.println(e); 83 } catch (IOException e) { 84 System.out.println(e); 85 } catch (Throwable e) { 86 // Unexpected exception. Test failed. 87 throw new RuntimeException("Test failed.", e); 88 } finally { 89 synchronized(lock) { 90 completeCount ++; 91 } 92 } 93 } 94 95 private static BufferedImage createTestImage() { 96 int w = 1024; 97 int h = 768; 98 99 BufferedImage img = new 100 BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 101 Graphics2D g = img.createGraphics(); 102 Color[] colors = { Color.red, Color.green, Color.blue }; 103 float[] dist = {0.0f, 0.5f, 1.0f }; 104 Point2D center = new Point2D.Float(0.5f * w, 0.5f * h); 105 106 RadialGradientPaint p = 107 new RadialGradientPaint(center, 0.5f * w, dist, colors); 108 g.setPaint(p); 109 g.fillRect(0, 0, w, h); 110 g.dispose(); 111 112 return img; 113 } 114} 115