8236987: Remove call to System.out.println from ImageIcon.loadImage

Reviewed-by: aivanov, psadhukhan, prr
This commit is contained in:
Tejesh R 2022-04-14 11:10:22 +00:00 committed by Prasanta Sadhukhan
parent 339005dbc9
commit f26403172f
2 changed files with 96 additions and 1 deletions

View File

@ -321,6 +321,7 @@ public class ImageIcon implements Icon, Serializable, Accessible {
*/
protected void loadImage(Image image) {
MediaTracker mTracker = getTracker();
boolean interrupted = false;
synchronized(mTracker) {
int id = getNextID();
@ -328,11 +329,16 @@ public class ImageIcon implements Icon, Serializable, Accessible {
try {
mTracker.waitForID(id, 0);
} catch (InterruptedException e) {
System.out.println("INTERRUPTED while loading Image");
interrupted = true;
}
loadStatus = mTracker.statusID(id, false);
mTracker.removeImage(image, id);
if (interrupted && ((loadStatus & MediaTracker.LOADING) != 0)) {
loadStatus = MediaTracker.ABORTED;
}
width = image.getWidth(imageObserver);
height = image.getHeight(imageObserver);
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.MediaTracker;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
/*
* @test
* @bug 8236987
* @summary Verifies ImageIcon constructor produces no output when the
* thread is interrupted
* @run main LoadInterruptTest
*/
public class LoadInterruptTest {
private static ByteArrayOutputStream testOut;
private static PrintStream prevSysOut;
public static void main(String[] args) throws Exception {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
loadImageIcon();
}
});
} finally {
unsetOutput();
}
}
public static void setUpOutput() {
prevSysOut = System.out;
testOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(testOut, true, StandardCharsets.UTF_8));
}
public static void unsetOutput() {
if (prevSysOut != null) {
System.setOut(prevSysOut);
}
testOut = null;
}
private static void loadImageIcon() {
setUpOutput();
Thread.currentThread().interrupt();
ImageIcon i = new ImageIcon("https://openjdk.java.net/images/openjdk.png");
int status = i.getImageLoadStatus();
System.out.flush();
String outString = testOut.toString(StandardCharsets.UTF_8);
if (!outString.isEmpty()) {
throw new RuntimeException("Test Case Failed!!! System.out is not empty : " + outString);
}
if (status == MediaTracker.LOADING) {
throw new RuntimeException("Test Case Failed!!! LOADING... status!!!");
}
}
}