home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / FileInputStream.java < prev    next >
Text File  |  1997-05-20  |  8KB  |  231 lines

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