KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > BufferedOutputStream


1 /*
2  * @(#)BufferedOutputStream.java 1.33 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * The class implements a buffered output stream. By setting up such
12  * an output stream, an application can write bytes to the underlying
13  * output stream without necessarily causing a call to the underlying
14  * system for each byte written.
15  *
16  * @author Arthur van Hoff
17  * @version 1.33, 12/19/03
18  * @since JDK1.0
19  */

20 public
21 class BufferedOutputStream extends FilterOutputStream JavaDoc {
22     /**
23      * The internal buffer where data is stored.
24      */

25     protected byte buf[];
26
27     /**
28      * The number of valid bytes in the buffer. This value is always
29      * in the range <tt>0</tt> through <tt>buf.length</tt>; elements
30      * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
31      * byte data.
32      */

33     protected int count;
34     
35     /**
36      * Creates a new buffered output stream to write data to the
37      * specified underlying output stream.
38      *
39      * @param out the underlying output stream.
40      */

41     public BufferedOutputStream(OutputStream JavaDoc out) {
42     this(out, 8192);
43     }
44
45     /**
46      * Creates a new buffered output stream to write data to the
47      * specified underlying output stream with the specified buffer
48      * 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 BufferedOutputStream(OutputStream JavaDoc out, int size) {
55     super(out);
56         if (size <= 0) {
57             throw new IllegalArgumentException JavaDoc("Buffer size <= 0");
58         }
59     buf = new byte[size];
60     }
61
62     /** Flush the internal buffer */
63     private void flushBuffer() throws IOException JavaDoc {
64         if (count > 0) {
65         out.write(buf, 0, count);
66         count = 0;
67         }
68     }
69
70     /**
71      * Writes the specified byte to this buffered output stream.
72      *
73      * @param b the byte to be written.
74      * @exception IOException if an I/O error occurs.
75      */

76     public synchronized void write(int b) throws IOException JavaDoc {
77     if (count >= buf.length) {
78         flushBuffer();
79     }
80     buf[count++] = (byte)b;
81     }
82
83     /**
84      * Writes <code>len</code> bytes from the specified byte array
85      * starting at offset <code>off</code> to this buffered output stream.
86      *
87      * <p> Ordinarily this method stores bytes from the given array into this
88      * stream's buffer, flushing the buffer to the underlying output stream as
89      * needed. If the requested length is at least as large as this stream's
90      * buffer, however, then this method will flush the buffer and write the
91      * bytes directly to the underlying output stream. Thus redundant
92      * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
93      *
94      * @param b the data.
95      * @param off the start offset in the data.
96      * @param len the number of bytes to write.
97      * @exception IOException if an I/O error occurs.
98      */

99     public synchronized void write(byte b[], int off, int len) throws IOException JavaDoc {
100     if (len >= buf.length) {
101         /* If the request length exceeds the size of the output buffer,
102                flush the output buffer and then write the data directly.
103                In this way buffered streams will cascade harmlessly. */

104         flushBuffer();
105         out.write(b, off, len);
106         return;
107     }
108     if (len > buf.length - count) {
109         flushBuffer();
110     }
111     System.arraycopy(b, off, buf, count, len);
112     count += len;
113     }
114
115     /**
116      * Flushes this buffered output stream. This forces any buffered
117      * output bytes to be written out to the underlying output stream.
118      *
119      * @exception IOException if an I/O error occurs.
120      * @see java.io.FilterOutputStream#out
121      */

122     public synchronized void flush() throws IOException JavaDoc {
123         flushBuffer();
124     out.flush();
125     }
126 }
127
Popular Tags