Skip to content

Package: Closeable

Closeable

Coverage

1: /*
2: * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
3: * Copyright (c) 2018 Payara Services Ltd.
4: *
5: * This program and the accompanying materials are made available under the
6: * terms of the Eclipse Public License v. 2.0, which is available at
7: * http://www.eclipse.org/legal/epl-2.0.
8: *
9: * This Source Code may also be made available under the following Secondary
10: * Licenses when the conditions for such availability set forth in the
11: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
12: * version 2 with the GNU Classpath Exception, which is available at
13: * https://www.gnu.org/software/classpath/license.html.
14: *
15: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16: */
17:
18: package org.glassfish.grizzly;
19:
20: import java.io.IOException;
21: import java.util.concurrent.Future;
22:
23: /**
24: * General asynchronous closable interface.
25: *
26: * <tt>Closeable</tt> interface contains two sets of methods: close* and terminate*, so interface implementations can
27: * provide graceful and abrupt releasing of resources.
28: *
29: * @see java.io.Closeable
30: * @author Alexey Stashok
31: */
32: public interface Closeable {
33: /**
34: * Is <tt>Closeable</tt> open and ready. Returns <tt>true</tt>, if the <tt>Closeable</tt> is open and ready, or
35: * <tt>false</tt> otherwise.
36: *
37: * @return <tt>true</tt>, if <tt>Closeable</tt> is open and ready, or <tt>false</tt> otherwise.
38: */
39: boolean isOpen();
40:
41: /**
42: * Checks if this <tt>Closeable</tt> is open and ready to be used. If this <tt>Closeable</tt> is closed then an
43: * IOException will be thrown
44: *
45: * @throws IOException giving the reason why this <tt>Closeable</tt> was closed.
46: */
47: void assertOpen() throws IOException;
48:
49: /**
50: * Closes this stream and releases any system resources associated with it.
51: *
52: * If the stream is already closed then invoking this method has no effect. Use this method, when no completion
53: * notification is needed.
54: */
55: void terminateSilently();
56:
57: /**
58: * Closes this stream and releases any system resources associated with it. If the stream is already closed then
59: * invoking this method has no effect.
60: *
61: * @return {@link java.util.concurrent.Future}, which could be checked in case, if close operation will be run
62: * asynchronously
63: */
64: GrizzlyFuture<Closeable> terminate();
65:
66: /**
67: * Closes the <tt>Closeable</tt> and provides the reason description.
68: *
69: * This method is similar to {@link #terminateSilently()}, but additionally provides the reason why the
70: * <tt>Closeable</tt> will be closed.
71: *
72: * @param cause reason why terminated. This will be thrown is {@link #isOpen()} is called subsequently
73: */
74: void terminateWithReason(IOException cause);
75:
76: /**
77: * Gracefully (if supported by the implementation) closes this stream and releases any system resources associated with
78: * it.
79: *
80: * If the stream is already closed then invoking this method has no effect. Use this method, when no completion
81: * notification is needed.
82: */
83: void closeSilently();
84:
85: /**
86: * Gracefully (if supported by the implementation) closes this stream and releases any system resources associated with
87: * it. If the stream is already closed then invoking this method has no effect.
88: *
89: * @return {@link java.util.concurrent.Future}, which could be checked in case, if close operation will be run
90: * asynchronously
91: * @see java.io.Closeable#close() which is not asynchronous
92: */
93: GrizzlyFuture<Closeable> close();
94:
95: /**
96: * Gracefully closes this stream and releases any system resources associated with it. This operation waits for all
97: * pending output data to be flushed before closing the stream. If the stream is already closed then invoking this
98: * method has no effect.
99: *
100: * @param completionHandler {@link CompletionHandler} to be called, when the stream is closed
101: * @deprecated please use {@link #close()} with the following
102: * {@link GrizzlyFuture#addCompletionHandler(org.glassfish.grizzly.CompletionHandler)} call
103: */
104: @Deprecated
105: void close(CompletionHandler<Closeable> completionHandler);
106:
107: /**
108: * Gracefully closes the <tt>Closeable</tt> and provides the reason description.
109: *
110: * This method is similar to {@link #closeSilently()}, but additionally provides the reason why the <tt>Closeable</tt>
111: * will be closed.
112: *
113: * @param cause reason why closed, this will be thrown by {@link #isOpen()} if called subsequently
114: */
115: void closeWithReason(IOException cause);
116:
117: /**
118: * Add the {@link CloseListener}, which will be notified once the stream will be closed.
119: *
120: * @param closeListener {@link CloseListener}.
121: */
122: void addCloseListener(CloseListener closeListener);
123:
124: /**
125: * Remove the {@link CloseListener}.
126: *
127: * @param closeListener {@link CloseListener}.
128: * @return <tt>true</tt> if the listener was successfully removed, or <tt>false</tt> otherwise.
129: */
130: boolean removeCloseListener(CloseListener closeListener);
131:
132: /**
133: * @return the {@link Future}, that will be notified once this <tt>Closeable</tt> is closed
134: * @since 2.3.24
135: */
136: GrizzlyFuture<CloseReason> closeFuture();
137: }