0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.imageio.stream;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport javax.imageio.stream.ImageInputStream;
0N/A
0N/A/**
0N/A * Small class to assist in properly closing an ImageInputStream instance
0N/A * prior to garbage collection. The ImageInputStreamImpl class defines a
0N/A * finalize() method, but in a number of its public subclasses
0N/A * (e.g. FileImageInputStream) we override the finalize() method to be
0N/A * empty for performance reasons, and instead rely on the Disposer mechanism
0N/A * for closing/disposing resources. This is fine when one of these classes
0N/A * is instantiated directly (e.g. new FileImageInputStream()) but in the
0N/A * unlikely case where a user defines their own subclass of one of those
0N/A * streams, we need some way to get back to the behavior of
0N/A * ImageInputStreamImpl, which will call close() as part of finalization.
0N/A *
0N/A * Typically an Image{Input,Output}Stream will construct an instance of
0N/A * StreamFinalizer in its constructor if it detects that it has been
0N/A * subclassed by the user. The ImageInputStream instance will hold a
0N/A * reference to the StreamFinalizer, and the StreamFinalizer will hold a
0N/A * reference back to the ImageInputStream from which it was created. When
0N/A * both are no longer reachable, the StreamFinalizer.finalize() method will
0N/A * be called, which will take care of closing down the ImageInputStream.
0N/A *
0N/A * Clearly this is a bit of a hack, but it will likely only be used in the
0N/A * rarest of circumstances: when a user has subclassed one of the public
0N/A * stream classes. (It should be no worse than the old days when the public
0N/A * stream classes had non-empty finalize() methods.)
0N/A */
0N/Apublic class StreamFinalizer {
0N/A private ImageInputStream stream;
0N/A
0N/A public StreamFinalizer(ImageInputStream stream) {
0N/A this.stream = stream;
0N/A }
0N/A
0N/A protected void finalize() throws Throwable {
0N/A try {
0N/A stream.close();
0N/A } catch (IOException e) {
0N/A } finally {
0N/A stream = null;
0N/A super.finalize();
0N/A }
0N/A }
0N/A}