Path: blob/master/src/java.desktop/share/classes/com/sun/imageio/stream/StreamFinalizer.java
41161 views
/*1* Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.imageio.stream;2627import java.io.IOException;28import javax.imageio.stream.ImageInputStream;2930/**31* Small class to assist in properly closing an ImageInputStream instance32* prior to garbage collection. The ImageInputStreamImpl class defines a33* finalize() method, but in a number of its public subclasses34* (e.g. FileImageInputStream) we override the finalize() method to be35* empty for performance reasons, and instead rely on the Disposer mechanism36* for closing/disposing resources. This is fine when one of these classes37* is instantiated directly (e.g. new FileImageInputStream()) but in the38* unlikely case where a user defines their own subclass of one of those39* streams, we need some way to get back to the behavior of40* ImageInputStreamImpl, which will call close() as part of finalization.41*42* Typically an Image{Input,Output}Stream will construct an instance of43* StreamFinalizer in its constructor if it detects that it has been44* subclassed by the user. The ImageInputStream instance will hold a45* reference to the StreamFinalizer, and the StreamFinalizer will hold a46* reference back to the ImageInputStream from which it was created. When47* both are no longer reachable, the StreamFinalizer.finalize() method will48* be called, which will take care of closing down the ImageInputStream.49*50* Clearly this is a bit of a hack, but it will likely only be used in the51* rarest of circumstances: when a user has subclassed one of the public52* stream classes. (It should be no worse than the old days when the public53* stream classes had non-empty finalize() methods.)54*/55public class StreamFinalizer {56private ImageInputStream stream;5758public StreamFinalizer(ImageInputStream stream) {59this.stream = stream;60}6162@SuppressWarnings("deprecation")63protected void finalize() throws Throwable {64try {65stream.close();66} catch (IOException e) {67} finally {68stream = null;69super.finalize();70}71}72}737475