• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.io;
19 
20 /**
21  * The lowest-level representation of a file, device, or
22  * socket. If is often used for wrapping an operating system "handle". Some
23  * I/O classes can be queried for the FileDescriptor they are operating on, and
24  * this descriptor can subsequently be used during the instantiation of another
25  * I/O class, so that the new object will reuse it.
26  * <p>
27  * The FileDescriptor class also contains static fields representing the
28  * system's standard input, output and error streams. These can be used directly
29  * if desired, but it is recommended to go through System.in, System.out, and
30  * System.err streams respectively.
31  * <p>
32  * Applications should not create new FileDescriptors.
33  *
34  * @see FileInputStream#getFD()
35  * @see FileOutputStream#getFD()
36  * @see RandomAccessFile#getFD()
37  */
38 public final class FileDescriptor {
39 
40     /**
41      * The FileDescriptor representing standard input.
42      */
43     public static final FileDescriptor in = new FileDescriptor();
44 
45     /**
46      * FileDescriptor representing standard out.
47      */
48     public static final FileDescriptor out = new FileDescriptor();
49 
50     /**
51      * FileDescriptor representing standard error.
52      */
53     public static final FileDescriptor err = new FileDescriptor();
54 
55     // BEGIN android-changed
56     // file descriptors are only int on android
57     /**
58      * Represents a link to any underlying OS resources for this FileDescriptor.
59      * A value of -1 indicates that this FileDescriptor is invalid.
60      */
61     int descriptor = -1;
62     // END android-changed
63 
64     boolean readOnly = false;
65 
oneTimeInitialization()66     private static native void oneTimeInitialization();
67 
68     static {
69         in.descriptor = 0;
70         out.descriptor = 1;
71         err.descriptor = 2;
72 
oneTimeInitialization()73         oneTimeInitialization();
74     }
75 
76     /**
77      * Constructs a new FileDescriptor containing an invalid handle. The
78      * contained handle is usually modified by native code at a later point.
79      */
FileDescriptor()80     public FileDescriptor() {
81         super();
82     }
83 
84     /**
85      * Ensures that data which is buffered within the underlying implementation
86      * is written out to the appropriate device before returning.
87      *
88      * @throws SyncFailedException
89      *             when the operation fails.
90      */
sync()91     public void sync() throws SyncFailedException {
92         // if the descriptor is a read-only one, do nothing
93         if (!readOnly) {
94             syncImpl();
95         }
96     }
97 
syncImpl()98     private native void syncImpl() throws SyncFailedException;
99 
100     /**
101      * Indicates whether this FileDescriptor is valid.
102      *
103      * @return {@code true} if this FileDescriptor is valid, {@code false}
104      *         otherwise.
105      */
valid()106     public boolean valid() {
107         return descriptor != -1;
108     }
109 }
110