KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > FileDescriptor


1 /*
2  * @(#)FileDescriptor.java 1.3 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * Instances of the file descriptor class serve as an opaque handle
12  * to the underlying machine-specific structure representing an open
13  * file, an open socket, or another source or sink of bytes. The
14  * main practical use for a file descriptor is to create a
15  * <code>FileInputStream</code> or <code>FileOutputStream</code> to
16  * contain it.
17  * <p>
18  * Applications should not create their own file descriptors.
19  *
20  * @author Pavani Diwanji
21  * @version 1.3, 12/19/03
22  * @see java.io.FileInputStream
23  * @see java.io.FileOutputStream
24  * @since JDK1.0
25  */

26 public final class FileDescriptor {
27  
28     private int fd;
29
30     private long handle;
31
32     /**
33      * Constructs an (invalid) FileDescriptor
34      * object.
35      */

36     public /**/ FileDescriptor() {
37     fd = -1;
38         handle = -1;
39     }
40
41     private /* */ FileDescriptor(int fd) {
42     this.fd = fd;
43         handle = -1;
44     }
45
46     static {
47         initIDs();
48     }
49
50     /**
51      * A handle to the standard input stream. Usually, this file
52      * descriptor is not used directly, but rather via the input stream
53      * known as <code>System.in</code>.
54      *
55      * @see java.lang.System#in
56      */

57     public static final FileDescriptor JavaDoc in = standardStream(0);
58
59     /**
60      * A handle to the standard output stream. Usually, this file
61      * descriptor is not used directly, but rather via the output stream
62      * known as <code>System.out</code>.
63      * @see java.lang.System#out
64      */

65     public static final FileDescriptor JavaDoc out = standardStream(1);
66
67     /**
68      * A handle to the standard error stream. Usually, this file
69      * descriptor is not used directly, but rather via the output stream
70      * known as <code>System.err</code>.
71      *
72      * @see java.lang.System#err
73      */

74     public static final FileDescriptor JavaDoc err = standardStream(2);
75
76     /**
77      * Tests if this file descriptor object is valid.
78      *
79      * @return <code>true</code> if the file descriptor object represents a
80      * valid, open file, socket, or other active I/O connection;
81      * <code>false</code> otherwise.
82      */

83     public boolean valid() {
84     return ((handle != -1) || (fd != -1));
85     }
86
87     /**
88      * Force all system buffers to synchronize with the underlying
89      * device. This method returns after all modified data and
90      * attributes of this FileDescriptor have been written to the
91      * relevant device(s). In particular, if this FileDescriptor
92      * refers to a physical storage medium, such as a file in a file
93      * system, sync will not return until all in-memory modified copies
94      * of buffers associated with this FileDesecriptor have been
95      * written to the physical medium.
96      *
97      * sync is meant to be used by code that requires physical
98      * storage (such as a file) to be in a known state For
99      * example, a class that provided a simple transaction facility
100      * might use sync to ensure that all changes to a file caused
101      * by a given transaction were recorded on a storage medium.
102      *
103      * sync only affects buffers downstream of this FileDescriptor. If
104      * any in-memory buffering is being done by the application (for
105      * example, by a BufferedOutputStream object), those buffers must
106      * be flushed into the FileDescriptor (for example, by invoking
107      * OutputStream.flush) before that data will be affected by sync.
108      *
109      * @exception SyncFailedException
110      * Thrown when the buffers cannot be flushed,
111      * or because the system cannot guarantee that all the
112      * buffers have been synchronized with physical media.
113      * @since JDK1.1
114      */

115     public native void sync() throws SyncFailedException JavaDoc;
116
117     /* This routine initializes JNI field offsets for the class */
118     private static native void initIDs();
119
120     private static native long set(int d);
121
122     private static FileDescriptor JavaDoc standardStream(int fd) {
123         FileDescriptor JavaDoc desc = new FileDescriptor JavaDoc();
124         desc.handle = set(fd);
125         return desc;
126     }
127
128 }
129
Popular Tags