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

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