Home | History | Annotate | Download | only in io
      1 /*
      2  * Copyright (c) 1995, 2011, 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 // Android-changed: Removed parent reference counting. Creator is responsible for closing
     50 // the file descriptor.
     51 public final class FileDescriptor {
     52 
     53     // Android-changed: Renamed fd to descriptor to avoid issues with JNI/reflection
     54     // fetching the descriptor value.
     55     private int descriptor;
     56 
     57     /**
     58      * Constructs an (invalid) FileDescriptor
     59      * object.
     60      */
     61     public /**/ FileDescriptor() {
     62         descriptor = -1;
     63     }
     64 
     65     private /* */ FileDescriptor(int descriptor) {
     66         this.descriptor = descriptor;
     67     }
     68 
     69     /**
     70      * A handle to the standard input stream. Usually, this file
     71      * descriptor is not used directly, but rather via the input stream
     72      * known as <code>System.in</code>.
     73      *
     74      * @see     java.lang.System#in
     75      */
     76     // Android-changed: Duplicates of FDs needed for RuntimeInit#redirectLogStreams
     77     public static final FileDescriptor in = dupFd(0);
     78 
     79     /**
     80      * A handle to the standard output stream. Usually, this file
     81      * descriptor is not used directly, but rather via the output stream
     82      * known as <code>System.out</code>.
     83      * @see     java.lang.System#out
     84      */
     85     // Android-changed: Duplicates of FDs needed for RuntimeInit#redirectLogStreams
     86     public static final FileDescriptor out = dupFd(1);
     87 
     88     /**
     89      * A handle to the standard error stream. Usually, this file
     90      * descriptor is not used directly, but rather via the output stream
     91      * known as <code>System.err</code>.
     92      *
     93      * @see     java.lang.System#err
     94      */
     95     // Android-changed: Duplicates of FDs needed for RuntimeInit#redirectLogStreams
     96     public static final FileDescriptor err = dupFd(2);
     97 
     98     /**
     99      * Tests if this file descriptor object is valid.
    100      *
    101      * @return  <code>true</code> if the file descriptor object represents a
    102      *          valid, open file, socket, or other active I/O connection;
    103      *          <code>false</code> otherwise.
    104      */
    105     public boolean valid() {
    106         return descriptor != -1;
    107     }
    108 
    109     /**
    110      * Force all system buffers to synchronize with the underlying
    111      * device.  This method returns after all modified data and
    112      * attributes of this FileDescriptor have been written to the
    113      * relevant device(s).  In particular, if this FileDescriptor
    114      * refers to a physical storage medium, such as a file in a file
    115      * system, sync will not return until all in-memory modified copies
    116      * of buffers associated with this FileDescriptor have been
    117      * written to the physical medium.
    118      *
    119      * sync is meant to be used by code that requires physical
    120      * storage (such as a file) to be in a known state  For
    121      * example, a class that provided a simple transaction facility
    122      * might use sync to ensure that all changes to a file caused
    123      * by a given transaction were recorded on a storage medium.
    124      *
    125      * sync only affects buffers downstream of this FileDescriptor.  If
    126      * any in-memory buffering is being done by the application (for
    127      * example, by a BufferedOutputStream object), those buffers must
    128      * be flushed into the FileDescriptor (for example, by invoking
    129      * OutputStream.flush) before that data will be affected by sync.
    130      *
    131      * @exception SyncFailedException
    132      *        Thrown when the buffers cannot be flushed,
    133      *        or because the system cannot guarantee that all the
    134      *        buffers have been synchronized with physical media.
    135      * @since     JDK1.1
    136      */
    137     public native void sync() throws SyncFailedException;
    138 
    139     // Android-removed: initIDs not used to allow compile-time intialization
    140     /* This routine initializes JNI field offsets for the class */
    141     //private static native void initIDs();
    142 
    143     /**
    144      * Returns the int descriptor. It's highly unlikely you should be calling this. Please discuss
    145      * your needs with a libcore maintainer before using this method.
    146      * @hide internal use only
    147      */
    148     // Android-added: Needed for framework to access descriptor value
    149     public final int getInt$() {
    150         return descriptor;
    151     }
    152 
    153     /**
    154      * Sets the int descriptor. It's highly unlikely you should be calling this. Please discuss
    155      * your needs with a libcore maintainer before using this method.
    156      * @hide internal use only
    157      */
    158     // Android-added: Needed for framework to access descriptor value
    159     public final void setInt$(int fd) {
    160         this.descriptor = fd;
    161     }
    162 
    163     /**
    164      * @hide internal use only
    165      */
    166     // Android-added: Needed for framework to test if it's a socket
    167     public boolean isSocket$() {
    168         return isSocket(descriptor);
    169     }
    170 
    171     // Android-added: Needed for RuntimeInit#redirectLogStreams.
    172     private static FileDescriptor dupFd(int fd) {
    173         try {
    174             return new FileDescriptor(Os.fcntlInt(new FileDescriptor(fd), F_DUPFD_CLOEXEC, 0));
    175         } catch (ErrnoException e) {
    176             throw new RuntimeException(e);
    177         }
    178     }
    179 
    180     private static native boolean isSocket(int descriptor);
    181     // Set up JavaIOFileDescriptorAccess in SharedSecrets
    182     static {
    183         sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
    184             new sun.misc.JavaIOFileDescriptorAccess() {
    185                 public void set(FileDescriptor obj, int fd) {
    186                     obj.descriptor = fd;
    187                 }
    188 
    189                 public int get(FileDescriptor obj) {
    190                     return obj.descriptor;
    191                 }
    192 
    193                 public void setHandle(FileDescriptor obj, long handle) {
    194                     throw new UnsupportedOperationException();
    195                 }
    196 
    197                 public long getHandle(FileDescriptor obj) {
    198                     throw new UnsupportedOperationException();
    199                 }
    200             }
    201         );
    202     }
    203 // Android-removed: Removed method required for parents reference counting
    204 }
    205