Skip to content

Commit f897772

Browse files
committed
Internalize FileDescriptor
1 parent 9862f8f commit f897772

21 files changed

+439
-386
lines changed

src/java.base/share/classes/java/io/FileDescriptor.java

Lines changed: 29 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@
2525

2626
package java.io;
2727

28-
import java.util.ArrayList;
29-
import java.util.List;
30-
import java.util.Objects;
31-
32-
import jdk.internal.access.JavaIOFileDescriptorAccess;
33-
import jdk.internal.access.SharedSecrets;
34-
import jdk.internal.misc.Blocker;
35-
import jdk.internal.ref.PhantomCleanable;
28+
import jdk.internal.io.InternalFileDescriptor;
3629

3730
/**
3831
* Instances of the file descriptor class serve as an opaque handle
@@ -46,87 +39,19 @@
4639
* @author Pavani Diwanji
4740
* @since 1.0
4841
*/
49-
public final class FileDescriptor {
50-
51-
private int fd;
42+
@SuppressWarnings("exports") // Todo: check this
43+
public final class FileDescriptor extends InternalFileDescriptor {
5244

53-
private long handle;
54-
55-
private Closeable parent;
56-
private List<Closeable> otherParents;
57-
private boolean closed;
58-
59-
/**
60-
* true, if file is opened for appending.
61-
*/
62-
private boolean append;
63-
64-
static {
65-
initIDs();
66-
}
67-
68-
// Set up JavaIOFileDescriptorAccess in SharedSecrets
69-
static {
70-
SharedSecrets.setJavaIOFileDescriptorAccess(
71-
new JavaIOFileDescriptorAccess() {
72-
public void set(FileDescriptor fdo, int fd) {
73-
fdo.set(fd);
74-
}
75-
76-
public int get(FileDescriptor fdo) {
77-
return fdo.fd;
78-
}
79-
80-
public void setAppend(FileDescriptor fdo, boolean append) {
81-
fdo.append = append;
82-
}
83-
84-
public boolean getAppend(FileDescriptor fdo) {
85-
return fdo.append;
86-
}
87-
88-
public void close(FileDescriptor fdo) throws IOException {
89-
fdo.close();
90-
}
91-
92-
/* Register for a normal FileCleanable fd/handle cleanup. */
93-
public void registerCleanup(FileDescriptor fdo) {
94-
FileCleanable.register(fdo);
95-
}
96-
97-
/* Register a custom PhantomCleanup. */
98-
public void registerCleanup(FileDescriptor fdo,
99-
PhantomCleanable<FileDescriptor> cleanup) {
100-
fdo.registerCleanup(cleanup);
101-
}
102-
103-
public void unregisterCleanup(FileDescriptor fdo) {
104-
fdo.unregisterCleanup();
105-
}
106-
107-
public void setHandle(FileDescriptor fdo, long handle) {
108-
fdo.setHandle(handle);
109-
}
110-
111-
public long getHandle(FileDescriptor fdo) {
112-
return fdo.handle;
113-
}
114-
}
115-
);
116-
}
117-
118-
/**
119-
* Cleanup in case FileDescriptor is not explicitly closed.
120-
*/
121-
private PhantomCleanable<FileDescriptor> cleanup;
45+
/* @Stable
46+
private final InternalFileDescriptor internalFileDescriptor;*/
12247

12348
/**
12449
* Constructs an (invalid) FileDescriptor object.
12550
* The fd or handle is set later.
12651
*/
12752
public FileDescriptor() {
128-
fd = -1;
129-
handle = -1;
53+
super();
54+
//internalFileDescriptor = new InternalFileDescriptor();
13055
}
13156

13257
/**
@@ -136,9 +61,8 @@ public FileDescriptor() {
13661
* @param fd the raw fd number (0, 1, 2)
13762
*/
13863
private FileDescriptor(int fd) {
139-
this.fd = fd;
140-
this.handle = getHandle(fd);
141-
this.append = getAppend(fd);
64+
super(fd);
65+
//internalFileDescriptor = new InternalFileDescriptor(fd);
14266
}
14367

14468
/**
@@ -175,7 +99,8 @@ private FileDescriptor(int fd) {
17599
* {@code false} otherwise.
176100
*/
177101
public boolean valid() {
178-
return (handle != -1) || (fd != -1);
102+
return super.valid();
103+
//return internalFileDescriptor.valid();
179104
}
180105

181106
/**
@@ -207,89 +132,24 @@ public boolean valid() {
207132
* @since 1.1
208133
*/
209134
public void sync() throws SyncFailedException {
210-
boolean attempted = Blocker.begin();
211-
try {
212-
sync0();
213-
} finally {
214-
Blocker.end(attempted);
215-
}
135+
super.sync();
136+
//internalFileDescriptor.sync();
216137
}
217138

218-
/* fsync/equivalent this file descriptor */
219-
private native void sync0() throws SyncFailedException;
220-
221-
/* This routine initializes JNI field offsets for the class */
222-
private static native void initIDs();
223-
224139
/*
225-
* On Windows return the handle for the standard streams.
226-
*/
227-
private static native long getHandle(int d);
228-
229-
/**
230-
* Returns true, if the file was opened for appending.
231-
*/
232-
private static native boolean getAppend(int fd);
233-
234-
/**
235-
* Set the fd.
236-
* Used on Unix and for sockets on Windows and Unix.
237-
* If setting to -1, clear the cleaner.
238-
* The {@link #registerCleanup} method should be called for new fds.
239-
* @param fd the raw fd or -1 to indicate closed
240-
*/
241-
synchronized void set(int fd) {
242-
if (fd == -1 && cleanup != null) {
243-
cleanup.clear();
244-
cleanup = null;
245-
}
246-
this.fd = fd;
247-
}
248-
249-
/**
250-
* Set the handle.
251-
* Used on Windows for regular files.
252-
* If setting to -1, clear the cleaner.
253-
* The {@link #registerCleanup} method should be called for new handles.
254-
* @param handle the handle or -1 to indicate closed
255-
*/
256-
void setHandle(long handle) {
257-
if (handle == -1 && cleanup != null) {
258-
cleanup.clear();
259-
cleanup = null;
260-
}
261-
this.handle = handle;
262-
}
263-
264-
/**
265-
* Register a cleanup for the current handle.
266-
* Used directly in java.io and indirectly via fdAccess.
267-
* The cleanup should be registered after the handle is set in the FileDescriptor.
268-
* @param cleanable a PhantomCleanable to register
140+
* Package private methods to track referents.
141+
* If multiple streams point to the same FileDescriptor, we cycle
142+
* through the list of all referents and call close()
269143
*/
270-
synchronized void registerCleanup(PhantomCleanable<FileDescriptor> cleanable) {
271-
Objects.requireNonNull(cleanable, "cleanable");
272-
if (cleanup != null) {
273-
cleanup.clear();
274-
}
275-
cleanup = cleanable;
276-
}
277144

278145
/**
279-
* Unregister a cleanup for the current raw fd or handle.
280-
* Used directly in java.io and indirectly via fdAccess.
281-
* Normally {@link #close()} should be used except in cases where
282-
* it is certain the caller will close the raw fd and the cleanup
283-
* must not close the raw fd. {@link #unregisterCleanup()} must be
284-
* called before the raw fd is closed to prevent a race that makes
285-
* it possible for the fd to be reallocated to another use and later
286-
* the cleanup might be invoked.
146+
* Attach a Closeable to this FD for tracking.
147+
* parent reference is added to otherParents when
148+
* needed to make closeAll simpler.
287149
*/
288-
synchronized void unregisterCleanup() {
289-
if (cleanup != null) {
290-
cleanup.clear();
291-
}
292-
cleanup = null;
150+
void attach(Closeable c) {
151+
super.attach0(c);
152+
//internalFileDescriptor.attach(c);
293153
}
294154

295155
/**
@@ -299,39 +159,9 @@ synchronized void unregisterCleanup() {
299159
* Package private to allow it to be used in java.io.
300160
* @throws IOException if close fails
301161
*/
302-
synchronized void close() throws IOException {
303-
unregisterCleanup();
304-
close0();
305-
}
306-
307-
/*
308-
* Close the raw file descriptor or handle, if it has not already been closed
309-
* and set the fd and handle to -1.
310-
*/
311-
private native void close0() throws IOException;
312-
313-
/*
314-
* Package private methods to track referents.
315-
* If multiple streams point to the same FileDescriptor, we cycle
316-
* through the list of all referents and call close()
317-
*/
318-
319-
/**
320-
* Attach a Closeable to this FD for tracking.
321-
* parent reference is added to otherParents when
322-
* needed to make closeAll simpler.
323-
*/
324-
synchronized void attach(Closeable c) {
325-
if (parent == null) {
326-
// first caller gets to do this
327-
parent = c;
328-
} else if (otherParents == null) {
329-
otherParents = new ArrayList<>();
330-
otherParents.add(parent);
331-
otherParents.add(c);
332-
} else {
333-
otherParents.add(c);
334-
}
162+
void close() throws IOException {
163+
super.close1();
164+
//internalFileDescriptor.close();
335165
}
336166

337167
/**
@@ -340,36 +170,9 @@ synchronized void attach(Closeable c) {
340170
*
341171
* The caller closeable gets to call close0().
342172
*/
343-
synchronized void closeAll(Closeable releaser) throws IOException {
344-
if (!closed) {
345-
closed = true;
346-
IOException ioe = null;
347-
try (releaser) {
348-
if (otherParents != null) {
349-
for (Closeable referent : otherParents) {
350-
try {
351-
referent.close();
352-
} catch(IOException x) {
353-
if (ioe == null) {
354-
ioe = x;
355-
} else {
356-
ioe.addSuppressed(x);
357-
}
358-
}
359-
}
360-
}
361-
} catch(IOException ex) {
362-
/*
363-
* If releaser close() throws IOException
364-
* add other exceptions as suppressed.
365-
*/
366-
if (ioe != null)
367-
ex.addSuppressed(ioe);
368-
ioe = ex;
369-
} finally {
370-
if (ioe != null)
371-
throw ioe;
372-
}
373-
}
173+
void closeAll(Closeable releaser) throws IOException {
174+
super.closeAll0(releaser);
175+
//internalFileDescriptor.closeAll(releaser);
374176
}
177+
375178
}

src/java.base/share/classes/java/io/FileInputStream.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import java.nio.channels.FileChannel;
2929
import java.util.Arrays;
30+
31+
import jdk.internal.io.FileCleanable;
3032
import jdk.internal.util.ArraysSupport;
3133
import jdk.internal.event.FileReadEvent;
3234
import jdk.internal.vm.annotation.Stable;

src/java.base/share/classes/java/io/FileOutputStream.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
package java.io;
2727

2828
import java.nio.channels.FileChannel;
29-
import jdk.internal.access.SharedSecrets;
30-
import jdk.internal.access.JavaIOFileDescriptorAccess;
3129
import jdk.internal.event.FileWriteEvent;
30+
import jdk.internal.access.JavaIOFileDescriptorAccessUtil;
31+
import jdk.internal.io.FileCleanable;
3232
import sun.nio.ch.FileChannelImpl;
3333

3434

@@ -63,11 +63,6 @@
6363
*/
6464
public class FileOutputStream extends OutputStream
6565
{
66-
/**
67-
* Access to FileDescriptor internals.
68-
*/
69-
private static final JavaIOFileDescriptorAccess FD_ACCESS =
70-
SharedSecrets.getJavaIOFileDescriptorAccess();
7166

7267
/**
7368
* Flag set by jdk.internal.event.JFRTracing to indicate if
@@ -284,7 +279,7 @@ private void traceWrite(int b, boolean append) throws IOException {
284279
*/
285280
@Override
286281
public void write(int b) throws IOException {
287-
boolean append = FD_ACCESS.getAppend(fd);
282+
boolean append = JavaIOFileDescriptorAccessUtil.getAppend(fd);
288283
if (jfrTracing && FileWriteEvent.enabled()) {
289284
traceWrite(b, append);
290285
return;
@@ -324,7 +319,7 @@ private void traceWriteBytes(byte b[], int off, int len, boolean append) throws
324319
*/
325320
@Override
326321
public void write(byte[] b) throws IOException {
327-
boolean append = FD_ACCESS.getAppend(fd);
322+
boolean append = JavaIOFileDescriptorAccessUtil.getAppend(fd);
328323
if (jfrTracing && FileWriteEvent.enabled()) {
329324
traceWriteBytes(b, 0, b.length, append);
330325
return;
@@ -344,7 +339,7 @@ public void write(byte[] b) throws IOException {
344339
*/
345340
@Override
346341
public void write(byte[] b, int off, int len) throws IOException {
347-
boolean append = FD_ACCESS.getAppend(fd);
342+
boolean append = JavaIOFileDescriptorAccessUtil.getAppend(fd);
348343
if (jfrTracing && FileWriteEvent.enabled()) {
349344
traceWriteBytes(b, off, len, append);
350345
return;

src/java.base/share/classes/java/io/RandomAccessFile.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import jdk.internal.access.JavaIORandomAccessFileAccess;
3131
import jdk.internal.access.SharedSecrets;
32+
import jdk.internal.io.FileCleanable;
3233
import jdk.internal.misc.Blocker;
3334
import jdk.internal.util.ByteArray;
3435
import jdk.internal.event.FileReadEvent;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package jdk.internal.access;
2+
3+
import jdk.internal.io.InternalFileDescriptor;
4+
5+
public final class JavaIOFileDescriptorAccessUtil extends InternalFileDescriptor.JavaIOFileDescriptorAccessUtilImpl {}

0 commit comments

Comments
 (0)