KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > BufferedOutputStream


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 import java.io.FilterOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /**
23  * This class is like the {@link java.io.BufferedOutputStream} but it
24  * extends it with a logic to count the number of bytes written to
25  * the output stream.
26  *
27  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
28  * @version CVS $Id: BufferedOutputStream.java 179249 2005-05-31 18:01:03Z cziegeler $
29  * @since 2.1
30  */

31 public final class BufferedOutputStream extends FilterOutputStream JavaDoc {
32     
33     protected byte buf[];
34
35     protected int count;
36     
37     /**
38      * Creates a new buffered output stream to write data to the
39      * specified underlying output stream with a default 8192-byte
40      * buffer size.
41      *
42      * @param out the underlying output stream.
43      */

44     public BufferedOutputStream(OutputStream JavaDoc out) {
45         this(out, 8192);
46     }
47
48     /**
49      * Creates a new buffered output stream to write data to the
50      * specified underlying output stream with the specified buffer
51      * size.
52      *
53      * @param out the underlying output stream.
54      * @param size the buffer size.
55      * @exception IllegalArgumentException if size <= 0.
56      */

57     public BufferedOutputStream(OutputStream JavaDoc out, int size) {
58         super(out);
59         if (size <= 0) {
60             throw new IllegalArgumentException JavaDoc("Buffer size <= 0");
61         }
62         this.buf = new byte[size];
63     }
64
65     /**
66      * Writes the specified byte to this buffered output stream.
67      *
68      * @param b the byte to be written.
69      * @exception IOException if an I/O error occurs.
70      */

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

94     public void write(byte b[], int off, int len) throws IOException JavaDoc {
95         while (len > buf.length - count) {
96             this.incBuffer();
97         }
98         System.arraycopy(b, off, buf, count, len);
99         count += len;
100     }
101
102     /**
103      * Flushes this buffered output stream.
104      * We don't flush here, flushing is done during closing.
105      *
106      * @exception IOException if an I/O error occurs.
107      */

108     public void flush() throws IOException JavaDoc {
109         // nothing
110
}
111
112     /**
113      * Closes this buffered output stream.
114      * Flush before closing.
115      *
116      * @exception IOException if an I/O error occurs.
117      */

118     public void close() throws IOException JavaDoc {
119         realFlush();
120         super.close ();
121     }
122
123     /**
124      * Flushes this buffered output stream.
125      */

126     public void realFlush() throws IOException JavaDoc {
127         this.writeBuffer();
128         this.out.flush();
129     }
130     
131     /**
132      * Write the buffer
133      */

134     private void writeBuffer()
135     throws IOException JavaDoc {
136         if (this.count > 0) {
137             this.out.write(this.buf, 0, this.count);
138             this.clearBuffer();
139         }
140     }
141
142     /**
143      * Increment the buffer
144      */

145     private void incBuffer() {
146         // currently we double the buffer size
147
// this is not so fast but is a very simple logic
148
byte[] newBuf = new byte[this.buf.length * 2];
149         System.arraycopy(this.buf, 0, newBuf, 0, this.buf.length);
150         this.buf = newBuf;
151     }
152     
153     /**
154      * Clear/reset the buffer
155      */

156     public void clearBuffer() {
157         this.count = 0;
158     }
159     
160     /**
161      * Return the size of the current buffer
162      */

163     public int getCount() {
164         return this.count;
165     }
166 }
167
168
Popular Tags