View Javadoc
1   /*
2    * Copyright (c) 1994, 2003, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package java.io;
27  
28  /**
29   * The class implements a buffered output stream. By setting up such
30   * an output stream, an application can write bytes to the underlying
31   * output stream without necessarily causing a call to the underlying
32   * system for each byte written.
33   *
34   * @author  Arthur van Hoff
35   * @since   JDK1.0
36   */
37  public
38  class BufferedOutputStream extends FilterOutputStream {
39      /**
40       * The internal buffer where data is stored.
41       */
42      protected byte buf[];
43  
44      /**
45       * The number of valid bytes in the buffer. This value is always
46       * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
47       * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
48       * byte data.
49       */
50      protected int count;
51  
52      /**
53       * Creates a new buffered output stream to write data to the
54       * specified underlying output stream.
55       *
56       * @param   out   the underlying output stream.
57       */
58      public BufferedOutputStream(OutputStream out) {
59          this(out, 8192);
60      }
61  
62      /**
63       * Creates a new buffered output stream to write data to the
64       * specified underlying output stream with the specified buffer
65       * size.
66       *
67       * @param   out    the underlying output stream.
68       * @param   size   the buffer size.
69       * @exception IllegalArgumentException if size &lt;= 0.
70       */
71      public BufferedOutputStream(OutputStream out, int size) {
72          super(out);
73          if (size <= 0) {
74              throw new IllegalArgumentException("Buffer size <= 0");
75          }
76          buf = new byte[size];
77      }
78  
79      /** Flush the internal buffer */
80      private void flushBuffer() throws IOException {
81          if (count > 0) {
82              out.write(buf, 0, count);
83              count = 0;
84          }
85      }
86  
87      /**
88       * Writes the specified byte to this buffered output stream.
89       *
90       * @param      b   the byte to be written.
91       * @exception  IOException  if an I/O error occurs.
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 }