KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > caching > CachingOutputStream


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.caching;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 /**
22  * This is an {@link OutputStream} which forwards all received bytes to another
23  * output stream and in addition caches all bytes, thus acting like a
24  * TeeOutputStream.
25  *
26  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
27  * @version CVS $Id: CachingOutputStream.java 30932 2004-07-29 17:35:38Z vgritsenko $
28  */

29
30 public final class CachingOutputStream
31 extends OutputStream JavaDoc {
32
33     private OutputStream JavaDoc receiver;
34
35     /** The buffer for the compile xml byte stream. */
36     private byte buf[];
37
38     /** The number of valid bytes in the buffer. */
39     private int bufCount;
40
41     public CachingOutputStream(OutputStream JavaDoc os) {
42         this.receiver = os;
43         this.buf = new byte[1024];
44         this.bufCount = 0;
45     }
46
47     public byte[] getContent() {
48         byte newbuf[] = new byte[this.bufCount];
49         System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
50         return newbuf;
51     }
52
53     public void write(int b) throws IOException JavaDoc {
54         this.receiver.write(b);
55         int newcount = this.bufCount + 1;
56         if (newcount > this.buf.length) {
57             byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
58             System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
59             this.buf = newbuf;
60         }
61         this.buf[this.bufCount] = (byte)b;
62         this.bufCount = newcount;
63     }
64
65     public void write( byte b[] ) throws IOException JavaDoc {
66         this.write(b, 0, b.length);
67     }
68
69     public void write(byte b[], int off, int len) throws IOException JavaDoc {
70         this.receiver.write(b, off, len);
71         if (len == 0) return;
72         int newcount = this.bufCount + (len-off);
73         if (newcount > this.buf.length) {
74             byte newbuf[] = new byte[Math.max(this.buf.length << 1, newcount)];
75             System.arraycopy(this.buf, 0, newbuf, 0, this.bufCount);
76             this.buf = newbuf;
77         }
78         System.arraycopy(b, off, this.buf, this.bufCount, len);
79         this.bufCount = newcount;
80     }
81
82     public void flush() throws IOException JavaDoc {
83         this.receiver.flush();
84     }
85
86     public void close() throws IOException JavaDoc {
87         this.receiver.close();
88     }
89
90
91 }
92
Popular Tags