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 */ FileDescriptor()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 */ sync()69 public void sync() throws SyncFailedException { 70 try { 71 Libcore.os.fsync(this); 72 } catch (ErrnoException errnoException) { 73 SyncFailedException sfe = new SyncFailedException(errnoException.getMessage()); 74 sfe.initCause(errnoException); 75 throw sfe; 76 } 77 } 78 79 /** 80 * Tests whether this {@code FileDescriptor} is valid. 81 */ valid()82 public boolean valid() { 83 return descriptor != -1; 84 } 85 86 /** 87 * Returns the int fd. It's highly unlikely you should be calling this. Please discuss 88 * your needs with a libcore maintainer before using this method. 89 * @hide internal use only 90 */ getInt$()91 public final int getInt$() { 92 return descriptor; 93 } 94 95 /** 96 * Sets the int fd. It's highly unlikely you should be calling this. Please discuss 97 * your needs with a libcore maintainer before using this method. 98 * @hide internal use only 99 */ setInt$(int fd)100 public final void setInt$(int fd) { 101 this.descriptor = fd; 102 } 103 toString()104 @Override public String toString() { 105 return "FileDescriptor[" + descriptor + "]"; 106 } 107 } 108