Skip to content

8362898: Remove finalize() methods from javax.imagio TIFF classes. #26416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import org.w3c.dom.Node;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

/**
* Base class for all possible forms of JPEG compression in TIFF.
Expand Down Expand Up @@ -219,12 +221,14 @@ protected void initJPEGWriter(boolean supportsStreamMetadata,
if(supportsStreamMetadata) {
String smName = spi.getNativeStreamMetadataFormatName();
if(smName == null || !smName.equals(STREAM_METADATA_NAME)) {
this.JPEGWriter.dispose();
this.JPEGWriter = null;
}
}
if(this.JPEGWriter != null && supportsImageMetadata) {
String imName = spi.getNativeImageMetadataFormatName();
if(imName == null || !imName.equals(IMAGE_METADATA_NAME)) {
this.JPEGWriter.dispose();
this.JPEGWriter = null;
}
}
Expand Down Expand Up @@ -263,6 +267,12 @@ protected void initJPEGWriter(boolean supportsStreamMetadata,

// Set the writer.
this.JPEGWriter = writer;
// The JDK built-in JPEGImageWriter will self-dispose.
// So a Disposer is only needed here if it is an unknown reader.
// This is not common, so likely this will rarely be needed.
if (!(this.JPEGWriter instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter)) {
Disposer.addRecord(this, new ImageWriterDisposerRecord(this.JPEGWriter));
}
break;
}

Expand Down Expand Up @@ -435,11 +445,16 @@ public final int encode(byte[] b, int off,
return compDataLength;
}

@SuppressWarnings("removal")
protected void finalize() throws Throwable {
super.finalize();
if(JPEGWriter != null) {
JPEGWriter.dispose();
private static class ImageWriterDisposerRecord implements DisposerRecord {
private final ImageWriter writer;

public ImageWriterDisposerRecord(ImageWriter writer) {
this.writer = writer;
}

@Override
public void dispose() {
writer.dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import javax.imageio.stream.ImageInputStream;
import javax.imageio.plugins.tiff.BaselineTIFFTagSet;
import javax.imageio.plugins.tiff.TIFFField;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

public class TIFFJPEGDecompressor extends TIFFDecompressor {
// Start of Image
Expand Down Expand Up @@ -65,6 +67,13 @@ public void beginDecoding() {
// Initialize reader to the first one.
this.JPEGReader = iter.next();

// The JDK built-in ImageReader will self-dispose.
// So a Disposer is only needed here if it is an unknown reader.
// This is not common, so likely this will rarely be needed.
if (!(this.JPEGReader instanceof com.sun.imageio.plugins.jpeg.JPEGImageReader)) {
Disposer.addRecord(this, new ImageReaderDisposerRecord(this.JPEGReader));
}

this.JPEGParam = JPEGReader.getDefaultReadParam();
}

Expand Down Expand Up @@ -139,9 +148,16 @@ public void decodeRaw(byte[] b,
JPEGReader.read(0, JPEGParam);
}

@SuppressWarnings("removal")
protected void finalize() throws Throwable {
super.finalize();
JPEGReader.dispose();
private static class ImageReaderDisposerRecord implements DisposerRecord {
private final ImageReader reader;

public ImageReaderDisposerRecord(ImageReader reader) {
this.reader = reader;
}

@Override
public void dispose() {
reader.dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,4 @@ public void decodeRaw(byte[] b,
JPEGReader.read(0, JPEGParam);
}

protected void finalize() throws Throwable {
super.finalize();
JPEGReader.dispose();
}
}