jdk/src/java.base/share/classes/java/lang/AutoCloseable.java
author simonis
Fri, 14 Aug 2015 10:35:45 +0200
changeset 32209 24bb680a1609
parent 25859 3317bb8137f4
permissions -rw-r--r--
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX Reviewed-by: rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     1
/*
15647
314007859004 8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents: 10347
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     4
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    10
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    15
 * accompanied this code).
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    16
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    20
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    23
 * questions.
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    24
 */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    25
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    26
package java.lang;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    27
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    28
/**
19798
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    29
 * An object that may hold resources (such as file or socket handles)
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    30
 * until it is closed. The {@link #close()} method of an {@code AutoCloseable}
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    31
 * object is called automatically when exiting a {@code
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    32
 * try}-with-resources block for which the object has been declared in
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    33
 * the resource specification header. This construction ensures prompt
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    34
 * release, avoiding resource exhaustion exceptions and errors that
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    35
 * may otherwise occur.
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    36
 *
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    37
 * @apiNote
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    38
 * <p>It is possible, and in fact common, for a base class to
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    39
 * implement AutoCloseable even though not all of its subclasses or
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    40
 * instances will hold releasable resources.  For code that must operate
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    41
 * in complete generality, or when it is known that the {@code AutoCloseable}
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    42
 * instance requires resource release, it is recommended to use {@code
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    43
 * try}-with-resources constructions. However, when using facilities such as
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    44
 * {@link java.util.stream.Stream} that support both I/O-based and
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    45
 * non-I/O-based forms, {@code try}-with-resources blocks are in
b627a06c30ed 8022176: Weaken contract of java.lang.AutoCloseable
henryjen
parents: 15647
diff changeset
    46
 * general unnecessary when using non-I/O-based forms.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    47
 *
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    48
 * @author Josh Bloch
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    49
 * @since 1.7
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    50
 */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    51
public interface AutoCloseable {
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    52
    /**
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    53
     * Closes this resource, relinquishing any underlying resources.
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    54
     * This method is invoked automatically on objects managed by the
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    55
     * {@code try}-with-resources statement.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    56
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    57
     * <p>While this interface method is declared to throw {@code
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    58
     * Exception}, implementers are <em>strongly</em> encouraged to
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    59
     * declare concrete implementations of the {@code close} method to
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    60
     * throw more specific exceptions, or to throw no exception at all
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    61
     * if the close operation cannot fail.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    62
     *
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    63
     * <p> Cases where the close operation may fail require careful
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    64
     * attention by implementers. It is strongly advised to relinquish
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    65
     * the underlying resources and to internally <em>mark</em> the
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    66
     * resource as closed, prior to throwing the exception. The {@code
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    67
     * close} method is unlikely to be invoked more than once and so
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    68
     * this ensures that the resources are released in a timely manner.
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    69
     * Furthermore it reduces problems that could arise when the resource
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    70
     * wraps, or is wrapped, by another resource.
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 8786
diff changeset
    71
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    72
     * <p><em>Implementers of this interface are also strongly advised
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    73
     * to not have the {@code close} method throw {@link
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    74
     * InterruptedException}.</em>
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    75
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    76
     * This exception interacts with a thread's interrupted status,
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    77
     * and runtime misbehavior is likely to occur if an {@code
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    78
     * InterruptedException} is {@linkplain Throwable#addSuppressed
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    79
     * suppressed}.
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    80
     *
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    81
     * More generally, if it would cause problems for an
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    82
     * exception to be suppressed, the {@code AutoCloseable.close}
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    83
     * method should not throw it.
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    84
     *
6110
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    85
     * <p>Note that unlike the {@link java.io.Closeable#close close}
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    86
     * method of {@link java.io.Closeable}, this {@code close} method
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    87
     * is <em>not</em> required to be idempotent.  In other words,
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    88
     * calling this {@code close} method more than once may have some
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    89
     * visible side effect, unlike {@code Closeable.close} which is
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    90
     * required to have no effect if called more than once.
6db4b0180328 6963622: Project Coin: Refinements to suppressed exceptions
darcy
parents: 5972
diff changeset
    91
     *
8786
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    92
     * However, implementers of this interface are strongly encouraged
50520f3f8ada 7027362: Project Coin: warn of throwing InterruptedException in AutoCloseable.close javadoc
darcy
parents: 7988
diff changeset
    93
     * to make their {@code close} methods idempotent.
7988
d31b7cc371ef 7012279: Project Coin: Clarify AutoCloseable and Throwable javadoc
darcy
parents: 6515
diff changeset
    94
     *
5972
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    95
     * @throws Exception if this resource cannot be closed
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    96
     */
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    97
    void close() throws Exception;
e3f47656e9d9 6911258: Project Coin: Add essential API support for Automatic Resource Management (ARM) blocks
darcy
parents:
diff changeset
    98
}