Skip to content

8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs #26650

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

Closed
wants to merge 4 commits into from
Closed
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 @@ -71,10 +71,4 @@ public void seek(long pos) throws IOException {
stream.seek(pos - startingPos);
streamPos = pos;
}

@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ void finish() throws IOException {
stream.seek(pos);
stream.flushBefore(pos);
}

@Override
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
}
}

// Compress output and write as a series of 'IDAT' chunks of
Expand Down Expand Up @@ -283,13 +276,6 @@ void finish() throws IOException {
def.end();
}
}

@Override
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer (for improved performance; no need to call
// super.finalize() in this case)
}
}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.RandomAccessFile;
import java.nio.file.Files;
import com.sun.imageio.stream.StreamCloser;
import com.sun.imageio.stream.StreamFinalizer;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

Expand All @@ -58,7 +57,7 @@ public class FileCacheImageInputStream extends ImageInputStreamImpl {
private boolean foundEOF = false;

/** The referent to be registered with the Disposer. */
private final Object disposerReferent;
private final Object disposerReferent = new Object();

/** The DisposerRecord that closes the underlying cache. */
private final DisposerRecord disposerRecord;
Expand Down Expand Up @@ -109,12 +108,7 @@ public FileCacheImageInputStream(InputStream stream, File cacheDir)
StreamCloser.addToQueue(closeAction);

disposerRecord = new StreamDisposerRecord(cacheFile, cache);
if (getClass() == FileCacheImageInputStream.class) {
disposerReferent = new Object();
Disposer.addRecord(disposerReferent, disposerRecord);
} else {
disposerReferent = new StreamFinalizer(this);
}
Disposer.addRecord(disposerReferent, disposerRecord);
}

/**
Expand Down Expand Up @@ -258,22 +252,7 @@ public void close() throws IOException {
StreamCloser.removeFromQueue(closeAction);
}

/**
* {@inheritDoc}
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer: for performance reasons we instead use the
// Disposer mechanism for ensuring that the underlying
// RandomAccessFile is closed/deleted prior to garbage collection
}

private static class StreamDisposerRecord implements DisposerRecord {
static class StreamDisposerRecord implements DisposerRecord {
private File cacheFile;
private RandomAccessFile cache;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import javax.imageio.stream.FileCacheImageInputStream.StreamDisposerRecord;
import com.sun.imageio.stream.StreamCloser;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

/**
* An implementation of {@code ImageOutputStream} that writes its
Expand All @@ -46,6 +49,9 @@ public class FileCacheImageOutputStream extends ImageOutputStreamImpl {

private RandomAccessFile cache;

private final Object disposerReferent = new Object();

private final StreamDisposerRecord disposerRecord;
// Pos after last (rightmost) byte written
private long maxStreamPos = 0L;

Expand Down Expand Up @@ -91,6 +97,13 @@ public FileCacheImageOutputStream(OutputStream stream, File cacheDir)
.toFile();
this.cache = new RandomAccessFile(cacheFile, "rw");

// If this instance becomes unreachable the disposer will clean up resources
// used for caching. This can't flush any un-flushed cache.
this.disposerRecord = new StreamDisposerRecord(cacheFile, cache);
Disposer.addRecord(this.disposerReferent, this.disposerRecord);
// If the VM is exiting and this instance is still reachable,
// StreamCloser will call close() to flush the cache and clean up resources.
// However closing the java.io.OutputStream is the application's responsibility.
this.closeAction = StreamCloser.createCloseAction(this);
StreamCloser.addToQueue(closeAction);
}
Expand Down Expand Up @@ -231,9 +244,8 @@ public void close() throws IOException {
seek(maxStreamPos);
flushBefore(maxStreamPos);
super.close();
cache.close();
disposerRecord.dispose();
cache = null;
cacheFile.delete();
cacheFile = null;
stream.flush();
stream = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import com.sun.imageio.stream.CloseableDisposerRecord;
import com.sun.imageio.stream.StreamFinalizer;
import sun.java2d.Disposer;

/**
Expand All @@ -45,7 +44,7 @@ public class FileImageInputStream extends ImageInputStreamImpl {
private RandomAccessFile raf;

/** The referent to be registered with the Disposer. */
private final Object disposerReferent;
private final Object disposerReferent = new Object();

/** The DisposerRecord that closes the underlying RandomAccessFile. */
private final CloseableDisposerRecord disposerRecord;
Expand Down Expand Up @@ -95,12 +94,7 @@ public FileImageInputStream(RandomAccessFile raf) {
}

disposerRecord = new CloseableDisposerRecord(raf);
if (getClass() == FileImageInputStream.class) {
disposerReferent = new Object();
Disposer.addRecord(disposerReferent, disposerRecord);
} else {
disposerReferent = new StreamFinalizer(this);
}
Disposer.addRecord(disposerReferent, disposerRecord);
}

public int read() throws IOException {
Expand Down Expand Up @@ -154,19 +148,4 @@ public void close() throws IOException {
disposerRecord.dispose(); // this closes the RandomAccessFile
raf = null;
}

/**
* {@inheritDoc}
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer: for performance reasons we instead use the
// Disposer mechanism for ensuring that the underlying
// RandomAccessFile is closed prior to garbage collection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import com.sun.imageio.stream.CloseableDisposerRecord;
import com.sun.imageio.stream.StreamFinalizer;
import sun.java2d.Disposer;

/**
Expand All @@ -44,7 +43,7 @@ public class FileImageOutputStream extends ImageOutputStreamImpl {
private RandomAccessFile raf;

/** The referent to be registered with the Disposer. */
private final Object disposerReferent;
private final Object disposerReferent = new Object();

/** The DisposerRecord that closes the underlying RandomAccessFile. */
private final CloseableDisposerRecord disposerRecord;
Expand Down Expand Up @@ -87,12 +86,7 @@ public FileImageOutputStream(RandomAccessFile raf) {
}

disposerRecord = new CloseableDisposerRecord(raf);
if (getClass() == FileImageOutputStream.class) {
disposerReferent = new Object();
Disposer.addRecord(disposerReferent, disposerRecord);
} else {
disposerReferent = new StreamFinalizer(this);
}
Disposer.addRecord(disposerReferent, disposerRecord);
}

public int read() throws IOException {
Expand Down Expand Up @@ -162,19 +156,4 @@ public void close() throws IOException {
disposerRecord.dispose(); // this closes the RandomAccessFile
raf = null;
}

/**
* {@inheritDoc}
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer: for performance reasons we instead use the
// Disposer mechanism for ensuring that the underlying
// RandomAccessFile is closed prior to garbage collection
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -841,29 +841,4 @@ public void close() throws IOException {

isClosed = true;
}

/**
* Finalizes this object prior to garbage collection. The
* {@code close} method is called to close any open input
* source. This method should not be called from application
* code.
*
* @throws Throwable if an error occurs during superclass
* finalization.
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
if (!isClosed) {
try {
close();
} catch (IOException e) {
}
}
super.finalize();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.InputStream;
import java.io.IOException;
import com.sun.imageio.stream.StreamFinalizer;
import sun.java2d.Disposer;
import sun.java2d.DisposerRecord;

Expand All @@ -50,7 +49,7 @@ public class MemoryCacheImageInputStream extends ImageInputStreamImpl {
private MemoryCache cache = new MemoryCache();

/** The referent to be registered with the Disposer. */
private final Object disposerReferent;
private final Object disposerReferent = new Object();

/** The DisposerRecord that resets the underlying MemoryCache. */
private final DisposerRecord disposerRecord;
Expand All @@ -71,12 +70,7 @@ public MemoryCacheImageInputStream(InputStream stream) {
this.stream = stream;

disposerRecord = new StreamDisposerRecord(cache);
if (getClass() == MemoryCacheImageInputStream.class) {
disposerReferent = new Object();
Disposer.addRecord(disposerReferent, disposerRecord);
} else {
disposerReferent = new StreamFinalizer(this);
}
Disposer.addRecord(disposerReferent, disposerRecord);
}

public int read() throws IOException {
Expand Down Expand Up @@ -176,21 +170,6 @@ public void close() throws IOException {
cache = null;
}

/**
* {@inheritDoc}
*
* @deprecated Finalization has been deprecated for removal. See
* {@link java.lang.Object#finalize} for background information and details
* about migration options.
*/
@Deprecated(since="9", forRemoval=true)
@SuppressWarnings("removal")
protected void finalize() throws Throwable {
// Empty finalizer: for performance reasons we instead use the
// Disposer mechanism for ensuring that the underlying
// MemoryCache is reset prior to garbage collection
}

private static class StreamDisposerRecord implements DisposerRecord {
private MemoryCache cache;

Expand Down
Loading