01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.caching;
18:
19: import java.io.IOException;
20: import java.io.OutputStream;
21:
22: /**
23: * This is an {@link OutputStream} which forwards all received bytes to another
24: * output stream and in addition caches all bytes, thus acting like a
25: * TeeOutputStream.
26: *
27: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
28: * @version CVS $Id: CachingOutputStream.java 433543 2006-08-22 06:22:54Z crossley $
29: */
30:
31: public final class CachingOutputStream extends OutputStream {
32:
33: private OutputStream 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 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 {
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,
58: newcount)];
59: System.arraycopy(this .buf, 0, newbuf, 0, this .bufCount);
60: this .buf = newbuf;
61: }
62: this .buf[this .bufCount] = (byte) b;
63: this .bufCount = newcount;
64: }
65:
66: public void write(byte b[]) throws IOException {
67: this .write(b, 0, b.length);
68: }
69:
70: public void write(byte b[], int off, int len) throws IOException {
71: this .receiver.write(b, off, len);
72: if (len == 0)
73: return;
74: int newcount = this .bufCount + (len - off);
75: if (newcount > this .buf.length) {
76: byte newbuf[] = new byte[Math.max(this .buf.length << 1,
77: newcount)];
78: System.arraycopy(this .buf, 0, newbuf, 0, this .bufCount);
79: this .buf = newbuf;
80: }
81: System.arraycopy(b, off, this .buf, this .bufCount, len);
82: this .bufCount = newcount;
83: }
84:
85: public void flush() throws IOException {
86: this .receiver.flush();
87: }
88:
89: public void close() throws IOException {
90: this.receiver.close();
91: }
92:
93: }
|