• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
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
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
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  *
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.
24  */
25 
26 package java.io;
27 
28 import android.system.ErrnoException;
29 import android.system.Os;
30 import static android.system.OsConstants.F_DUPFD_CLOEXEC;
31 
32 import java.util.concurrent.atomic.AtomicInteger;
33 
34 /**
35  * Instances of the file descriptor class serve as an opaque handle
36  * to the underlying machine-specific structure representing an open
37  * file, an open socket, or another source or sink of bytes. The
38  * main practical use for a file descriptor is to create a
39  * <code>FileInputStream</code> or <code>FileOutputStream</code> to
40  * contain it.
41  * <p>
42  * Applications should not create their own file descriptors.
43  *
44  * @author  Pavani Diwanji
45  * @see     java.io.FileInputStream
46  * @see     java.io.FileOutputStream
47  * @since   JDK1.0
48  */
49 public final class FileDescriptor {
50 
51     private int descriptor;
52 
53     /**
54      * Constructs an (invalid) FileDescriptor
55      * object.
56      */
FileDescriptor()57     public FileDescriptor() {
58         descriptor = -1;
59     }
60 
FileDescriptor(int descriptor)61     private FileDescriptor(int descriptor) {
62         this.descriptor = descriptor;
63     }
64 
65     /**
66      * A handle to the standard input stream. Usually, this file
67      * descriptor is not used directly, but rather via the input stream
68      * known as <code>System.in</code>.
69      *
70      * @see     java.lang.System#in
71      */
72     public static final FileDescriptor in = dupFd(0);
73 
74     /**
75      * A handle to the standard output stream. Usually, this file
76      * descriptor is not used directly, but rather via the output stream
77      * known as <code>System.out</code>.
78      * @see     java.lang.System#out
79      */
80     public static final FileDescriptor out = dupFd(1);
81 
82     /**
83      * A handle to the standard error stream. Usually, this file
84      * descriptor is not used directly, but rather via the output stream
85      * known as <code>System.err</code>.
86      *
87      * @see     java.lang.System#err
88      */
89     public static final FileDescriptor err = dupFd(2);
90 
91     /**
92      * Tests if this file descriptor object is valid.
93      *
94      * @return  <code>true</code> if the file descriptor object represents a
95      *          valid, open file, socket, or other active I/O connection;
96      *          <code>false</code> otherwise.
97      */
valid()98     public boolean valid() {
99         return descriptor != -1;
100     }
101 
102     /**
103      * Force all system buffers to synchronize with the underlying
104      * device.  This method returns after all modified data and
105      * attributes of this FileDescriptor have been written to the
106      * relevant device(s).  In particular, if this FileDescriptor
107      * refers to a physical storage medium, such as a file in a file
108      * system, sync will not return until all in-memory modified copies
109      * of buffers associated with this FileDescriptor have been
110      * written to the physical medium.
111      *
112      * sync is meant to be used by code that requires physical
113      * storage (such as a file) to be in a known state  For
114      * example, a class that provided a simple transaction facility
115      * might use sync to ensure that all changes to a file caused
116      * by a given transaction were recorded on a storage medium.
117      *
118      * sync only affects buffers downstream of this FileDescriptor.  If
119      * any in-memory buffering is being done by the application (for
120      * example, by a BufferedOutputStream object), those buffers must
121      * be flushed into the FileDescriptor (for example, by invoking
122      * OutputStream.flush) before that data will be affected by sync.
123      *
124      * @exception SyncFailedException
125      *        Thrown when the buffers cannot be flushed,
126      *        or because the system cannot guarantee that all the
127      *        buffers have been synchronized with physical media.
128      * @since     JDK1.1
129      */
sync()130     public native void sync() throws SyncFailedException;
131 
132     /**
133      * Returns the int descriptor. It's highly unlikely you should be calling this. Please discuss
134      * your needs with a libcore maintainer before using this method.
135      * @hide internal use only
136      */
137     // Android-added.
getInt$()138     public final int getInt$() {
139         return descriptor;
140     }
141 
142     /**
143      * Sets the int descriptor. It's highly unlikely you should be calling this. Please discuss
144      * your needs with a libcore maintainer before using this method.
145      * @hide internal use only
146      */
147     // Android-added.
setInt$(int fd)148     public final void setInt$(int fd) {
149         this.descriptor = fd;
150     }
151 
152     /**
153      * @hide internal use only
154      */
155     // Android-added.
isSocket$()156     public boolean isSocket$() {
157         return isSocket(descriptor);
158     }
159 
160     // Android-added.
dupFd(int fd)161     private static FileDescriptor dupFd(int fd) {
162         try {
163             return new FileDescriptor(Os.fcntlInt(new FileDescriptor(fd), F_DUPFD_CLOEXEC, 0));
164         } catch (ErrnoException e) {
165             throw new RuntimeException(e);
166         }
167     }
168 
isSocket(int descriptor)169     private static native boolean isSocket(int descriptor);
170 
171 }
172