jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
*
|
jtulach@1334
|
6 |
*
|
jtulach@1334
|
7 |
*
|
jtulach@1334
|
8 |
*
|
jtulach@1334
|
9 |
*
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
*
|
jtulach@1334
|
12 |
*
|
jtulach@1334
|
13 |
*
|
jtulach@1334
|
14 |
*
|
jtulach@1334
|
15 |
*
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
*
|
jtulach@1334
|
18 |
*
|
jtulach@1334
|
19 |
*
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
*
|
jtulach@1334
|
22 |
*
|
jtulach@1334
|
23 |
*
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
package java.io;
|
jtulach@1334
|
27 |
|
jtulach@1334
|
28 |
import java.util.concurrent.atomic.AtomicInteger;
|
jtulach@1334
|
29 |
|
jtulach@1334
|
30 |
/**
|
jtulach@1334
|
31 |
* Instances of the file descriptor class serve as an opaque handle
|
jtulach@1334
|
32 |
* to the underlying machine-specific structure representing an open
|
jtulach@1334
|
33 |
* file, an open socket, or another source or sink of bytes. The
|
jtulach@1334
|
34 |
* main practical use for a file descriptor is to create a
|
jtulach@1334
|
35 |
* <code>FileInputStream</code> or <code>FileOutputStream</code> to
|
jtulach@1334
|
36 |
* contain it.
|
jtulach@1334
|
37 |
* <p>
|
jtulach@1334
|
38 |
* Applications should not create their own file descriptors.
|
jtulach@1334
|
39 |
*
|
jtulach@1334
|
40 |
* @author Pavani Diwanji
|
jtulach@1334
|
41 |
* @see java.io.FileInputStream
|
jtulach@1334
|
42 |
* @see java.io.FileOutputStream
|
jtulach@1334
|
43 |
* @since JDK1.0
|
jtulach@1334
|
44 |
*/
|
jtulach@1334
|
45 |
public final class FileDescriptor {
|
jtulach@1334
|
46 |
|
jtulach@1334
|
47 |
private int fd;
|
jtulach@1334
|
48 |
|
jtulach@1334
|
49 |
/**
|
jtulach@1334
|
50 |
* A counter for tracking the FIS/FOS/RAF instances that
|
jtulach@1334
|
51 |
* use this FileDescriptor. The FIS/FOS.finalize() will not release
|
jtulach@1334
|
52 |
* the FileDescriptor if it is still under user by a stream.
|
jtulach@1334
|
53 |
*/
|
jtulach@1334
|
54 |
private AtomicInteger useCount;
|
jtulach@1334
|
55 |
|
jtulach@1334
|
56 |
/**
|
jtulach@1334
|
57 |
* Constructs an (invalid) FileDescriptor
|
jtulach@1334
|
58 |
* object.
|
jtulach@1334
|
59 |
*/
|
jtulach@1334
|
60 |
public /**/ FileDescriptor() {
|
jtulach@1334
|
61 |
fd = -1;
|
jtulach@1334
|
62 |
useCount = new AtomicInteger();
|
jtulach@1334
|
63 |
}
|
jtulach@1334
|
64 |
|
jtulach@1334
|
65 |
private /* */ FileDescriptor(int fd) {
|
jtulach@1334
|
66 |
this.fd = fd;
|
jtulach@1334
|
67 |
useCount = new AtomicInteger();
|
jtulach@1334
|
68 |
}
|
jtulach@1334
|
69 |
|
jtulach@1334
|
70 |
/**
|
jtulach@1334
|
71 |
* A handle to the standard input stream. Usually, this file
|
jtulach@1334
|
72 |
* descriptor is not used directly, but rather via the input stream
|
jtulach@1334
|
73 |
* known as <code>System.in</code>.
|
jtulach@1334
|
74 |
*
|
jtulach@1334
|
75 |
* @see java.lang.System#in
|
jtulach@1334
|
76 |
*/
|
jtulach@1334
|
77 |
public static final FileDescriptor in = new FileDescriptor(0);
|
jtulach@1334
|
78 |
|
jtulach@1334
|
79 |
/**
|
jtulach@1334
|
80 |
* A handle to the standard output stream. Usually, this file
|
jtulach@1334
|
81 |
* descriptor is not used directly, but rather via the output stream
|
jtulach@1334
|
82 |
* known as <code>System.out</code>.
|
jtulach@1334
|
83 |
* @see java.lang.System#out
|
jtulach@1334
|
84 |
*/
|
jtulach@1334
|
85 |
public static final FileDescriptor out = new FileDescriptor(1);
|
jtulach@1334
|
86 |
|
jtulach@1334
|
87 |
/**
|
jtulach@1334
|
88 |
* A handle to the standard error stream. Usually, this file
|
jtulach@1334
|
89 |
* descriptor is not used directly, but rather via the output stream
|
jtulach@1334
|
90 |
* known as <code>System.err</code>.
|
jtulach@1334
|
91 |
*
|
jtulach@1334
|
92 |
* @see java.lang.System#err
|
jtulach@1334
|
93 |
*/
|
jtulach@1334
|
94 |
public static final FileDescriptor err = new FileDescriptor(2);
|
jtulach@1334
|
95 |
|
jtulach@1334
|
96 |
/**
|
jtulach@1334
|
97 |
* Tests if this file descriptor object is valid.
|
jtulach@1334
|
98 |
*
|
jtulach@1334
|
99 |
* @return <code>true</code> if the file descriptor object represents a
|
jtulach@1334
|
100 |
* valid, open file, socket, or other active I/O connection;
|
jtulach@1334
|
101 |
* <code>false</code> otherwise.
|
jtulach@1334
|
102 |
*/
|
jtulach@1334
|
103 |
public boolean valid() {
|
jtulach@1334
|
104 |
return fd != -1;
|
jtulach@1334
|
105 |
}
|
jtulach@1334
|
106 |
|
jtulach@1334
|
107 |
/**
|
jtulach@1334
|
108 |
* Force all system buffers to synchronize with the underlying
|
jtulach@1334
|
109 |
* device. This method returns after all modified data and
|
jtulach@1334
|
110 |
* attributes of this FileDescriptor have been written to the
|
jtulach@1334
|
111 |
* relevant device(s). In particular, if this FileDescriptor
|
jtulach@1334
|
112 |
* refers to a physical storage medium, such as a file in a file
|
jtulach@1334
|
113 |
* system, sync will not return until all in-memory modified copies
|
jtulach@1334
|
114 |
* of buffers associated with this FileDescriptor have been
|
jtulach@1334
|
115 |
* written to the physical medium.
|
jtulach@1334
|
116 |
*
|
jtulach@1334
|
117 |
* sync is meant to be used by code that requires physical
|
jtulach@1334
|
118 |
* storage (such as a file) to be in a known state For
|
jtulach@1334
|
119 |
* example, a class that provided a simple transaction facility
|
jtulach@1334
|
120 |
* might use sync to ensure that all changes to a file caused
|
jtulach@1334
|
121 |
* by a given transaction were recorded on a storage medium.
|
jtulach@1334
|
122 |
*
|
jtulach@1334
|
123 |
* sync only affects buffers downstream of this FileDescriptor. If
|
jtulach@1334
|
124 |
* any in-memory buffering is being done by the application (for
|
jtulach@1334
|
125 |
* example, by a BufferedOutputStream object), those buffers must
|
jtulach@1334
|
126 |
* be flushed into the FileDescriptor (for example, by invoking
|
jtulach@1334
|
127 |
* OutputStream.flush) before that data will be affected by sync.
|
jtulach@1334
|
128 |
*
|
jtulach@1334
|
129 |
* @exception SyncFailedException
|
jtulach@1334
|
130 |
* Thrown when the buffers cannot be flushed,
|
jtulach@1334
|
131 |
* or because the system cannot guarantee that all the
|
jtulach@1334
|
132 |
* buffers have been synchronized with physical media.
|
jtulach@1334
|
133 |
* @since JDK1.1
|
jtulach@1334
|
134 |
*/
|
jtulach@1334
|
135 |
public native void sync() throws SyncFailedException;
|
jtulach@1334
|
136 |
|
jtulach@1334
|
137 |
/* This routine initializes JNI field offsets for the class */
|
jtulach@1334
|
138 |
private static native void initIDs();
|
jtulach@1334
|
139 |
|
jtulach@1334
|
140 |
static {
|
jtulach@1334
|
141 |
initIDs();
|
jtulach@1334
|
142 |
}
|
jtulach@1334
|
143 |
|
jtulach@1334
|
144 |
// Set up JavaIOFileDescriptorAccess in SharedSecrets
|
jtulach@1334
|
145 |
static {
|
jtulach@1334
|
146 |
sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
|
jtulach@1334
|
147 |
new sun.misc.JavaIOFileDescriptorAccess() {
|
jtulach@1334
|
148 |
public void set(FileDescriptor obj, int fd) {
|
jtulach@1334
|
149 |
obj.fd = fd;
|
jtulach@1334
|
150 |
}
|
jtulach@1334
|
151 |
|
jtulach@1334
|
152 |
public int get(FileDescriptor obj) {
|
jtulach@1334
|
153 |
return obj.fd;
|
jtulach@1334
|
154 |
}
|
jtulach@1334
|
155 |
|
jtulach@1334
|
156 |
public void setHandle(FileDescriptor obj, long handle) {
|
jtulach@1334
|
157 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
158 |
}
|
jtulach@1334
|
159 |
|
jtulach@1334
|
160 |
public long getHandle(FileDescriptor obj) {
|
jtulach@1334
|
161 |
throw new UnsupportedOperationException();
|
jtulach@1334
|
162 |
}
|
jtulach@1334
|
163 |
}
|
jtulach@1334
|
164 |
);
|
jtulach@1334
|
165 |
}
|
jtulach@1334
|
166 |
|
jtulach@1334
|
167 |
// package private methods used by FIS, FOS and RAF
|
jtulach@1334
|
168 |
|
jtulach@1334
|
169 |
int incrementAndGetUseCount() {
|
jtulach@1334
|
170 |
return useCount.incrementAndGet();
|
jtulach@1334
|
171 |
}
|
jtulach@1334
|
172 |
|
jtulach@1334
|
173 |
int decrementAndGetUseCount() {
|
jtulach@1334
|
174 |
return useCount.decrementAndGet();
|
jtulach@1334
|
175 |
}
|
jtulach@1334
|
176 |
}
|