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

  1. /*
  2.  * @(#)BufferedOutputStream.java    1.17 95/12/18 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.  
  22. /**
  23.  * A buffered output stream. This stream lets you write characters
  24.  * to a stream without causing a write every time. The data
  25.  * is first written into a buffer. Data is written to the
  26.  * actual stream only when the buffer is full, or when the stream is
  27.  * flushed.
  28.  *
  29.  * @version     1.17, 18 Dec 1995
  30.  * @author    Arthur van Hoff
  31.  */
  32. public 
  33. class BufferedOutputStream extends FilterOutputStream {
  34.     /**
  35.      * The buffer where data is stored.
  36.      */
  37.     protected byte buf[];
  38.  
  39.     /**
  40.      * The number of bytes in the buffer.
  41.      */
  42.     protected int count;
  43.     
  44.     /**
  45.      * Creates a new buffered stream with a default
  46.      * buffer size.
  47.      * @param out     the output stream
  48.      */
  49.     public BufferedOutputStream(OutputStream out) {
  50.     this(out, 512);
  51.     }
  52.  
  53.     /**
  54.      * Creates a new buffered stream with the specified
  55.      * buffer size.
  56.      * @param out         the output stream
  57.      * @param size    the buffer size
  58.      */
  59.     public BufferedOutputStream(OutputStream out, int size) {
  60.     super(out);
  61.     buf = new byte[size];
  62.     }
  63.  
  64.     /**
  65.      * Writes a byte. This method will block until the byte is actually
  66.      * written.
  67.      * @param b the byte to be written
  68.      * @exception IOException If an I/O error has occurred.
  69.      */
  70.     public synchronized void write(int b) throws IOException {
  71.     if (count == buf.length) {
  72.         flush();
  73.     }
  74.     buf[count++] = (byte)b;
  75.     }
  76.  
  77.     /**
  78.      * Writes a subarray of bytes.
  79.      * @param b    the data to be written
  80.      * @param off    the start offset in the data
  81.      * @param len    the number of bytes that are written
  82.      * @exception IOException If an I/O error has occurred.
  83.      */
  84.     public synchronized void write(byte b[], int off, int len) throws IOException {
  85.     int avail = buf.length - count;
  86.  
  87.     if (len <= avail) {
  88.         System.arraycopy(b, off, buf, count, len);
  89.         count += len;
  90.         return;
  91.     }
  92.  
  93.     flush();
  94.     out.write(b, off, len);
  95.     }
  96.  
  97.     /**
  98.      * Flushes the stream. This will write any buffered
  99.      * output bytes.
  100.      * @exception IOException If an I/O error has occurred.
  101.      */
  102.     public synchronized void flush() throws IOException {
  103.     out.write(buf, 0, count);
  104.     out.flush();
  105.     count = 0;
  106.     }
  107. }
  108.