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

  1. /*
  2.  * @(#)FileOutputStream.java    1.21 95/11/15 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. import java.io.File;
  22.  
  23. /**
  24.  * File output stream, can be constructed from
  25.  * a file descriptor or a file name.
  26.  * @see    FileInputStream
  27.  * @see    File
  28.  * @version     1.21, 15 Nov 1995
  29.  * @author    Arthur van Hoff
  30.  */
  31. public
  32. class FileOutputStream extends OutputStream
  33. {
  34.     /**
  35.      * The system dependent file descriptor. The value is
  36.      * 1 more than actual file descriptor. This means that
  37.      * the default value 0 indicates that the file is not open.
  38.      */
  39.     private FileDescriptor fd;
  40.  
  41.     /**
  42.      * Creates an output file with the specified system dependent
  43.      * file name.
  44.      * @param name the system dependent file name 
  45.      * @exception IOException If the file is not found.
  46.      */
  47.     public FileOutputStream(String name) throws IOException {
  48.     SecurityManager security = System.getSecurityManager();
  49.     if (security != null) {
  50.         security.checkWrite(name);
  51.     }
  52.     try {
  53.         fd = new FileDescriptor();
  54.         open(name);
  55.     } catch (IOException e) {
  56.         throw new FileNotFoundException(name);
  57.     }
  58.     }
  59.     
  60.     /**
  61.      * Creates an output file with the specified File object.
  62.      * @param file the file to be opened for reading
  63.      * @exception IOException If the file is not found.
  64.      */
  65.     public FileOutputStream(File file) throws IOException {
  66.     this(file.getPath());
  67.     }
  68.  
  69.     /* This routine attaches a stream to existing FileDescriptor object.
  70.      * Subclass SocketOutputStream uses this routine.
  71.      */
  72.     public FileOutputStream(FileDescriptor fdObj) {
  73.     SecurityManager security = System.getSecurityManager();
  74.     if (security != null) {
  75.         security.checkWrite(fdObj);
  76.     }
  77.     fd = fdObj;
  78.     }
  79.  
  80.     /**
  81.      * Opens a file, with the specified name, for writing.
  82.      * @param name name of file to be opened
  83.      */
  84.     private native void open(String name) throws IOException;
  85.  
  86.     /**
  87.      * Writes a byte of data. This method will block until the byte is 
  88.      * actually written.
  89.      * @param b the byte to be written
  90.      * @exception IOException If an I/O error has occurred.
  91.      */
  92.     public native void write(int b) throws IOException;
  93.  
  94.     /**
  95.      * Writes a sub array as a sequence of bytes.
  96.      * @param b the data to be written
  97.      * @param off the start offset in the data
  98.      * @param len the number of bytes that are written
  99.      * @exception IOException If an I/O error has occurred.
  100.      */
  101.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  102.  
  103.     /**
  104.      * Writes an array of bytes. Will block until the bytes
  105.      * are actually written.
  106.      * @param b    the data to be written
  107.      * @exception IOException If an I/O error has occurred.
  108.      */
  109.     public void write(byte b[]) throws IOException {
  110.     writeBytes(b, 0, b.length);
  111.     }
  112.  
  113.     /**
  114.      * Writes a sub array of bytes. 
  115.      * @param b    the data to be written
  116.      * @param off    the start offset in the data
  117.      * @param len    the number of bytes that are written
  118.      * @exception IOException If an I/O error has occurred.
  119.      */
  120.     public void write(byte b[], int off, int len) throws IOException {
  121.     writeBytes(b, off, len);
  122.     }
  123.  
  124.     /**
  125.      * Closes the stream. This method must be called
  126.      * to release any resources associated with the
  127.      * stream.
  128.      * @exception IOException If an I/O error has occurred.
  129.      */
  130.      public native void close() throws IOException;
  131.  
  132.      /**
  133.       * Returns the file descriptor associated with this stream.
  134.       * @return the file descriptor.
  135.       */
  136.      public final FileDescriptor getFD()  throws IOException {
  137.     if (fd != null) return fd;
  138.     throw new IOException();
  139.      }
  140.     
  141.     /**
  142.      * Closes the stream when garbage is collected.
  143.      */
  144.     protected void finalize() throws IOException {
  145.     if (fd != null) close();
  146.     }
  147. }
  148.