Path: blob/master/src/java.base/share/classes/java/lang/AutoCloseable.java
41152 views
/*1* Copyright (c) 2009, 2013, 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 java.lang;2627/**28* An object that may hold resources (such as file or socket handles)29* until it is closed. The {@link #close()} method of an {@code AutoCloseable}30* object is called automatically when exiting a {@code31* try}-with-resources block for which the object has been declared in32* the resource specification header. This construction ensures prompt33* release, avoiding resource exhaustion exceptions and errors that34* may otherwise occur.35*36* @apiNote37* <p>It is possible, and in fact common, for a base class to38* implement AutoCloseable even though not all of its subclasses or39* instances will hold releasable resources. For code that must operate40* in complete generality, or when it is known that the {@code AutoCloseable}41* instance requires resource release, it is recommended to use {@code42* try}-with-resources constructions. However, when using facilities such as43* {@link java.util.stream.Stream} that support both I/O-based and44* non-I/O-based forms, {@code try}-with-resources blocks are in45* general unnecessary when using non-I/O-based forms.46*47* @author Josh Bloch48* @since 1.749*/50public interface AutoCloseable {51/**52* Closes this resource, relinquishing any underlying resources.53* This method is invoked automatically on objects managed by the54* {@code try}-with-resources statement.55*56* <p>While this interface method is declared to throw {@code57* Exception}, implementers are <em>strongly</em> encouraged to58* declare concrete implementations of the {@code close} method to59* throw more specific exceptions, or to throw no exception at all60* if the close operation cannot fail.61*62* <p> Cases where the close operation may fail require careful63* attention by implementers. It is strongly advised to relinquish64* the underlying resources and to internally <em>mark</em> the65* resource as closed, prior to throwing the exception. The {@code66* close} method is unlikely to be invoked more than once and so67* this ensures that the resources are released in a timely manner.68* Furthermore it reduces problems that could arise when the resource69* wraps, or is wrapped, by another resource.70*71* <p><em>Implementers of this interface are also strongly advised72* to not have the {@code close} method throw {@link73* InterruptedException}.</em>74*75* This exception interacts with a thread's interrupted status,76* and runtime misbehavior is likely to occur if an {@code77* InterruptedException} is {@linkplain Throwable#addSuppressed78* suppressed}.79*80* More generally, if it would cause problems for an81* exception to be suppressed, the {@code AutoCloseable.close}82* method should not throw it.83*84* <p>Note that unlike the {@link java.io.Closeable#close close}85* method of {@link java.io.Closeable}, this {@code close} method86* is <em>not</em> required to be idempotent. In other words,87* calling this {@code close} method more than once may have some88* visible side effect, unlike {@code Closeable.close} which is89* required to have no effect if called more than once.90*91* However, implementers of this interface are strongly encouraged92* to make their {@code close} methods idempotent.93*94* @throws Exception if this resource cannot be closed95*/96void close() throws Exception;97}9899100