home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / FileInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.4 KB  |  218 lines

  1. /*
  2.  * @(#)FileInputStream.java    1.36 98/03/18
  3.  *
  4.  * Copyright 1994-1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A file input stream is an input stream for reading data from a 
  19.  * <code>File</code> or from a <code>FileDescriptor</code>. 
  20.  *
  21.  * @author  Arthur van Hoff
  22.  * @version 1.36, 03/18/98
  23.  * @see     java.io.File
  24.  * @see     java.io.FileDescriptor
  25.  * @see        java.io.FileOutputStream
  26.  * @since   JDK1.0
  27.  */
  28. public
  29. class FileInputStream extends InputStream 
  30. {
  31.     /* File Descriptor - handle to the open file */
  32.     private FileDescriptor fd;
  33.     
  34.     /**
  35.      * Creates an input file stream to read from a file with the 
  36.      * specified name. 
  37.      *
  38.      * @param      name   the system-dependent file name.
  39.      * @exception  FileNotFoundException  if the file is not found.
  40.      * @exception  SecurityException      if a security manager exists, its
  41.      *               <code>checkRead</code> method is called with the name
  42.      *               argument to see if the application is allowed read access
  43.      *               to the file.
  44.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  45.      */
  46.     public FileInputStream(String name) throws FileNotFoundException {
  47.     SecurityManager security = System.getSecurityManager();
  48.     if (security != null) {
  49.         security.checkRead(name);
  50.     }
  51.     try {
  52.         fd = new FileDescriptor();
  53.         open(name);
  54.     } catch (IOException e) {
  55.         throw new FileNotFoundException(name);
  56.     }
  57.     }
  58.     
  59.     /**
  60.      * Creates an input file stream to read from the specified 
  61.      * <code>File</code> object. 
  62.      *
  63.      * @param      file   the file to be opened for reading.
  64.      * @exception  FileNotFoundException  if the file is not found.
  65.      * @exception  SecurityException      if a security manager exists, its
  66.      *               <code>checkRead</code> method is called with the pathname
  67.      *               of this <code>File</code> argument to see if the
  68.      *               application is allowed read access to the file.
  69.      * @see        java.io.File#getPath()
  70.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  71.      */
  72.     public FileInputStream(File file) throws FileNotFoundException {
  73.     this(file.getPath());
  74.     }
  75.  
  76.     /**
  77.      * Creates an input file stream to read from the specified file descriptor.
  78.      *
  79.      * @param      fdObj   the file descriptor to be opened for reading.
  80.      * @exception  SecurityException  if a security manager exists, its
  81.      *               <code>checkRead</code> method is called with the file
  82.      *               descriptor to see if the application is allowed to read
  83.      *               from the specified file descriptor.
  84.      * @see        java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
  85.      */
  86.     public FileInputStream(FileDescriptor fdObj) {
  87.     SecurityManager security = System.getSecurityManager();
  88.     if (fdObj == null) {
  89.         throw new NullPointerException();
  90.     }
  91.     if (security != null) {
  92.         security.checkRead(fdObj);
  93.     }
  94.     fd = fdObj;
  95.     }
  96.  
  97.     /**
  98.      * Opens the specified file for reading.
  99.      * @param name the name of the file
  100.      */
  101.     private native void open(String name) throws IOException;
  102.  
  103.     /**
  104.      * Reads a byte of data from this input stream. This method blocks 
  105.      * if no input is yet available. 
  106.      *
  107.      * @return     the next byte of data, or <code>-1</code> if the end of the
  108.      *             file is reached.
  109.      * @exception  IOException  if an I/O error occurs.
  110.      */
  111.     public native int read() throws IOException;
  112.  
  113.  
  114.     /** 
  115.      * Reads a subarray as a sequence of bytes. 
  116.      * @param b the data to be written
  117.      * @param off the start offset in the data
  118.      * @param len the number of bytes that are written
  119.      * @exception IOException If an I/O error has occurred. 
  120.      */ 
  121.     private native int readBytes(byte b[], int off, int len) throws IOException;
  122.  
  123.     /**
  124.      * Reads up to <code>b.length</code> bytes of data from this input 
  125.      * stream into an array of bytes. This method blocks until some input 
  126.      * is available. 
  127.      *
  128.      * @param      b   the buffer into which the data is read.
  129.      * @return     the total number of bytes read into the buffer, or
  130.      *             <code>-1</code> if there is no more data because the end of
  131.      *             the file has been reached.
  132.      * @exception  IOException  if an I/O error occurs.
  133.      */
  134.     public int read(byte b[]) throws IOException {
  135.     return readBytes(b, 0, b.length);
  136.     }
  137.  
  138.     /**
  139.      * Reads up to <code>len</code> bytes of data from this input stream 
  140.      * into an array of bytes. This method blocks until some input is 
  141.      * available. 
  142.      *
  143.      * @param      b     the buffer into which the data is read.
  144.      * @param      off   the start offset of the data.
  145.      * @param      len   the maximum number of bytes read.
  146.      * @return     the total number of bytes read into the buffer, or
  147.      *             <code>-1</code> if there is no more data because the end of
  148.      *             the file has been reached.
  149.      * @exception  IOException  if an I/O error occurs.
  150.      */
  151.     public int read(byte b[], int off, int len) throws IOException {
  152.     return readBytes(b, off, len);
  153.     }
  154.  
  155.     /**
  156.      * Skips over and discards <code>n</code> bytes of data from the 
  157.      * input stream. The <code>skip</code> method may, for a variety of 
  158.      * reasons, end up skipping over some smaller number of bytes, 
  159.      * possibly <code>0</code>. The actual number of bytes skipped is returned.
  160.      *
  161.      * @param      n   the number of bytes to be skipped.
  162.      * @return     the actual number of bytes skipped.
  163.      * @exception  IOException  if an I/O error occurs.
  164.      */
  165.     public native long skip(long n) throws IOException;
  166.  
  167.     /**
  168.      * Returns the number of bytes that can be read from this file input
  169.      * stream without blocking.
  170.      *
  171.      * @return     the number of bytes that can be read from this file input
  172.      *             stream without blocking.
  173.      * @exception  IOException  if an I/O error occurs.
  174.      */
  175.     public native int available() throws IOException;
  176.  
  177.     /**
  178.      * Closes this file input stream and releases any system resources 
  179.      * associated with the stream. 
  180.      *
  181.      * @exception  IOException  if an I/O error occurs.
  182.      */
  183.     public native void close() throws IOException;
  184.  
  185.     /**
  186.      * Returns the opaque file descriptor object associated with this stream.
  187.      *
  188.      * @return     the file descriptor object associated with this stream.
  189.      * @exception  IOException  if an I/O error occurs.
  190.      * @see        java.io.FileDescriptor
  191.      */
  192.     public final FileDescriptor getFD() throws IOException {
  193.     if (fd != null) return fd;
  194.     throw new IOException();
  195.     }
  196.  
  197.     private static native void initIDs();
  198.  
  199.     static {
  200.     initIDs();
  201.     }
  202.  
  203.     /**
  204.      * Ensures that the <code>close</code> method of this file input stream is
  205.      * called when there are no more references to it. 
  206.      *
  207.      * @exception  IOException  if an I/O error occurs.
  208.      * @see        java.io.FileInputStream#close()
  209.      */
  210.     protected void finalize() throws IOException {
  211.     if (fd != null) {
  212.         if (fd != fd.in) {
  213.         close();
  214.         }
  215.     }
  216.     }
  217. }
  218.