home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / io / fileinpu.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.5 KB  |  177 lines

  1. /*
  2.  * @(#)FileInputStream.java    1.23 95/08/11 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * File input stream, can be constructed from
  24.  * a file descriptor or a file name.
  25.  * @see    FileOutputStream
  26.  * @see    File
  27.  * @version     1.23, 08/11/95
  28.  * @author    Arthur van Hoff
  29.  */
  30. public
  31. class FileInputStream extends InputStream 
  32. {
  33.     /**
  34.      * The system dependent file descriptor. The value is
  35.      * 1 more than the actual file descriptor. This means that
  36.      * the default value, 0, indicates that the file is not open.
  37.      */
  38.     private int fd;
  39.  
  40.     /**
  41.      * Creates an input file with the specified system dependent file 
  42.      * name.
  43.      * @param name the system dependent file name
  44.      * @exception IOException If the file is not found.
  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.         open(name);
  53.     } catch (IOException e) {
  54.         throw new FileNotFoundException(name);
  55.     }
  56.     }
  57.     
  58.     /**
  59.      * Creates an input file from the specified File object.
  60.      * @param file the file to be opened for reading
  61.      * @exception IOException If the file is not found.
  62.      */
  63.     public FileInputStream(File file) throws FileNotFoundException {
  64.     this(file.getPath());
  65.     }
  66.     
  67.     /**
  68.      * Creates an input file with the specified system dependent
  69.      * file descriptor.
  70.      * @param fd the system dependent file descriptor
  71.      * @exception IOException If an I/O error has occurred.
  72.      */
  73.     public FileInputStream(int fd) throws IOException {
  74.     SecurityManager security = System.getSecurityManager();
  75.     if (security != null) {
  76.         security.checkRead(fd);
  77.     }
  78.     openfd(fd);
  79.     }
  80.  
  81.     /**
  82.      * Opens the specified file for reading.
  83.      * @param name the name of the file
  84.      */
  85.     private native void open(String name) throws IOException;
  86.  
  87.     /**
  88.      * Uses a specified system dependent file descriptor for reading.
  89.      * @param the system dependent file descriptor
  90.      */
  91.     private native void openfd(int fd) throws IOException;
  92.  
  93.     /**
  94.      * Reads a byte of data. This method will block if no input is 
  95.      * available.
  96.      * @return     the byte read, or -1 if the end of the
  97.      *        stream is reached.
  98.      * @exception IOException If an I/O error has occurred.
  99.      */
  100.     public native int read() throws IOException;
  101.  
  102.  
  103.     /** 
  104.      * Reads a sub array as a sequence of bytes. 
  105.      * @param b the data to be written
  106.      * @param off the start offset in the data
  107.      * @param len the number of bytes that are written
  108.      * @exception IOException If an I/O error has occurred. 
  109.      */ 
  110.     private native int readBytes(byte b[], int off, int len) throws IOException;
  111.  
  112.     /**
  113.      * Reads data into an array of bytes.
  114.      * This method blocks until some input is available.
  115.      * @param b    the buffer into which the data is read
  116.      * @return  the actual number of bytes read, -1 is
  117.      *         returned when the end of the stream is reached.
  118.      * @exception IOException If an I/O error has occurred.
  119.      */
  120.     public int read(byte b[]) throws IOException {
  121.     return readBytes(b, 0, b.length);
  122.     }
  123.  
  124.     /**
  125.      * Reads data into an array of bytes.
  126.      * This method blocks until some input is available.
  127.      * @param b    the buffer into which the data is read
  128.      * @param off the start offset of the data
  129.      * @param len the maximum number of bytes read
  130.      * @return  the actual number of bytes read, -1 is
  131.      *         returned when the end of the stream is reached.
  132.      * @exception IOException If an I/O error has occurred.
  133.      */
  134.     public int read(byte b[], int off, int len) throws IOException {
  135.     return readBytes(b, off, len);
  136.     }
  137.  
  138.     /**
  139.      * Skips n bytes of input.
  140.      * @param n the number of bytes to be skipped
  141.      * @return    the actual number of bytes skipped.
  142.      * @exception IOException If an I/O error has occurred.
  143.      */
  144.     public native long skip(long n) throws IOException;
  145.  
  146.     /**
  147.      * Returns the number of bytes that can be read
  148.      * without blocking.
  149.      * @return the number of available bytes, which is initially
  150.      *        equal to the file size.
  151.      */
  152.     public native int available() throws IOException;
  153.  
  154.     /**
  155.      * Closes the input stream. This method must be called
  156.      * to release any resources associated with
  157.      * the stream.
  158.      * @exception IOException If an I/O error has occurred.
  159.      */
  160.     public native void close() throws IOException;
  161.  
  162.     /**
  163.      * Returns the file descriptor associated with this stream.
  164.      * @return the file descriptor.
  165.      */
  166.     public final int getFD() {
  167.     return fd - 1;
  168.     }
  169.  
  170.     /**
  171.      * Closes the stream when garbage is collected.
  172.      */
  173.     protected void finalize() throws IOException {
  174.     close();
  175.     }
  176. }
  177.