01: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.serialize;
07:
08: import java.io.IOException;
09: import java.io.StringWriter;
10: import java.io.Writer;
11:
12: public class CachingWriter extends Writer implements
13: CharacterCachingWriter {
14:
15: Writer out;
16: StringWriter cache;
17: boolean caching;
18:
19: public CachingWriter(Writer out) {
20: cache = null;
21: caching = false;
22: this .out = out;
23: }
24:
25: public boolean startCaching() throws IOException {
26: if (caching)
27: return false;
28: flush();
29: caching = true;
30: cache = new StringWriter();
31: return true;
32: }
33:
34: public String getCache(String encoding) throws IOException {
35: flush();
36: return cache.toString();
37: }
38:
39: public boolean stopCaching() {
40: if (!caching)
41: return false;
42: caching = false;
43: return true;
44: }
45:
46: public void close() throws IOException {
47: out.close();
48: }
49:
50: public void flush() throws IOException {
51: if (out != null)
52: out.flush();
53: if (cache != null)
54: cache.flush();
55: }
56:
57: public void write(char[] c) throws IOException {
58: out.write(c);
59: if (caching)
60: cache.write(c);
61: }
62:
63: public void write(char[] c, int off, int len) throws IOException {
64: out.write(c, off, len);
65: if (caching)
66: cache.write(c, off, len);
67: }
68:
69: public void write(int c) throws IOException {
70: out.write(c);
71: if (caching)
72: cache.write(c);
73: }
74:
75: public void write(String str) throws IOException {
76: out.write(str);
77: if (caching)
78: cache.write(str);
79: }
80:
81: public void write(String str, int off, int len) throws IOException {
82: out.write(str, off, len);
83: if (caching)
84: cache.write(str, off, len);
85: }
86: }
|