View Javadoc

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