提交 b58616eb 编写于 作者: B bae

6657133: Mutable statics in imageio plugins (findbugs)

Reviewed-by: prr
上级 9d18b9ea
...@@ -43,35 +43,35 @@ import javax.imageio.stream.ImageInputStream; ...@@ -43,35 +43,35 @@ import javax.imageio.stream.ImageInputStream;
*/ */
public class StreamCloser { public class StreamCloser {
private static WeakHashMap<ImageInputStream, Object> toCloseQueue; private static WeakHashMap<CloseAction, Object> toCloseQueue;
private static Thread streamCloser; private static Thread streamCloser;
public static void addToQueue(ImageInputStream iis) { public static void addToQueue(CloseAction ca) {
synchronized (StreamCloser.class) { synchronized (StreamCloser.class) {
if (toCloseQueue == null) { if (toCloseQueue == null) {
toCloseQueue = toCloseQueue =
new WeakHashMap<ImageInputStream, Object>(); new WeakHashMap<CloseAction, Object>();
} }
toCloseQueue.put(iis, null); toCloseQueue.put(ca, null);
if (streamCloser == null) { if (streamCloser == null) {
final Runnable streamCloserRunnable = new Runnable() { final Runnable streamCloserRunnable = new Runnable() {
public void run() { public void run() {
if (toCloseQueue != null) { if (toCloseQueue != null) {
synchronized (StreamCloser.class) { synchronized (StreamCloser.class) {
Set<ImageInputStream> set = Set<CloseAction> set =
toCloseQueue.keySet(); toCloseQueue.keySet();
// Make a copy of the set in order to avoid // Make a copy of the set in order to avoid
// concurrent modification (the is.close() // concurrent modification (the is.close()
// will in turn call removeFromQueue()) // will in turn call removeFromQueue())
ImageInputStream[] streams = CloseAction[] actions =
new ImageInputStream[set.size()]; new CloseAction[set.size()];
streams = set.toArray(streams); actions = set.toArray(actions);
for (ImageInputStream is : streams) { for (CloseAction ca : actions) {
if (is != null) { if (ca != null) {
try { try {
is.close(); ca.performAction();
} catch (IOException e) { } catch (IOException e) {
} }
} }
...@@ -106,10 +106,28 @@ public class StreamCloser { ...@@ -106,10 +106,28 @@ public class StreamCloser {
} }
} }
public static void removeFromQueue(ImageInputStream iis) { public static void removeFromQueue(CloseAction ca) {
synchronized (StreamCloser.class) { synchronized (StreamCloser.class) {
if (toCloseQueue != null) { if (toCloseQueue != null) {
toCloseQueue.remove(iis); toCloseQueue.remove(ca);
}
}
}
public static CloseAction createCloseAction(ImageInputStream iis) {
return new CloseAction(iis);
}
public static final class CloseAction {
private ImageInputStream iis;
private CloseAction(ImageInputStream iis) {
this.iis = iis;
}
public void performAction() throws IOException {
if (iis != null) {
iis.close();
} }
} }
} }
......
...@@ -78,7 +78,7 @@ public class BMPImageWriteParam extends ImageWriteParam { ...@@ -78,7 +78,7 @@ public class BMPImageWriteParam extends ImageWriteParam {
super(locale); super(locale);
// Set compression types ("BI_RGB" denotes uncompressed). // Set compression types ("BI_RGB" denotes uncompressed).
compressionTypes = BMPConstants.compressionTypeNames; compressionTypes = BMPConstants.compressionTypeNames.clone();
// Set compression flag. // Set compression flag.
canWriteCompressed = true; canWriteCompressed = true;
......
...@@ -62,6 +62,10 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl { ...@@ -62,6 +62,10 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl {
/** The DisposerRecord that closes the underlying cache. */ /** The DisposerRecord that closes the underlying cache. */
private final DisposerRecord disposerRecord; private final DisposerRecord disposerRecord;
/** The CloseAction that closes the stream in
* the StreamCloser's shutdown hook */
private final StreamCloser.CloseAction closeAction;
/** /**
* Constructs a <code>FileCacheImageInputStream</code> that will read * Constructs a <code>FileCacheImageInputStream</code> that will read
* from a given <code>InputStream</code>. * from a given <code>InputStream</code>.
...@@ -96,7 +100,9 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl { ...@@ -96,7 +100,9 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl {
this.cacheFile = this.cacheFile =
File.createTempFile("imageio", ".tmp", cacheDir); File.createTempFile("imageio", ".tmp", cacheDir);
this.cache = new RandomAccessFile(cacheFile, "rw"); this.cache = new RandomAccessFile(cacheFile, "rw");
StreamCloser.addToQueue(this);
this.closeAction = StreamCloser.createCloseAction(this);
StreamCloser.addToQueue(closeAction);
disposerRecord = new StreamDisposerRecord(cacheFile, cache); disposerRecord = new StreamDisposerRecord(cacheFile, cache);
if (getClass() == FileCacheImageInputStream.class) { if (getClass() == FileCacheImageInputStream.class) {
...@@ -242,7 +248,7 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl { ...@@ -242,7 +248,7 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl {
stream = null; stream = null;
cache = null; cache = null;
cacheFile = null; cacheFile = null;
StreamCloser.removeFromQueue(this); StreamCloser.removeFromQueue(closeAction);
} }
/** /**
......
...@@ -48,6 +48,10 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl { ...@@ -48,6 +48,10 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
// Pos after last (rightmost) byte written // Pos after last (rightmost) byte written
private long maxStreamPos = 0L; private long maxStreamPos = 0L;
/** The CloseAction that closes the stream in
* the StreamCloser's shutdown hook */
private final StreamCloser.CloseAction closeAction;
/** /**
* Constructs a <code>FileCacheImageOutputStream</code> that will write * Constructs a <code>FileCacheImageOutputStream</code> that will write
* to a given <code>outputStream</code>. * to a given <code>outputStream</code>.
...@@ -82,7 +86,9 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl { ...@@ -82,7 +86,9 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
this.cacheFile = this.cacheFile =
File.createTempFile("imageio", ".tmp", cacheDir); File.createTempFile("imageio", ".tmp", cacheDir);
this.cache = new RandomAccessFile(cacheFile, "rw"); this.cache = new RandomAccessFile(cacheFile, "rw");
StreamCloser.addToQueue(this);
this.closeAction = StreamCloser.createCloseAction(this);
StreamCloser.addToQueue(closeAction);
} }
public int read() throws IOException { public int read() throws IOException {
...@@ -227,7 +233,7 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl { ...@@ -227,7 +233,7 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl {
cacheFile = null; cacheFile = null;
stream.flush(); stream.flush();
stream = null; stream = null;
StreamCloser.removeFromQueue(this); StreamCloser.removeFromQueue(closeAction);
} }
public void flushBefore(long pos) throws IOException { public void flushBefore(long pos) throws IOException {
......
...@@ -127,7 +127,7 @@ system.scope=sun.security.provider.IdentityDatabase ...@@ -127,7 +127,7 @@ system.scope=sun.security.provider.IdentityDatabase
# passed to checkPackageAccess unless the # passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has # corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted. # been granted.
package.access=sun. package.access=sun.,com.sun.imageio.
# #
# List of comma-separated packages that start with or equal this string # List of comma-separated packages that start with or equal this string
......
...@@ -128,7 +128,7 @@ system.scope=sun.security.provider.IdentityDatabase ...@@ -128,7 +128,7 @@ system.scope=sun.security.provider.IdentityDatabase
# passed to checkPackageAccess unless the # passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has # corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted. # been granted.
package.access=sun. package.access=sun.,com.sun.imageio.
# #
# List of comma-separated packages that start with or equal this string # List of comma-separated packages that start with or equal this string
......
...@@ -128,7 +128,7 @@ system.scope=sun.security.provider.IdentityDatabase ...@@ -128,7 +128,7 @@ system.scope=sun.security.provider.IdentityDatabase
# passed to checkPackageAccess unless the # passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has # corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted. # been granted.
package.access=sun. package.access=sun.,com.sun.imageio.
# #
# List of comma-separated packages that start with or equal this string # List of comma-separated packages that start with or equal this string
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册