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

  1. /*
  2.  * @(#)BufferedOutputStream.java    1.20 97/03/03
  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.  
  25. /**
  26.  * The class implements a buffered output stream. By setting up such 
  27.  * an output stream, an application can write bytes to the underlying 
  28.  * output stream without necessarily causing a call to the underlying 
  29.  * system for each byte written. The data is written into a buffer, 
  30.  * and then written to the underlying stream if the buffer reaches 
  31.  * its capacity, the buffer output stream is closed, or the buffer 
  32.  * output stream is explicity flushed. 
  33.  *
  34.  * @author  Arthur van Hoff
  35.  * @version 1.20, 03/03/97
  36.  * @since   JDK1.0
  37.  */
  38. public 
  39. class BufferedOutputStream extends FilterOutputStream {
  40.     /**
  41.      * The buffer where data is stored. 
  42.      *
  43.      * @since   JDK1.0
  44.      */
  45.     protected byte buf[];
  46.  
  47.     /**
  48.      * The number of valid bytes in the buffer. 
  49.      *
  50.      * @since   JDK1.0
  51.      */
  52.     protected int count;
  53.     
  54.     /**
  55.      * Creates a new buffered output stream to write data to the 
  56.      * specified underlying output stream with a default 512-byte buffer size.
  57.      *
  58.      * @param   out   the underlying output stream.
  59.      * @since   JDK1.0
  60.      */
  61.     public BufferedOutputStream(OutputStream out) {
  62.     this(out, 512);
  63.     }
  64.  
  65.     /**
  66.      * Creates a new buffered output stream to write data to the 
  67.      * specified underlying output stream with the specified buffer size. 
  68.      *
  69.      * @param   out    the underlying output stream.
  70.      * @param   size   the buffer size.
  71.      * @since   JDK1.0
  72.      */
  73.     public BufferedOutputStream(OutputStream out, int size) {
  74.     super(out);
  75.     buf = new byte[size];
  76.     }
  77.  
  78.     /** Flush the internal buffer */
  79.     private void flushBuffer() throws IOException {
  80.         if (count > 0) {
  81.         out.write(buf, 0, count);
  82.         count = 0;
  83.         }
  84.     }
  85.  
  86.     /**
  87.      * Writes the specified byte to this buffered output stream. 
  88.      *
  89.      * @param      b   the byte to be written.
  90.      * @exception  IOException  if an I/O error occurs.
  91.      * @since      JDK1.0
  92.      */
  93.     public synchronized void write(int b) throws IOException {
  94.     if (count >= buf.length) {
  95.         flushBuffer();
  96.     }
  97.     buf[count++] = (byte)b;
  98.     }
  99.  
  100.     /**
  101.      * Writes <code>len</code> bytes from the specified byte array 
  102.      * starting at offset <code>off</code> to this buffered output stream.
  103.      *
  104.      * <p> Ordinarily this method stores bytes from the given array into this
  105.      * stream's buffer, flushing the buffer to the underlying output stream as
  106.      * needed.  If the requested length is at least as large as this stream's
  107.      * buffer, however, then this method will flush the buffer and write the
  108.      * bytes directly to the underlying output stream.  Thus redundant
  109.      * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
  110.      *
  111.      * @param      b     the data.
  112.      * @param      off   the start offset in the data.
  113.      * @param      len   the number of bytes to write.
  114.      * @exception  IOException  if an I/O error occurs.
  115.      */
  116.     public synchronized void write(byte b[], int off, int len) throws IOException {
  117.     if (len >= buf.length) {
  118.         /* If the request length exceeds the size of the output buffer,
  119.                flush the output buffer and then write the data directly.
  120.                In this way buffered streams will cascade harmlessly. */
  121.         flushBuffer();
  122.         out.write(b, off, len);
  123.         return;
  124.     }
  125.     if (len > buf.length - count) {
  126.         flushBuffer();
  127.     }
  128.     System.arraycopy(b, off, buf, count, len);
  129.     count += len;
  130.     }
  131.  
  132.     /**
  133.      * Flushes this buffered output stream. This forces any buffered 
  134.      * output bytes to be written out to the underlying output stream. 
  135.      *
  136.      * @exception  IOException  if an I/O error occurs.
  137.      * @see        java.io.FilterOutputStream#out
  138.      */
  139.     public synchronized void flush() throws IOException {
  140.         flushBuffer();
  141.     out.flush();
  142.     }
  143. }
  144.