Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ConcurrentReadingTest.java
41152 views
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 reader instance
28
* by number of threads does not cause crash in jpeg library.
29
* @run main ConcurrentReadingTest
30
*/
31
32
import java.awt.Color;
33
import java.awt.Graphics2D;
34
import java.awt.RadialGradientPaint;
35
import java.awt.geom.Point2D;
36
import java.awt.image.BufferedImage;
37
import java.io.File;
38
import java.io.IOException;
39
import javax.imageio.ImageIO;
40
import javax.imageio.ImageReader;
41
import javax.imageio.ImageReadParam;
42
import javax.imageio.stream.ImageInputStream;
43
44
public class ConcurrentReadingTest extends Thread {
45
46
static ImageReader r = null;
47
static File file = new File("IMGP1001.JPG");
48
49
private static final int MAX_THREADS = 50;
50
51
static int completeCount = 0;;
52
static Object lock = new Object();
53
54
public static void main(String[] args) throws Exception {
55
createTestFile();
56
57
ImageInputStream iis = ImageIO.createImageInputStream(file);
58
r = ImageIO.getImageReaders(iis).next();
59
iis.close();
60
61
for (int i = 0; i < MAX_THREADS; i++) {
62
(new ConcurrentReadingTest()).start();
63
}
64
65
// wait for started threads
66
boolean needWait = true;
67
while (needWait) {
68
Thread.sleep(100);
69
synchronized(lock) {
70
needWait = completeCount < MAX_THREADS;
71
}
72
}
73
System.out.println("Test PASSED.");
74
}
75
76
public void run() {
77
try {
78
ImageInputStream iis = ImageIO.createImageInputStream(file);
79
r.setInput(iis);
80
ImageReadParam p = r.getDefaultReadParam();
81
Thread.sleep(70);
82
BufferedImage res = r.read(0, p);
83
Thread.sleep(70);
84
r.reset();
85
} catch (IllegalStateException e) {
86
System.out.println(e);
87
} catch (IOException e) {
88
System.out.println(e);
89
} catch (Throwable e) {
90
// Unexpected exception. Test failed.
91
throw new RuntimeException("Test failed.", e);
92
} finally {
93
synchronized(lock) {
94
completeCount ++;
95
}
96
}
97
}
98
99
private static void createTestFile() {
100
int w = 1280;
101
int h = 1024;
102
103
BufferedImage img = new
104
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
105
Graphics2D g = img.createGraphics();
106
Color[] colors = { Color.red, Color.green, Color.blue };
107
float[] dist = {0.0f, 0.5f, 1.0f };
108
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
109
110
RadialGradientPaint p =
111
new RadialGradientPaint(center, 0.5f * w, dist, colors);
112
g.setPaint(p);
113
g.fillRect(0, 0, w, h);
114
g.dispose();
115
116
try {
117
System.out.println("Create test image " + file.getAbsolutePath());
118
boolean b = ImageIO.write(img, "JPEG", file);
119
if (!b) {
120
throw new RuntimeException("Failed to create test image.");
121
}
122
} catch (IOException e) {
123
throw new RuntimeException("Test failed", e);
124
}
125
}
126
}
127
128