author | mchung |
Tue, 06 Nov 2018 10:01:16 -0800 | |
changeset 52427 | 3c6aa484536c |
parent 48941 | 4f11514fe783 |
child 58242 | 94bb65cb37d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
2 |
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
28 |
import java.util.ArrayList; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
29 |
import java.util.List; |
48941 | 30 |
import java.util.Objects; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
31 |
|
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
48941
diff
changeset
|
32 |
import jdk.internal.access.JavaIOFileDescriptorAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
48941
diff
changeset
|
33 |
import jdk.internal.access.SharedSecrets; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
34 |
import jdk.internal.ref.PhantomCleanable; |
2 | 35 |
|
36 |
/** |
|
37 |
* Instances of the file descriptor class serve as an opaque handle |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
38 |
* to the underlying machine-specific structure representing an open |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
39 |
* file, an open socket, or another source or sink of bytes. |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
40 |
* The main practical use for a file descriptor is to create a |
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
41 |
* {@link FileInputStream} or {@link FileOutputStream} to contain it. |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
42 |
* <p> |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
43 |
* Applications should not create their own file descriptors. |
2 | 44 |
* |
45 |
* @author Pavani Diwanji |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
11017
diff
changeset
|
46 |
* @since 1.0 |
2 | 47 |
*/ |
48 |
public final class FileDescriptor { |
|
49 |
||
50 |
private int fd; |
|
51 |
||
52 |
private long handle; |
|
53 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
54 |
private Closeable parent; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
55 |
private List<Closeable> otherParents; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
56 |
private boolean closed; |
2 | 57 |
|
58 |
/** |
|
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
59 |
* true, if file is opened for appending. |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
60 |
*/ |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
61 |
private boolean append; |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
62 |
|
2 | 63 |
static { |
64 |
initIDs(); |
|
65 |
} |
|
66 |
||
67 |
// Set up JavaIOFileDescriptorAccess in SharedSecrets |
|
68 |
static { |
|
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
28425
diff
changeset
|
69 |
SharedSecrets.setJavaIOFileDescriptorAccess( |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
70 |
new JavaIOFileDescriptorAccess() { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
71 |
public void set(FileDescriptor fdo, int fd) { |
48941 | 72 |
fdo.set(fd); |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
73 |
} |
2 | 74 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
75 |
public int get(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
76 |
return fdo.fd; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
77 |
} |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
78 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
79 |
public void setAppend(FileDescriptor fdo, boolean append) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
80 |
fdo.append = append; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
81 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
82 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
83 |
public boolean getAppend(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
84 |
return fdo.append; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
85 |
} |
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
86 |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
87 |
public void close(FileDescriptor fdo) throws IOException { |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
88 |
fdo.close(); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
89 |
} |
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
90 |
|
48941 | 91 |
/* Register for a normal FileCleanable fd/handle cleanup. */ |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
92 |
public void registerCleanup(FileDescriptor fdo) { |
48941 | 93 |
FileCleanable.register(fdo); |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
94 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
95 |
|
48941 | 96 |
/* Register a custom PhantomCleanup. */ |
97 |
public void registerCleanup(FileDescriptor fdo, |
|
98 |
PhantomCleanable<FileDescriptor> cleanup) { |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
99 |
fdo.registerCleanup(cleanup); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
100 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
101 |
|
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
102 |
public void unregisterCleanup(FileDescriptor fdo) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
103 |
fdo.unregisterCleanup(); |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
104 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
105 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
106 |
public void setHandle(FileDescriptor fdo, long handle) { |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
107 |
fdo.setHandle(handle); |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
108 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
109 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
110 |
public long getHandle(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
111 |
return fdo.handle; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
112 |
} |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
113 |
} |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
114 |
); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
115 |
} |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
116 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
117 |
/** |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
118 |
* Cleanup in case FileDescriptor is not explicitly closed. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
119 |
*/ |
48941 | 120 |
private PhantomCleanable<FileDescriptor> cleanup; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
121 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
122 |
/** |
48941 | 123 |
* Constructs an (invalid) FileDescriptor object. |
124 |
* The fd or handle is set later. |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
125 |
*/ |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
126 |
public FileDescriptor() { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
127 |
fd = -1; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
128 |
handle = -1; |
2 | 129 |
} |
130 |
||
131 |
/** |
|
48941 | 132 |
* Used for standard input, output, and error only. |
133 |
* For Windows the corresponding handle is initialized. |
|
134 |
* For Unix the append mode is cached. |
|
135 |
* @param fd the raw fd number (0, 1, 2) |
|
136 |
*/ |
|
137 |
private FileDescriptor(int fd) { |
|
138 |
this.fd = fd; |
|
139 |
this.handle = getHandle(fd); |
|
140 |
this.append = getAppend(fd); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
2 | 144 |
* A handle to the standard input stream. Usually, this file |
145 |
* descriptor is not used directly, but rather via the input stream |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
146 |
* known as {@code System.in}. |
2 | 147 |
* |
148 |
* @see java.lang.System#in |
|
149 |
*/ |
|
48941 | 150 |
public static final FileDescriptor in = new FileDescriptor(0); |
2 | 151 |
|
152 |
/** |
|
153 |
* A handle to the standard output stream. Usually, this file |
|
154 |
* descriptor is not used directly, but rather via the output stream |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
155 |
* known as {@code System.out}. |
2 | 156 |
* @see java.lang.System#out |
157 |
*/ |
|
48941 | 158 |
public static final FileDescriptor out = new FileDescriptor(1); |
2 | 159 |
|
160 |
/** |
|
161 |
* A handle to the standard error stream. Usually, this file |
|
162 |
* descriptor is not used directly, but rather via the output stream |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
163 |
* known as {@code System.err}. |
2 | 164 |
* |
165 |
* @see java.lang.System#err |
|
166 |
*/ |
|
48941 | 167 |
public static final FileDescriptor err = new FileDescriptor(2); |
2 | 168 |
|
169 |
/** |
|
170 |
* Tests if this file descriptor object is valid. |
|
171 |
* |
|
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
172 |
* @return {@code true} if the file descriptor object represents a |
2 | 173 |
* valid, open file, socket, or other active I/O connection; |
48
dc5744ca15ea
4960438: (process) Need IO redirection API for subprocesses
martin
parents:
2
diff
changeset
|
174 |
* {@code false} otherwise. |
2 | 175 |
*/ |
176 |
public boolean valid() { |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
177 |
return (handle != -1) || (fd != -1); |
2 | 178 |
} |
179 |
||
180 |
/** |
|
181 |
* Force all system buffers to synchronize with the underlying |
|
182 |
* device. This method returns after all modified data and |
|
183 |
* attributes of this FileDescriptor have been written to the |
|
184 |
* relevant device(s). In particular, if this FileDescriptor |
|
185 |
* refers to a physical storage medium, such as a file in a file |
|
186 |
* system, sync will not return until all in-memory modified copies |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
187 |
* of buffers associated with this FileDescriptor have been |
2 | 188 |
* written to the physical medium. |
189 |
* |
|
190 |
* sync is meant to be used by code that requires physical |
|
191 |
* storage (such as a file) to be in a known state For |
|
192 |
* example, a class that provided a simple transaction facility |
|
193 |
* might use sync to ensure that all changes to a file caused |
|
194 |
* by a given transaction were recorded on a storage medium. |
|
195 |
* |
|
196 |
* sync only affects buffers downstream of this FileDescriptor. If |
|
197 |
* any in-memory buffering is being done by the application (for |
|
198 |
* example, by a BufferedOutputStream object), those buffers must |
|
199 |
* be flushed into the FileDescriptor (for example, by invoking |
|
200 |
* OutputStream.flush) before that data will be affected by sync. |
|
201 |
* |
|
202 |
* @exception SyncFailedException |
|
203 |
* Thrown when the buffers cannot be flushed, |
|
204 |
* or because the system cannot guarantee that all the |
|
205 |
* buffers have been synchronized with physical media. |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
11017
diff
changeset
|
206 |
* @since 1.1 |
2 | 207 |
*/ |
208 |
public native void sync() throws SyncFailedException; |
|
209 |
||
210 |
/* This routine initializes JNI field offsets for the class */ |
|
211 |
private static native void initIDs(); |
|
212 |
||
48941 | 213 |
/* |
214 |
* On Windows return the handle for the standard streams. |
|
215 |
*/ |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
216 |
private static native long getHandle(int d); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
217 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
218 |
/** |
48941 | 219 |
* Returns true, if the file was opened for appending. |
220 |
*/ |
|
221 |
private static native boolean getAppend(int fd); |
|
222 |
||
223 |
/** |
|
224 |
* Set the fd. |
|
225 |
* Used on Unix and for sockets on Windows and Unix. |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
226 |
* If setting to -1, clear the cleaner. |
48941 | 227 |
* The {@link #registerCleanup} method should be called for new fds. |
228 |
* @param fd the raw fd or -1 to indicate closed |
|
229 |
*/ |
|
230 |
@SuppressWarnings("unchecked") |
|
231 |
synchronized void set(int fd) { |
|
232 |
if (fd == -1 && cleanup != null) { |
|
233 |
cleanup.clear(); |
|
234 |
cleanup = null; |
|
235 |
} |
|
236 |
this.fd = fd; |
|
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Set the handle. |
|
241 |
* Used on Windows for regular files. |
|
242 |
* If setting to -1, clear the cleaner. |
|
243 |
* The {@link #registerCleanup} method should be called for new handles. |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
244 |
* @param handle the handle or -1 to indicate closed |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
245 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
246 |
@SuppressWarnings("unchecked") |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
247 |
void setHandle(long handle) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
248 |
if (handle == -1 && cleanup != null) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
249 |
cleanup.clear(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
250 |
cleanup = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
251 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
252 |
this.handle = handle; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
253 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
254 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
255 |
/** |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
256 |
* Register a cleanup for the current handle. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
257 |
* Used directly in java.io and indirectly via fdAccess. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
258 |
* The cleanup should be registered after the handle is set in the FileDescriptor. |
48941 | 259 |
* @param cleanable a PhantomCleanable to register |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
260 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
261 |
@SuppressWarnings("unchecked") |
48941 | 262 |
synchronized void registerCleanup(PhantomCleanable<FileDescriptor> cleanable) { |
263 |
Objects.requireNonNull(cleanable, "cleanable"); |
|
264 |
if (cleanup != null) { |
|
265 |
cleanup.clear(); |
|
266 |
} |
|
267 |
cleanup = cleanable; |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
268 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
269 |
|
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
270 |
/** |
48941 | 271 |
* Unregister a cleanup for the current raw fd or handle. |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
272 |
* Used directly in java.io and indirectly via fdAccess. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
273 |
* Normally {@link #close()} should be used except in cases where |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
274 |
* it is certain the caller will close the raw fd and the cleanup |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
275 |
* must not close the raw fd. {@link #unregisterCleanup()} must be |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
276 |
* called before the raw fd is closed to prevent a race that makes |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
277 |
* it possible for the fd to be reallocated to another use and later |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
278 |
* the cleanup might be invoked. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
279 |
*/ |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
280 |
synchronized void unregisterCleanup() { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
281 |
if (cleanup != null) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
282 |
cleanup.clear(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
283 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
284 |
cleanup = null; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
285 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
286 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
287 |
/** |
48941 | 288 |
* Close the raw file descriptor or handle, if it has not already been closed. |
289 |
* The native code sets the fd and handle to -1. |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
290 |
* Clear the cleaner so the close does not happen twice. |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
291 |
* Package private to allow it to be used in java.io. |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
292 |
* @throws IOException if close fails |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
293 |
*/ |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
294 |
@SuppressWarnings("unchecked") |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
295 |
synchronized void close() throws IOException { |
48941 | 296 |
unregisterCleanup(); |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
297 |
close0(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
298 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
299 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
300 |
/* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
301 |
* Close the raw file descriptor or handle, if it has not already been closed |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
302 |
* and set the fd and handle to -1. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
303 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
304 |
private native void close0() throws IOException; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
305 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
306 |
/* |
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
307 |
* Package private methods to track referents. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
308 |
* If multiple streams point to the same FileDescriptor, we cycle |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
309 |
* through the list of all referents and call close() |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
310 |
*/ |
2 | 311 |
|
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
312 |
/** |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
313 |
* Attach a Closeable to this FD for tracking. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
314 |
* parent reference is added to otherParents when |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
315 |
* needed to make closeAll simpler. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
316 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
317 |
synchronized void attach(Closeable c) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
318 |
if (parent == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
319 |
// first caller gets to do this |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
320 |
parent = c; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
321 |
} else if (otherParents == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
322 |
otherParents = new ArrayList<>(); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
323 |
otherParents.add(parent); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
324 |
otherParents.add(c); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
325 |
} else { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
326 |
otherParents.add(c); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
327 |
} |
2 | 328 |
} |
329 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
330 |
/** |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
331 |
* Cycle through all Closeables sharing this FD and call |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
332 |
* close() on each one. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
333 |
* |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
334 |
* The caller closeable gets to call close0(). |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
335 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
336 |
@SuppressWarnings("try") |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
337 |
synchronized void closeAll(Closeable releaser) throws IOException { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
338 |
if (!closed) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
339 |
closed = true; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
340 |
IOException ioe = null; |
28425
241dd1e2d9cb
8068948: Update java.base module to use new try-with-resources statement
darcy
parents:
27263
diff
changeset
|
341 |
try (releaser) { |
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
342 |
if (otherParents != null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
343 |
for (Closeable referent : otherParents) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
344 |
try { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
345 |
referent.close(); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
346 |
} catch(IOException x) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
347 |
if (ioe == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
348 |
ioe = x; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
349 |
} else { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
350 |
ioe.addSuppressed(x); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
351 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
352 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
353 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
354 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
355 |
} catch(IOException ex) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
356 |
/* |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
357 |
* If releaser close() throws IOException |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
358 |
* add other exceptions as suppressed. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
359 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
360 |
if (ioe != null) |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
361 |
ex.addSuppressed(ioe); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
362 |
ioe = ex; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
363 |
} finally { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
364 |
if (ioe != null) |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
365 |
throw ioe; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
366 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
367 |
} |
2 | 368 |
} |
369 |
} |