filedescriptor.java

来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 582 行 · 第 1/2 页

JAVA
582
字号
/* FileDescriptor.java -- Opaque file handle class   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004   Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version. GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.io;import gnu.classpath.Configuration;/** * This class represents an opaque file handle as a Java class.  It should * be used only to pass to other methods that expect an object of this * type.  No system specific information can be obtained from this object. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Tom Tromey (tromey@cygnus.com) * @date September 24, 1998  */public final class FileDescriptor{  /**   * A <code>FileDescriptor</code> representing the system standard input   * stream.  This will usually be accessed through the   * <code>System.in</code>variable.   */  public static final FileDescriptor in = new FileDescriptor();  /**   * A <code>FileDescriptor</code> representing the system standard output   * stream.  This will usually be accessed through the   * <code>System.out</code>variable.   */  public static final FileDescriptor out = new FileDescriptor();  /**   * A <code>FileDescriptor</code> representing the system standard error   * stream.  This will usually be accessed through the   * <code>System.err</code>variable.   */  public static final FileDescriptor err = new FileDescriptor();  static    {      if (Configuration.INIT_LOAD_LIBRARY)        {          System.loadLibrary("io");        }      nativeInit();    }  // These are WHENCE values for seek.  static final int SET = 0;  static final int CUR = 1;  static final int END = 2;  // These are mode values for open().  static final int READ   = 1;  static final int WRITE  = 2;  static final int APPEND = 4;  // EXCL is used only when making a temp file.  static final int EXCL   = 8;  static final int SYNC   = 16;  static final int DSYNC  = 32;  /**   * This is the actual native file descriptor value   */  private long nativeFd = -1L;  /**   * This method is used to initialize an invalid FileDescriptor object.   */  public FileDescriptor()  {  }  private FileDescriptor(long nativeFd)  {    this.nativeFd = nativeFd;  }  FileDescriptor(String path, int mode) throws FileNotFoundException  {    open(path, mode);  }  /**   * This method forces all data that has not yet been physically written to   * the underlying storage medium associated with this    * <code>FileDescriptor</code>   * to be written out.  This method will not return until all data has   * been fully written to the underlying device.  If the device does not   * support this functionality or if an error occurs, then an exception   * will be thrown.   */  public void sync() throws SyncFailedException  {    if (nativeFd == -1L)      throw new SyncFailedException("Invalid FileDescriptor");    nativeSync(nativeFd);  }  /**   * This methods tests whether or not this object represents a valid open   * native file handle.   *   * @return <code>true</code> if this object represents a valid    * native file handle, <code>false</code> otherwise   */  public boolean valid()  {    if (nativeFd == -1L)      return false;    return nativeValid(nativeFd);  }  /**   * Opens the specified file in the specified mode.  This can be done   * in one of the specified modes:   * <ul>   * <li>r - Read Only   * <li>rw - Read / Write   * <li>ra - Read / Write - append to end of file   * <li>rws - Read / Write - synchronous writes of data/metadata   * <li>rwd - Read / Write - synchronous writes of data.   *   * @param path Name of the file to open   * @param mode Mode to open   *   * @exception IOException If an error occurs.   */  void open(String path, int mode) throws FileNotFoundException  {    // We don't want fd leakage.    if (nativeFd != -1L)      throw new InternalError("FileDescriptor already open");    // Note that it can be ok to have an empty path.    // FIXME: verify    if (path == null)      throw new NullPointerException("Path cannot be null");    if ((mode & (READ | WRITE)) == 0)      throw new InternalError("Invalid mode value: " + mode);    nativeFd = nativeOpen(path, mode);  }  /**   * Closes this specified file descriptor   *    * @exception IOException If an error occurs    */      synchronized void close() throws IOException  {    if (nativeFd == -1L)      return;    try      {        nativeClose(nativeFd);      }    finally      {        nativeFd = -1L;      }  }  /**   * Writes a single byte to the file   *   * @param b The byte to write, encoded in the low eight bits   *   * @exception IOException If an error occurs   */  void write(int b) throws IOException  {    if (nativeFd == -1L)      throw new IOException("Invalid FileDescriptor");    nativeWriteByte(nativeFd, (b & 0xFF));  }  /**   * Writes a byte buffer to the file   *   * @param buf The byte buffer to write from   * @param int The offset into the buffer to start writing from   * @param len The number of bytes to write.   *   * @exception IOException If an error occurs   */  void write(byte[] buf, int offset, int len) throws IOException  {    if (nativeFd == -1L)      throw new IOException("Invalid FileDescriptor");    if (len == 0)      return;    if ((offset < 0) || (offset > buf.length))      throw new IllegalArgumentException("Offset invalid: " + offset);    if ((len < 0) || (len > (buf.length - offset)))      throw new IllegalArgumentException("Length invalid: " + len);    // Note that above ops implicitly bomb if buf == null    nativeWriteBuf(nativeFd, buf, offset, len);  }  /**   * Reads a single byte from the file   *   * @return The byte read, in the low eight bits on a long, or -1   * if end of file   *   * @exception IOException If an error occurs   */  int read() throws IOException  {    if (nativeFd == -1L)      throw new IOException("Invalid FileDescriptor");    return nativeReadByte(nativeFd);  }  /**   * Reads a buffer of  bytes from the file   *   * @param buf The buffer to read bytes into   * @param offset The offset into the buffer to start storing bytes   * @param len The number of bytes to read.   *   * @return The number of bytes read, or -1 if end of file.   *   * @exception IOException If an error occurs   */  int read(byte[] buf, int offset, int len) throws IOException  {    if (nativeFd == -1L)      throw new IOException("Invalid FileDescriptor");    if (len == 0)      return(0);    if ((offset < 0) || (offset > buf.length))      throw new IllegalArgumentException("Offset invalid: " + offset);    if ((len < 0) || (len > (buf.length - offset)))      throw new IllegalArgumentException("Length invalid: " + len);    // Note that above ops implicitly bomb if buf == null

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?