home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / FileInputStream.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  168 lines

  1. /*
  2.  * @(#)FileInputStream.java    1.29 95/12/19 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.29, 19 Dec 1995
  28.  * @author    Arthur van Hoff
  29.  */
  30. public
  31. class FileInputStream extends InputStream 
  32. {
  33.     /* File Descriptor - handle to the open file */
  34.     private FileDescriptor fd;
  35.     
  36.     /**
  37.      * Creates an input file with the specified system dependent file 
  38.      * name.
  39.      * @param name the system dependent file name
  40.      * @exception IOException If the file is not found.
  41.      */
  42.     public FileInputStream(String name) throws FileNotFoundException {
  43.     SecurityManager security = System.getSecurityManager();
  44.     if (security != null) {
  45.         security.checkRead(name);
  46.     }
  47.     try {
  48.         fd = new FileDescriptor();
  49.         open(name);
  50.     } catch (IOException e) {
  51.         throw new FileNotFoundException(name);
  52.     }
  53.     }
  54.     
  55.     /**
  56.      * Creates an input file from the specified File object.
  57.      * @param file the file to be opened for reading
  58.      * @exception IOException If the file is not found.
  59.      */
  60.     public FileInputStream(File file) throws FileNotFoundException {
  61.     this(file.getPath());
  62.     }
  63.  
  64.     /* This routine attaches a stream to existing FileDescriptor object.
  65.      * Subclass SocketInputStream uses this routine.
  66.      */
  67.     public FileInputStream(FileDescriptor fdObj) {
  68.     SecurityManager security = System.getSecurityManager();
  69.     if (security != null) {
  70.         security.checkRead(fdObj);
  71.     }
  72.     fd = fdObj;
  73.     }
  74.  
  75.     /**
  76.      * Opens the specified file for reading.
  77.      * @param name the name of the file
  78.      */
  79.     private native void open(String name) throws IOException;
  80.  
  81.     /**
  82.      * Reads a byte of data. This method will block if no input is 
  83.      * available.
  84.      * @return     the byte read, or -1 if the end of the
  85.      *        stream is reached.
  86.      * @exception IOException If an I/O error has occurred.
  87.      */
  88.     public native int read() throws IOException;
  89.  
  90.  
  91.     /** 
  92.      * Reads a subarray as a sequence of bytes. 
  93.      * @param b the data to be written
  94.      * @param off the start offset in the data
  95.      * @param len the number of bytes that are written
  96.      * @exception IOException If an I/O error has occurred. 
  97.      */ 
  98.     private native int readBytes(byte b[], int off, int len) throws IOException;
  99.  
  100.     /**
  101.      * Reads data into an array of bytes.
  102.      * This method blocks until some input is available.
  103.      * @param b    the buffer into which the data is read
  104.      * @return  the actual number of bytes read. -1 is
  105.      *         returned if the end of stream is reached.
  106.      * @exception IOException If an I/O error has occurred.
  107.      */
  108.     public int read(byte b[]) throws IOException {
  109.     return readBytes(b, 0, b.length);
  110.     }
  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.      * @param off the start offset of the data
  117.      * @param len the maximum number of bytes read
  118.      * @return  the actual number of bytes read. -1 is
  119.      *         returned when the end of the stream is reached.
  120.      * @exception IOException If an I/O error has occurred.
  121.      */
  122.     public int read(byte b[], int off, int len) throws IOException {
  123.     return readBytes(b, off, len);
  124.     }
  125.  
  126.     /**
  127.      * Skips n bytes of input.
  128.      * @param n the number of bytes to be skipped
  129.      * @return    the actual number of bytes skipped.
  130.      * @exception IOException If an I/O error has occurred.
  131.      */
  132.     public native long skip(long n) throws IOException;
  133.  
  134.     /**
  135.      * Returns the number of bytes that can be read
  136.      * without blocking.
  137.      * @return the number of available bytes, which is initially
  138.      *        equal to the file size.
  139.      */
  140.     public native int available() throws IOException;
  141.  
  142.     /**
  143.      * Closes the input stream. This method must be called
  144.      * to release any resources associated with
  145.      * the stream.
  146.      * @exception IOException If an I/O error has occurred.
  147.      */
  148.     public native void close() throws IOException;
  149.  
  150.     /**
  151.      * Returns the opaque file descriptor object associated with this stream.
  152.      * @return the file descriptor.
  153.      */
  154.     public final FileDescriptor getFD() throws IOException {
  155.     if (fd != null) return fd;
  156.     throw new IOException();
  157.     }
  158.  
  159.     /**
  160.      * Closes the stream when garbage is collected.
  161.      */
  162.     protected void finalize() throws IOException {
  163.     if (fd != null) close();
  164.     }
  165. }
  166.  
  167.  
  168.