001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.io;
014:
015: import org.wings.util.SStringBuilder;
016:
017: import java.io.ByteArrayOutputStream;
018: import java.io.IOException;
019: import java.io.Serializable;
020:
021: /**
022: * A Device encapsulating a StringBuilder
023: *
024: * @author <a href="mailto:ole@freiheit.com">Ole Langbehn</a>
025: */
026: public final class StringBuilderDevice implements Device, Serializable {
027: private SStringBuilder builder;
028: private transient ByteArrayOutputStream byteStream = null;
029:
030: public StringBuilderDevice() {
031: builder = new SStringBuilder(4 * 1024);
032: }
033:
034: public StringBuilderDevice(int capacity) {
035: builder = new SStringBuilder(capacity);
036: }
037:
038: public String toString() {
039: flush();
040: return builder.toString();
041: }
042:
043: public boolean isSizePreserving() {
044: return true;
045: }
046:
047: /**
048: * Flush this Stream.
049: */
050: public void flush() {
051: if (byteStream != null) {
052: builder.append(byteStream.toString());
053: byteStream = null;
054: }
055: }
056:
057: public void close() {
058: flush();
059: }
060:
061: public void reset() {
062: flush();
063: builder.setLength(0);
064: }
065:
066: private ByteArrayOutputStream getStream() {
067: if (byteStream != null)
068: return byteStream;
069: byteStream = new ByteArrayOutputStream();
070: return byteStream;
071: }
072:
073: /**
074: * Print a String.
075: */
076: public Device print(String s) {
077: if (byteStream != null)
078: flush();
079: builder.append(s);
080: return this ;
081: }
082:
083: /**
084: * Print a character.
085: */
086: public Device print(char c) {
087: if (byteStream != null)
088: flush();
089: builder.append(c);
090: return this ;
091: }
092:
093: /**
094: * Print a character array.
095: */
096: public Device print(char[] c) {
097: if (byteStream != null)
098: flush();
099: builder.append(c);
100: return this ;
101: }
102:
103: /**
104: * Print a character array.
105: */
106: public Device print(char[] c, int start, int len) {
107: if (byteStream != null)
108: flush();
109: builder.append(c, start, len);
110: return this ;
111: }
112:
113: /**
114: * Print an integer.
115: */
116: public Device print(int i) {
117: if (byteStream != null)
118: flush();
119: builder.append(i);
120: return this ;
121: }
122:
123: /**
124: * Print any Object
125: */
126: public Device print(Object o) {
127: if (byteStream != null)
128: flush();
129: builder.append(o);
130: return this ;
131: }
132:
133: /**
134: * Writes the specified byte to this data output stream.
135: */
136: public Device write(int c) {
137: getStream().write(c);
138: return this ;
139: }
140:
141: /**
142: * Writes b.length bytes from the specified byte array to this
143: * output stream.
144: */
145: public Device write(byte b[]) throws IOException {
146: getStream().write(b);
147: return this ;
148: }
149:
150: /**
151: * Writes len bytes from the specified byte array starting at offset
152: * off to this output stream.
153: */
154: public Device write(byte b[], int off, int len) {
155: getStream().write(b, off, len);
156: return this;
157: }
158: }
|