0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6547241
0N/A * @summary Test verifies that concurrent usage of jpeg writer instance
0N/A * by number of threads does not cause crash in jpeg library.
0N/A * @run main ConcurrentWritingTest
0N/A */
0N/A
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Graphics2D;
0N/Aimport java.awt.RadialGradientPaint;
0N/Aimport java.awt.geom.Point2D;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.ImageIO;
0N/Aimport javax.imageio.ImageWriter;
0N/Aimport javax.imageio.stream.ImageOutputStream;
0N/A
0N/Apublic class ConcurrentWritingTest extends Thread {
0N/A
0N/A static ImageWriter w = null;
0N/A static File pwd = new File(".");
0N/A static BufferedImage img;
0N/A
0N/A private static int MAX_THREADS = 50;
0N/A private static int completeCount = 0;
0N/A private static Object lock = new Object();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A img = createTestImage();
0N/A
0N/A w = ImageIO.getImageWritersByFormatName("JPEG").next();
0N/A
0N/A for (int i = 0; i < MAX_THREADS; i++) {
0N/A (new ConcurrentWritingTest()).start();
0N/A }
0N/A
0N/A // wait for threads
0N/A boolean needWait = true;
0N/A while(needWait) {
0N/A synchronized(lock) {
0N/A needWait = completeCount < MAX_THREADS;
0N/A }
0N/A }
0N/A System.out.println("Test PASSED");
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A File f = File.createTempFile("writer_", ".jpg", pwd);
0N/A ImageOutputStream ios = ImageIO.createImageOutputStream(f);
0N/A w.setOutput(ios);
0N/A Thread.sleep(70);
0N/A w.write(img);
0N/A Thread.sleep(70);
0N/A w.reset();
0N/A } catch (IllegalStateException e) {
0N/A System.out.println(e);
0N/A } catch (IOException e) {
0N/A System.out.println(e);
0N/A } catch (Throwable e) {
0N/A // Unexpected exception. Test failed.
0N/A throw new RuntimeException("Test failed.", e);
0N/A } finally {
0N/A synchronized(lock) {
0N/A completeCount ++;
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static BufferedImage createTestImage() {
0N/A int w = 1024;
0N/A int h = 768;
0N/A
0N/A BufferedImage img = new
0N/A BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
0N/A Graphics2D g = img.createGraphics();
0N/A Color[] colors = { Color.red, Color.green, Color.blue };
0N/A float[] dist = {0.0f, 0.5f, 1.0f };
0N/A Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
0N/A
0N/A RadialGradientPaint p =
0N/A new RadialGradientPaint(center, 0.5f * w, dist, colors);
0N/A g.setPaint(p);
0N/A g.fillRect(0, 0, w, h);
0N/A g.dispose();
0N/A
0N/A return img;
0N/A }
0N/A}