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

  1. /*
  2.  * @(#)FilterOutputStream.java    1.13 96/04/08 Jonathan Payne
  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.  * Abstract class representing a filtered output stream of bytes.
  24.  * This class is the basis for enhancing output stream functionality.
  25.  * It allows multiple output stream filters to be chained together,
  26.  * each providing additional functionality.
  27.  * @version     1.13, 08 Apr 1996
  28.  * @author    Jonathan Payne
  29.  */
  30. public
  31. class FilterOutputStream extends OutputStream {
  32.     /**
  33.      * The actual output stream.
  34.      */
  35.     protected OutputStream out;
  36.  
  37.     /**
  38.      * Creates an output stream filter.
  39.      * @param out    the output stream
  40.      */
  41.     public FilterOutputStream(OutputStream out) {
  42.     this.out = out;
  43.     }
  44.  
  45.     /**
  46.      * Writes a byte. Will block until the byte is actually
  47.      * written.
  48.      * @param b the byte
  49.      * @exception IOException If an I/O error has occurred.
  50.      */
  51.     public void write(int b) throws IOException {
  52.     out.write(b);
  53.     }
  54.  
  55.     /**
  56.      * Writes an array of bytes. Will block until the bytes
  57.      * are actually written.
  58.      * @param b    the data to be written
  59.      * @exception IOException If an I/O error has occurred.
  60.      */
  61.     public void write(byte b[]) throws IOException {
  62.     write(b, 0, b.length);
  63.     }
  64.  
  65.     /**
  66.      * Writes a subarray of bytes. To be efficient it should
  67.      * be overridden in a subclass. 
  68.      * @param b    the data to be written
  69.      * @param off    the start offset in the data
  70.      * @param len    the number of bytes that are written
  71.      * @exception IOException If an I/O error has occurred.
  72.      */
  73.     public void write(byte b[], int off, int len) throws IOException {
  74.     for (int i = 0 ; i < len ; i++) {
  75.         out.write(b[off + i]);
  76.     }
  77.     }
  78.  
  79.     /**
  80.      * Flushes the stream. This will write any buffered
  81.      * output bytes.
  82.      * @exception IOException If an I/O error has occurred.
  83.      */
  84.     public void flush() throws IOException {
  85.     out.flush();
  86.     }
  87.  
  88.     /**
  89.      * Closes the stream. This method must be called
  90.      * to release any resources associated with the
  91.      * stream.
  92.      * @exception IOException If an I/O error has occurred.
  93.      */
  94.     public void close() throws IOException {
  95.     try {
  96.       flush();
  97.     } catch (IOException ignored) {
  98.     }
  99.     out.close();
  100.     }
  101. }
  102.