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

  1. /*
  2.  * @(#)FileOutputStream.java    1.26 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. import java.io.File;
  25.  
  26. /**
  27.  * A file output stream is an output stream for writing data to a 
  28.  * <code>File</code> or to a <code>FileDescriptor</code>. 
  29.  *
  30.  * @author  Arthur van Hoff
  31.  * @version 1.26, 01/22/97
  32.  * @see     java.io.File
  33.  * @see     java.io.FileDescriptor
  34.  * @see     java.io.FileInputStream
  35.  * @since   JDK1.0
  36.  */
  37. public
  38. class FileOutputStream extends OutputStream
  39. {
  40.     /**
  41.      * The system dependent file descriptor. The value is
  42.      * 1 more than actual file descriptor. This means that
  43.      * the default value 0 indicates that the file is not open.
  44.      */
  45.     private FileDescriptor fd;
  46.  
  47.     /**
  48.      * Creates an output file stream to write to the file with the 
  49.      * specified name. 
  50.      *
  51.      * @param      name   the system-dependent filename.
  52.      * @exception  IOException  if the file could not be opened for writing.
  53.      * @exception  SecurityException  if a security manager exists, its
  54.      *               <code>checkWrite</code> method is called with the name
  55.      *               argument to see if the application is allowed write access
  56.      *               to the file.
  57.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  58.      * @since      JDK1.0
  59.      */
  60.     public FileOutputStream(String name) throws IOException {
  61.     SecurityManager security = System.getSecurityManager();
  62.     if (security != null) {
  63.         security.checkWrite(name);
  64.     }
  65.     try {
  66.         fd = new FileDescriptor();
  67.         open(name);
  68.     } catch (IOException e) {
  69.         throw new FileNotFoundException(name);
  70.     }
  71.     }
  72.  
  73.     /**
  74.      * Creates an output file with the specified system dependent
  75.      * file name.
  76.      * @param name the system dependent file name 
  77.      * @exception IOException If the file is not found.
  78.      * @since     JDK1.1
  79.      */
  80.     public FileOutputStream(String name, boolean append) throws IOException {
  81.     SecurityManager security = System.getSecurityManager();
  82.     if (security != null) {
  83.         security.checkWrite(name);
  84.     }
  85.     try {
  86.         fd = new FileDescriptor();
  87.         if(append)
  88.         openAppend(name);
  89.         else
  90.         open(name);
  91.     } catch (IOException e) {
  92.         throw new FileNotFoundException(name);
  93.     }
  94.     }
  95.     
  96.     /**
  97.      * Creates a file output stream to write to the specified 
  98.      * <code>File</code> object. 
  99.      *
  100.      * @param      file   the file to be opened for writing.
  101.      * @exception  IOException        if the file could not be opened for
  102.      *               writing.
  103.      * @exception  SecurityException  if a security manager exists, its
  104.      *               <code>checkWrite</code> method is called with the pathname
  105.      *               of the <code>File</code> argument to see if the
  106.      *               application is allowed write access to the file. This may
  107.      *               result in a security exception.
  108.      * @see        java.io.File#getPath()
  109.      * @see        java.lang.SecurityException
  110.      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
  111.      * @since      JDK1.0
  112.      */
  113.     public FileOutputStream(File file) throws IOException {
  114.     this(file.getPath());
  115.     }
  116.  
  117.     /**
  118.      * Creates an output file stream to write to the specified file descriptor.
  119.      *
  120.      * @param      fdObj   the file descriptor to be opened for writing.
  121.      * @exception  SecurityException  if a security manager exists, its
  122.      *               <code>checkWrite</code> method is called with the file
  123.      *               descriptor to see if the application is allowed to write
  124.      *               to the specified file descriptor.
  125.      * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
  126.      * @since      JDK1.0
  127.      */
  128.     public FileOutputStream(FileDescriptor fdObj) {
  129.     SecurityManager security = System.getSecurityManager();
  130.     if (fdObj == null) {
  131.         throw new NullPointerException();
  132.     }
  133.     if (security != null) {
  134.         security.checkWrite(fdObj);
  135.     }
  136.     fd = fdObj;
  137.     }
  138.  
  139.     /**
  140.      * Opens a file, with the specified name, for writing.
  141.      * @param name name of file to be opened
  142.      */
  143.     private native void open(String name) throws IOException;
  144.  
  145.     /**
  146.      * Opens a file, with the specified name, for appending.
  147.      * @param name name of file to be opened
  148.      */
  149.     private native void openAppend(String name) throws IOException;
  150.  
  151.     /**
  152.      * Writes the specified byte to this file output stream. 
  153.      *
  154.      * @param      b   the byte to be written.
  155.      * @exception  IOException  if an I/O error occurs.
  156.      * @since      JDK1.0
  157.      */
  158.     public native void write(int b) throws IOException;
  159.  
  160.     /**
  161.      * Writes a sub array as a sequence of bytes.
  162.      * @param b the data to be written
  163.      * @param off the start offset in the data
  164.      * @param len the number of bytes that are written
  165.      * @exception IOException If an I/O error has occurred.
  166.      */
  167.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  168.  
  169.     /**
  170.      * Writes <code>b.length</code> bytes from the specified byte array 
  171.      * to this file output stream. 
  172.      *
  173.      * @param      b   the data.
  174.      * @exception  IOException  if an I/O error occurs.
  175.      * @since      JDK1.0
  176.      */
  177.     public void write(byte b[]) throws IOException {
  178.     writeBytes(b, 0, b.length);
  179.     }
  180.  
  181.     /**
  182.      * Writes <code>len</code> bytes from the specified byte array 
  183.      * starting at offset <code>off</code> to this file output stream. 
  184.      *
  185.      * @param      b     the data.
  186.      * @param      off   the start offset in the data.
  187.      * @param      len   the number of bytes to write.
  188.      * @exception  IOException  if an I/O error occurs.
  189.      * @since      JDK1.0
  190.      */
  191.     public void write(byte b[], int off, int len) throws IOException {
  192.     writeBytes(b, off, len);
  193.     }
  194.  
  195.     /**
  196.      * Closes this file output stream and releases any system resources 
  197.      * associated with this stream. 
  198.      *
  199.      * @exception  IOException  if an I/O error occurs.
  200.      * @since      JDK1.0
  201.      */
  202.      public native void close() throws IOException;
  203.  
  204.      /**
  205.       * Returns the file descriptor associated with this stream.
  206.      *
  207.      * @return  the file descriptor object associated with this stream.
  208.      * @exception  IOException  if an I/O error occurs.
  209.      * @see        java.io.FileDescriptor
  210.      * @since      JDK1.0
  211.       */
  212.      public final FileDescriptor getFD()  throws IOException {
  213.     if (fd != null) return fd;
  214.     throw new IOException();
  215.      }
  216.     
  217.     /**
  218.      * Ensures that the <code>close</code> method of this file output stream is
  219.      * called when there are no more references to this stream. 
  220.      *
  221.      * @exception  IOException  if an I/O error occurs.
  222.      * @see        java.io.FileInputStream#close()
  223.      * @since      JDK1.0
  224.      */
  225.     protected void finalize() throws IOException {
  226.      if (fd != null) {
  227.          if (fd == fd.out || fd == fd.err) {
  228.          flush();
  229.          } else {
  230.          close();
  231.          }
  232.      }
  233.     }
  234. }
  235.