author | rriggs |
Fri, 02 Feb 2018 14:17:07 -0500 | |
changeset 48737 | 7c12219870fd |
parent 48224 | be0df5ab3093 |
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) 1995, 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 |
||
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
28 |
import java.lang.ref.Cleaner; |
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
29 |
import java.util.ArrayList; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
30 |
import java.util.List; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
31 |
|
32834
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
28425
diff
changeset
|
32 |
import jdk.internal.misc.JavaIOFileDescriptorAccess; |
e1dca5fe4de3
8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents:
28425
diff
changeset
|
33 |
import jdk.internal.misc.SharedSecrets; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
34 |
import jdk.internal.ref.CleanerFactory; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
35 |
import jdk.internal.ref.PhantomCleanable; |
2 | 36 |
|
37 |
/** |
|
38 |
* Instances of the file descriptor class serve as an opaque handle |
|
39 |
* to the underlying machine-specific structure representing an open |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
40 |
* file, an open socket, or another source or sink of bytes. |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
41 |
* The main practical use for a file descriptor is to create a |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
42 |
* {@link FileInputStream} or {@link FileOutputStream} to contain it. |
2 | 43 |
* <p> |
44 |
* Applications should not create their own file descriptors. |
|
45 |
* |
|
46 |
* @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
|
47 |
* @since 1.0 |
2 | 48 |
*/ |
49 |
public final class FileDescriptor { |
|
50 |
||
51 |
private int fd; |
|
52 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
53 |
private Closeable parent; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
54 |
private List<Closeable> otherParents; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
55 |
private boolean closed; |
2 | 56 |
|
57 |
/** |
|
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
58 |
* true, if file is opened for appending. |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
59 |
*/ |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
60 |
private boolean append; |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
61 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
62 |
static { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
63 |
initIDs(); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
64 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
65 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
66 |
// Set up JavaIOFileDescriptorAccess in SharedSecrets |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
67 |
static { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
68 |
SharedSecrets.setJavaIOFileDescriptorAccess( |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
69 |
new JavaIOFileDescriptorAccess() { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
70 |
public void set(FileDescriptor fdo, int fd) { |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
71 |
fdo.set(fd); |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
72 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
73 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
74 |
public int get(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
75 |
return fdo.fd; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
76 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
77 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
78 |
public void setAppend(FileDescriptor fdo, boolean append) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
79 |
fdo.append = append; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
80 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
81 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
82 |
public boolean getAppend(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
83 |
return fdo.append; |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
84 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
85 |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
86 |
public void close(FileDescriptor fdo) throws IOException { |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
87 |
fdo.close(); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
88 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
89 |
|
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
90 |
public void registerCleanup(FileDescriptor fdo) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
91 |
fdo.registerCleanup(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
92 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
93 |
|
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
94 |
public void registerCleanup(FileDescriptor fdo, PhantomCleanable<Object> cleanup) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
95 |
fdo.registerCleanup(cleanup); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
96 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
97 |
|
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
98 |
public void unregisterCleanup(FileDescriptor fdo) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
99 |
fdo.unregisterCleanup(); |
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 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
102 |
public void setHandle(FileDescriptor fdo, long handle) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
103 |
throw new UnsupportedOperationException(); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
104 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
105 |
|
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
106 |
public long getHandle(FileDescriptor fdo) { |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
107 |
throw new UnsupportedOperationException(); |
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 |
); |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
111 |
} |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
112 |
|
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
113 |
/** |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
114 |
* Cleanup in case FileDescriptor is not explicitly closed. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
115 |
*/ |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
116 |
private PhantomCleanable<Object> cleanup; |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
117 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
118 |
/** |
2 | 119 |
* Constructs an (invalid) FileDescriptor |
120 |
* object. |
|
121 |
*/ |
|
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
122 |
public FileDescriptor() { |
2 | 123 |
fd = -1; |
124 |
} |
|
125 |
||
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
126 |
private FileDescriptor(int fd) { |
2 | 127 |
this.fd = fd; |
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
128 |
this.append = getAppend(fd); |
2 | 129 |
} |
130 |
||
131 |
/** |
|
132 |
* A handle to the standard input stream. Usually, this file |
|
133 |
* descriptor is not used directly, but rather via the input stream |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
134 |
* known as {@code System.in}. |
2 | 135 |
* |
136 |
* @see java.lang.System#in |
|
137 |
*/ |
|
138 |
public static final FileDescriptor in = new FileDescriptor(0); |
|
139 |
||
140 |
/** |
|
141 |
* A handle to the standard output stream. Usually, this file |
|
142 |
* descriptor is not used directly, but rather via the output stream |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
143 |
* known as {@code System.out}. |
2 | 144 |
* @see java.lang.System#out |
145 |
*/ |
|
146 |
public static final FileDescriptor out = new FileDescriptor(1); |
|
147 |
||
148 |
/** |
|
149 |
* A handle to the standard error stream. Usually, this file |
|
150 |
* descriptor is not used directly, but rather via the output stream |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
151 |
* known as {@code System.err}. |
2 | 152 |
* |
153 |
* @see java.lang.System#err |
|
154 |
*/ |
|
155 |
public static final FileDescriptor err = new FileDescriptor(2); |
|
156 |
||
157 |
/** |
|
158 |
* Tests if this file descriptor object is valid. |
|
159 |
* |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
160 |
* @return {@code true} if the file descriptor object represents a |
2 | 161 |
* valid, open file, socket, or other active I/O connection; |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
162 |
* {@code false} otherwise. |
2 | 163 |
*/ |
164 |
public boolean valid() { |
|
165 |
return fd != -1; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Force all system buffers to synchronize with the underlying |
|
170 |
* device. This method returns after all modified data and |
|
171 |
* attributes of this FileDescriptor have been written to the |
|
172 |
* relevant device(s). In particular, if this FileDescriptor |
|
173 |
* refers to a physical storage medium, such as a file in a file |
|
174 |
* system, sync will not return until all in-memory modified copies |
|
175 |
* of buffers associated with this FileDescriptor have been |
|
176 |
* written to the physical medium. |
|
177 |
* |
|
178 |
* sync is meant to be used by code that requires physical |
|
179 |
* storage (such as a file) to be in a known state For |
|
180 |
* example, a class that provided a simple transaction facility |
|
181 |
* might use sync to ensure that all changes to a file caused |
|
182 |
* by a given transaction were recorded on a storage medium. |
|
183 |
* |
|
184 |
* sync only affects buffers downstream of this FileDescriptor. If |
|
185 |
* any in-memory buffering is being done by the application (for |
|
186 |
* example, by a BufferedOutputStream object), those buffers must |
|
187 |
* be flushed into the FileDescriptor (for example, by invoking |
|
188 |
* OutputStream.flush) before that data will be affected by sync. |
|
189 |
* |
|
190 |
* @exception SyncFailedException |
|
191 |
* Thrown when the buffers cannot be flushed, |
|
192 |
* or because the system cannot guarantee that all the |
|
193 |
* 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
|
194 |
* @since 1.1 |
2 | 195 |
*/ |
196 |
public native void sync() throws SyncFailedException; |
|
197 |
||
198 |
/* This routine initializes JNI field offsets for the class */ |
|
199 |
private static native void initIDs(); |
|
200 |
||
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
201 |
/** |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
202 |
* Set the fd. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
203 |
* If setting to -1, clear the cleaner. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
204 |
* The {@link #registerCleanup()} method should be called for new fds. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
205 |
* @param fd the fd or -1 to indicate closed |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
206 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
207 |
@SuppressWarnings("unchecked") |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
208 |
synchronized void set(int fd) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
209 |
if (fd == -1 && cleanup != null) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
210 |
cleanup.clear(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
211 |
cleanup = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
212 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
213 |
this.fd = fd; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
214 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
215 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
216 |
/** |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
217 |
* Register a cleanup for the current handle. |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
218 |
* Used directly in java.io and indirectly via fdAccess. |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
219 |
* The cleanup should be registered after the handle is set in the FileDescriptor. |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
220 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
221 |
@SuppressWarnings("unchecked") |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
222 |
void registerCleanup() { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
223 |
registerCleanup(null); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
224 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
225 |
|
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
226 |
/** |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
227 |
* Register a cleanup for the current handle. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
228 |
* 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
|
229 |
* The cleanup should be registered after the handle is set in the FileDescriptor. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
230 |
* @param newCleanable a PhantomCleanable to register |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
231 |
*/ |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
232 |
@SuppressWarnings("unchecked") |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
233 |
synchronized void registerCleanup(PhantomCleanable<Object> newCleanable) { |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
234 |
if (cleanup != null) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
235 |
cleanup.clear(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
236 |
} |
48737
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
237 |
cleanup = (newCleanable == null) ? FDCleanup.create(this) : newCleanable; |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
238 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
239 |
|
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
240 |
/** |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
241 |
* Unregister a cleanup for the current raw fd. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
242 |
* 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
|
243 |
* 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
|
244 |
* 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
|
245 |
* 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
|
246 |
* 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
|
247 |
* 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
|
248 |
* the cleanup might be invoked. |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
249 |
*/ |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
250 |
synchronized void unregisterCleanup() { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
251 |
if (cleanup != null) { |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
252 |
cleanup.clear(); |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
253 |
} |
7c12219870fd
8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
48224
diff
changeset
|
254 |
cleanup = null; |
48224
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 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
257 |
/** |
27263
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
258 |
* Returns true, if the file was opened for appending. |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
259 |
*/ |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
260 |
private static native boolean getAppend(int fd); |
819f5f87d485
8023173: FileDescriptor should respect append flag
igerasim
parents:
25859
diff
changeset
|
261 |
|
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
262 |
/** |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
263 |
* Close the raw file descriptor or handle, if it has not already been closed |
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
264 |
* and set the fd and handle to -1. |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
265 |
* Clear the cleaner so the close does not happen twice. |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
266 |
* 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
|
267 |
* @throws IOException if close fails |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
268 |
*/ |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
269 |
@SuppressWarnings("unchecked") |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
270 |
synchronized void close() throws IOException { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
271 |
if (cleanup != null) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
272 |
cleanup.clear(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
273 |
cleanup = null; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
274 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
275 |
close0(); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
276 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
277 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
278 |
/* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
279 |
* 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
|
280 |
* and set the fd and handle to -1. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
281 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
282 |
private native void close0() throws IOException; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
283 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
284 |
/* |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
285 |
* Raw close of the file descriptor. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
286 |
* Used only for last chance cleanup. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
287 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
288 |
private static native void cleanupClose0(int fd) throws IOException; |
47235
9ef10c6e67b8
8187631: Refactor FileDescriptor close implementation
rriggs
parents:
47216
diff
changeset
|
289 |
|
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
290 |
/* |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
291 |
* Package private methods to track referents. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
292 |
* If multiple streams point to the same FileDescriptor, we cycle |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
293 |
* through the list of all referents and call close() |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
294 |
*/ |
2 | 295 |
|
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
296 |
/** |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
297 |
* Attach a Closeable to this FD for tracking. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
298 |
* parent reference is added to otherParents when |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
299 |
* needed to make closeAll simpler. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
300 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
301 |
synchronized void attach(Closeable c) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
302 |
if (parent == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
303 |
// first caller gets to do this |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
304 |
parent = c; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
305 |
} else if (otherParents == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
306 |
otherParents = new ArrayList<>(); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
307 |
otherParents.add(parent); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
308 |
otherParents.add(c); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
309 |
} else { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
310 |
otherParents.add(c); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
311 |
} |
2 | 312 |
} |
313 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
314 |
/** |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
315 |
* Cycle through all Closeables sharing this FD and call |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
316 |
* close() on each one. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
317 |
* |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
318 |
* The caller closeable gets to call close0(). |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
319 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
320 |
@SuppressWarnings("try") |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
321 |
synchronized void closeAll(Closeable releaser) throws IOException { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
322 |
if (!closed) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
323 |
closed = true; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
324 |
IOException ioe = null; |
28425
241dd1e2d9cb
8068948: Update java.base module to use new try-with-resources statement
darcy
parents:
27263
diff
changeset
|
325 |
try (releaser) { |
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
326 |
if (otherParents != null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
327 |
for (Closeable referent : otherParents) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
328 |
try { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
329 |
referent.close(); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
330 |
} catch(IOException x) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
331 |
if (ioe == null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
332 |
ioe = x; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
333 |
} else { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
334 |
ioe.addSuppressed(x); |
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 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
337 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
338 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
339 |
} catch(IOException ex) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
340 |
/* |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
341 |
* If releaser close() throws IOException |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
342 |
* add other exceptions as suppressed. |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
343 |
*/ |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
344 |
if (ioe != null) |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
345 |
ex.addSuppressed(ioe); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
346 |
ioe = ex; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
347 |
} finally { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
348 |
if (ioe != null) |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
349 |
throw ioe; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
350 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
5506
diff
changeset
|
351 |
} |
2 | 352 |
} |
48224
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
353 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
354 |
/** |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
355 |
* Cleanup for a FileDescriptor when it becomes phantom reachable. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
356 |
* Create a cleanup if fd != -1. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
357 |
* Subclassed from {@code PhantomCleanable} so that {@code clear} can be |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
358 |
* called to disable the cleanup when the fd is closed by any means other |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
359 |
* than calling {@link FileDescriptor#close}. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
360 |
* Otherwise, it may close the native fd after it has been reused. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
361 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
362 |
static final class FDCleanup extends PhantomCleanable<Object> { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
363 |
private final int fd; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
364 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
365 |
static FDCleanup create(FileDescriptor fdo) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
366 |
return fdo.fd == -1 |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
367 |
? null |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
368 |
: new FDCleanup(fdo, CleanerFactory.cleaner(), fdo.fd); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
369 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
370 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
371 |
/** |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
372 |
* Constructor for a phantom cleanable reference. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
373 |
* @param obj the object to monitor |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
374 |
* @param cleaner the cleaner |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
375 |
* @param fd file descriptor to close |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
376 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
377 |
private FDCleanup(Object obj, Cleaner cleaner, int fd) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
378 |
super(obj, cleaner); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
379 |
this.fd = fd; |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
380 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
381 |
|
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
382 |
/** |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
383 |
* Close the native fd. |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
384 |
*/ |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
385 |
@Override |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
386 |
protected void performCleanup() { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
387 |
try { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
388 |
cleanupClose0(fd); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
389 |
} catch (IOException ioe) { |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
390 |
throw new UncheckedIOException("close", ioe); |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
391 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
392 |
} |
be0df5ab3093
8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents:
47235
diff
changeset
|
393 |
} |
2 | 394 |
} |