Home | History | Annotate | Download | only in io
      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 import libcore.io.ErrnoException;
     21 import libcore.io.Libcore;
     22 import static libcore.io.OsConstants.*;
     23 
     24 /**
     25  * Wraps a Unix file descriptor. It's possible to get the file descriptor used by some
     26  * classes (such as {@link FileInputStream}, {@link FileOutputStream},
     27  * and {@link RandomAccessFile}), and then create new streams that point to the same
     28  * file descriptor.
     29  */
     30 public final class FileDescriptor {
     31 
     32     /**
     33      * Corresponds to {@code stdin}.
     34      */
     35     public static final FileDescriptor in = new FileDescriptor();
     36 
     37     /**
     38      * Corresponds to {@code stdout}.
     39      */
     40     public static final FileDescriptor out = new FileDescriptor();
     41 
     42     /**
     43      * Corresponds to {@code stderr}.
     44      */
     45     public static final FileDescriptor err = new FileDescriptor();
     46 
     47     /**
     48      * The Unix file descriptor backing this FileDescriptor.
     49      * A value of -1 indicates that this FileDescriptor is invalid.
     50      */
     51     private int descriptor = -1;
     52 
     53     static {
     54         in.descriptor = STDIN_FILENO;
     55         out.descriptor = STDOUT_FILENO;
     56         err.descriptor = STDERR_FILENO;
     57     }
     58 
     59     /**
     60      * Constructs a new invalid FileDescriptor.
     61      */
     62     public FileDescriptor() {
     63     }
     64 
     65     /**
     66      * Ensures that data which is buffered within the underlying implementation
     67      * is written out to the appropriate device before returning.
     68      */
     69     public void sync() throws SyncFailedException {
     70         try {
     71             if (Libcore.os.isatty(this)) {
     72                 Libcore.os.tcdrain(this);
     73             } else {
     74                 Libcore.os.fsync(this);
     75             }
     76         } catch (ErrnoException errnoException) {
     77             SyncFailedException sfe = new SyncFailedException(errnoException.getMessage());
     78             sfe.initCause(errnoException);
     79             throw sfe;
     80         }
     81     }
     82 
     83     /**
     84      * Tests whether this {@code FileDescriptor} is valid.
     85      */
     86     public boolean valid() {
     87         return descriptor != -1;
     88     }
     89 
     90     /**
     91      * Returns the int fd. It's highly unlikely you should be calling this. Please discuss
     92      * your needs with a libcore maintainer before using this method.
     93      * @hide internal use only
     94      */
     95     public final int getInt$() {
     96         return descriptor;
     97     }
     98 
     99     /**
    100      * Sets the int fd. It's highly unlikely you should be calling this. Please discuss
    101      * your needs with a libcore maintainer before using this method.
    102      * @hide internal use only
    103      */
    104     public final void setInt$(int fd) {
    105         this.descriptor = fd;
    106     }
    107 
    108     @Override public String toString() {
    109         return "FileDescriptor[" + descriptor + "]";
    110     }
    111 }
    112