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

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