Skip to content

Commit 0858743

Browse files
committed
8277585: Remove the terminally deprecated finalize() method from javax.imageio.stream APIs
Reviewed-by: achung, azvegint, serb
1 parent 884076f commit 0858743

File tree

10 files changed

+48
-214
lines changed

10 files changed

+48
-214
lines changed

src/java.desktop/share/classes/com/sun/imageio/plugins/common/SubImageInputStream.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,4 @@ public void seek(long pos) throws IOException {
7171
stream.seek(pos - startingPos);
7272
streamPos = pos;
7373
}
74-
75-
@SuppressWarnings("removal")
76-
protected void finalize() throws Throwable {
77-
// Empty finalizer (for improved performance; no need to call
78-
// super.finalize() in this case)
79-
}
8074
}

src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,6 @@ void finish() throws IOException {
146146
stream.seek(pos);
147147
stream.flushBefore(pos);
148148
}
149-
150-
@Override
151-
@SuppressWarnings("removal")
152-
protected void finalize() throws Throwable {
153-
// Empty finalizer (for improved performance; no need to call
154-
// super.finalize() in this case)
155-
}
156149
}
157150

158151
// Compress output and write as a series of 'IDAT' chunks of
@@ -283,13 +276,6 @@ void finish() throws IOException {
283276
def.end();
284277
}
285278
}
286-
287-
@Override
288-
@SuppressWarnings("removal")
289-
protected void finalize() throws Throwable {
290-
// Empty finalizer (for improved performance; no need to call
291-
// super.finalize() in this case)
292-
}
293279
}
294280

295281

src/java.desktop/share/classes/com/sun/imageio/stream/StreamFinalizer.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/java.desktop/share/classes/javax/imageio/stream/FileCacheImageInputStream.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.io.RandomAccessFile;
3232
import java.nio.file.Files;
3333
import com.sun.imageio.stream.StreamCloser;
34-
import com.sun.imageio.stream.StreamFinalizer;
3534
import sun.java2d.Disposer;
3635
import sun.java2d.DisposerRecord;
3736

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

6059
/** The referent to be registered with the Disposer. */
61-
private final Object disposerReferent;
60+
private final Object disposerReferent = new Object();
6261

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

111110
disposerRecord = new StreamDisposerRecord(cacheFile, cache);
112-
if (getClass() == FileCacheImageInputStream.class) {
113-
disposerReferent = new Object();
114-
Disposer.addRecord(disposerReferent, disposerRecord);
115-
} else {
116-
disposerReferent = new StreamFinalizer(this);
117-
}
111+
Disposer.addRecord(disposerReferent, disposerRecord);
118112
}
119113

120114
/**
@@ -258,22 +252,7 @@ public void close() throws IOException {
258252
StreamCloser.removeFromQueue(closeAction);
259253
}
260254

261-
/**
262-
* {@inheritDoc}
263-
*
264-
* @deprecated Finalization has been deprecated for removal. See
265-
* {@link java.lang.Object#finalize} for background information and details
266-
* about migration options.
267-
*/
268-
@Deprecated(since="9", forRemoval=true)
269-
@SuppressWarnings("removal")
270-
protected void finalize() throws Throwable {
271-
// Empty finalizer: for performance reasons we instead use the
272-
// Disposer mechanism for ensuring that the underlying
273-
// RandomAccessFile is closed/deleted prior to garbage collection
274-
}
275-
276-
private static class StreamDisposerRecord implements DisposerRecord {
255+
static class StreamDisposerRecord implements DisposerRecord {
277256
private File cacheFile;
278257
private RandomAccessFile cache;
279258

src/java.desktop/share/classes/javax/imageio/stream/FileCacheImageOutputStream.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
import java.io.OutputStream;
3131
import java.io.RandomAccessFile;
3232
import java.nio.file.Files;
33+
import javax.imageio.stream.FileCacheImageInputStream.StreamDisposerRecord;
3334
import com.sun.imageio.stream.StreamCloser;
35+
import sun.java2d.Disposer;
36+
import sun.java2d.DisposerRecord;
3437

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

4750
private RandomAccessFile cache;
4851

52+
private final Object disposerReferent = new Object();
53+
54+
private final StreamDisposerRecord disposerRecord;
4955
// Pos after last (rightmost) byte written
5056
private long maxStreamPos = 0L;
5157

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

100+
// If this instance becomes unreachable the disposer will clean up resources
101+
// used for caching. This can't flush any un-flushed cache.
102+
this.disposerRecord = new StreamDisposerRecord(cacheFile, cache);
103+
Disposer.addRecord(this.disposerReferent, this.disposerRecord);
104+
// If the VM is exiting and this instance is still reachable,
105+
// StreamCloser will call close() to flush the cache and clean up resources.
106+
// However closing the java.io.OutputStream is the application's responsibility.
94107
this.closeAction = StreamCloser.createCloseAction(this);
95108
StreamCloser.addToQueue(closeAction);
96109
}
@@ -231,9 +244,8 @@ public void close() throws IOException {
231244
seek(maxStreamPos);
232245
flushBefore(maxStreamPos);
233246
super.close();
234-
cache.close();
247+
disposerRecord.dispose();
235248
cache = null;
236-
cacheFile.delete();
237249
cacheFile = null;
238250
stream.flush();
239251
stream = null;

src/java.desktop/share/classes/javax/imageio/stream/FileImageInputStream.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.IOException;
3131
import java.io.RandomAccessFile;
3232
import com.sun.imageio.stream.CloseableDisposerRecord;
33-
import com.sun.imageio.stream.StreamFinalizer;
3433
import sun.java2d.Disposer;
3534

3635
/**
@@ -45,7 +44,7 @@ public class FileImageInputStream extends ImageInputStreamImpl {
4544
private RandomAccessFile raf;
4645

4746
/** The referent to be registered with the Disposer. */
48-
private final Object disposerReferent;
47+
private final Object disposerReferent = new Object();
4948

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

9796
disposerRecord = new CloseableDisposerRecord(raf);
98-
if (getClass() == FileImageInputStream.class) {
99-
disposerReferent = new Object();
100-
Disposer.addRecord(disposerReferent, disposerRecord);
101-
} else {
102-
disposerReferent = new StreamFinalizer(this);
103-
}
97+
Disposer.addRecord(disposerReferent, disposerRecord);
10498
}
10599

106100
public int read() throws IOException {
@@ -154,19 +148,4 @@ public void close() throws IOException {
154148
disposerRecord.dispose(); // this closes the RandomAccessFile
155149
raf = null;
156150
}
157-
158-
/**
159-
* {@inheritDoc}
160-
*
161-
* @deprecated Finalization has been deprecated for removal. See
162-
* {@link java.lang.Object#finalize} for background information and details
163-
* about migration options.
164-
*/
165-
@Deprecated(since="9", forRemoval=true)
166-
@SuppressWarnings("removal")
167-
protected void finalize() throws Throwable {
168-
// Empty finalizer: for performance reasons we instead use the
169-
// Disposer mechanism for ensuring that the underlying
170-
// RandomAccessFile is closed prior to garbage collection
171-
}
172151
}

src/java.desktop/share/classes/javax/imageio/stream/FileImageOutputStream.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.io.IOException;
3131
import java.io.RandomAccessFile;
3232
import com.sun.imageio.stream.CloseableDisposerRecord;
33-
import com.sun.imageio.stream.StreamFinalizer;
3433
import sun.java2d.Disposer;
3534

3635
/**
@@ -44,7 +43,7 @@ public class FileImageOutputStream extends ImageOutputStreamImpl {
4443
private RandomAccessFile raf;
4544

4645
/** The referent to be registered with the Disposer. */
47-
private final Object disposerReferent;
46+
private final Object disposerReferent = new Object();
4847

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

8988
disposerRecord = new CloseableDisposerRecord(raf);
90-
if (getClass() == FileImageOutputStream.class) {
91-
disposerReferent = new Object();
92-
Disposer.addRecord(disposerReferent, disposerRecord);
93-
} else {
94-
disposerReferent = new StreamFinalizer(this);
95-
}
89+
Disposer.addRecord(disposerReferent, disposerRecord);
9690
}
9791

9892
public int read() throws IOException {
@@ -162,19 +156,4 @@ public void close() throws IOException {
162156
disposerRecord.dispose(); // this closes the RandomAccessFile
163157
raf = null;
164158
}
165-
166-
/**
167-
* {@inheritDoc}
168-
*
169-
* @deprecated Finalization has been deprecated for removal. See
170-
* {@link java.lang.Object#finalize} for background information and details
171-
* about migration options.
172-
*/
173-
@Deprecated(since="9", forRemoval=true)
174-
@SuppressWarnings("removal")
175-
protected void finalize() throws Throwable {
176-
// Empty finalizer: for performance reasons we instead use the
177-
// Disposer mechanism for ensuring that the underlying
178-
// RandomAccessFile is closed prior to garbage collection
179-
}
180159
}

src/java.desktop/share/classes/javax/imageio/stream/ImageInputStreamImpl.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -841,29 +841,4 @@ public void close() throws IOException {
841841

842842
isClosed = true;
843843
}
844-
845-
/**
846-
* Finalizes this object prior to garbage collection. The
847-
* {@code close} method is called to close any open input
848-
* source. This method should not be called from application
849-
* code.
850-
*
851-
* @throws Throwable if an error occurs during superclass
852-
* finalization.
853-
*
854-
* @deprecated Finalization has been deprecated for removal. See
855-
* {@link java.lang.Object#finalize} for background information and details
856-
* about migration options.
857-
*/
858-
@Deprecated(since="9", forRemoval=true)
859-
@SuppressWarnings("removal")
860-
protected void finalize() throws Throwable {
861-
if (!isClosed) {
862-
try {
863-
close();
864-
} catch (IOException e) {
865-
}
866-
}
867-
super.finalize();
868-
}
869844
}

src/java.desktop/share/classes/javax/imageio/stream/MemoryCacheImageInputStream.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.io.InputStream;
2929
import java.io.IOException;
30-
import com.sun.imageio.stream.StreamFinalizer;
3130
import sun.java2d.Disposer;
3231
import sun.java2d.DisposerRecord;
3332

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

5251
/** The referent to be registered with the Disposer. */
53-
private final Object disposerReferent;
52+
private final Object disposerReferent = new Object();
5453

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

7372
disposerRecord = new StreamDisposerRecord(cache);
74-
if (getClass() == MemoryCacheImageInputStream.class) {
75-
disposerReferent = new Object();
76-
Disposer.addRecord(disposerReferent, disposerRecord);
77-
} else {
78-
disposerReferent = new StreamFinalizer(this);
79-
}
73+
Disposer.addRecord(disposerReferent, disposerRecord);
8074
}
8175

8276
public int read() throws IOException {
@@ -176,21 +170,6 @@ public void close() throws IOException {
176170
cache = null;
177171
}
178172

179-
/**
180-
* {@inheritDoc}
181-
*
182-
* @deprecated Finalization has been deprecated for removal. See
183-
* {@link java.lang.Object#finalize} for background information and details
184-
* about migration options.
185-
*/
186-
@Deprecated(since="9", forRemoval=true)
187-
@SuppressWarnings("removal")
188-
protected void finalize() throws Throwable {
189-
// Empty finalizer: for performance reasons we instead use the
190-
// Disposer mechanism for ensuring that the underlying
191-
// MemoryCache is reset prior to garbage collection
192-
}
193-
194173
private static class StreamDisposerRecord implements DisposerRecord {
195174
private MemoryCache cache;
196175

0 commit comments

Comments
 (0)