KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > io > StringBufferDevice


1 /*
2  * $Id: StringBufferDevice.java,v 1.4 2005/02/11 15:10:35 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.io;
15
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.OutputStream JavaDoc;
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * A Device encapsulating a StringBuffer
23  *
24  * @author <a HREF="mailto:hzeller@to.com">Henner Zeller</a>
25  * @version $Revision: 1.4 $
26  */

27 public final class StringBufferDevice implements Device, Serializable JavaDoc {
28     private StringBuffer JavaDoc buffer;
29     private ByteArrayOutputStream JavaDoc byteStream = null;
30
31     public StringBufferDevice() {
32         buffer = new StringBuffer JavaDoc();
33     }
34
35     public String JavaDoc toString() {
36         flush();
37         return buffer.toString();
38     }
39
40     public boolean isSizePreserving() { return true; }
41
42     /**
43      * Flush this Stream.
44      */

45     public void flush() {
46         if (byteStream != null) {
47             buffer.append(byteStream.toString());
48             byteStream = null;
49         }
50     }
51
52     public void close() {
53         flush();
54     }
55
56     public void reset() {
57         flush();
58         buffer.setLength(0);
59     }
60
61     private OutputStream JavaDoc getStream() {
62         if (byteStream != null)
63             return byteStream;
64         byteStream = new ByteArrayOutputStream JavaDoc();
65         return byteStream;
66     }
67
68     /**
69      * Print a String.
70      */

71     public Device print(String JavaDoc s) {
72         if (byteStream != null) flush();
73         buffer.append(s);
74         return this;
75     }
76
77     /**
78      * Print a character.
79      */

80     public Device print(char c) {
81         if (byteStream != null) flush();
82         buffer.append(c);
83         return this;
84     }
85
86     /**
87      * Print a character array.
88      */

89     public Device print(char[] c) throws IOException JavaDoc {
90         if (byteStream != null) flush();
91         buffer.append(c);
92         return this;
93     }
94
95     /**
96      * Print a character array.
97      */

98     public Device print(char[] c, int start, int len) throws IOException JavaDoc {
99         if (byteStream != null) flush();
100         buffer.append(c, start, len);
101         return this;
102     }
103
104     /**
105      * Print an integer.
106      */

107     public Device print(int i) {
108         if (byteStream != null) flush();
109         buffer.append(i);
110         return this;
111     }
112
113     /**
114      * Print any Object
115      */

116     public Device print(Object JavaDoc o) {
117         if (byteStream != null) flush();
118         buffer.append(o);
119         return this;
120     }
121
122     /**
123      * Writes the specified byte to this data output stream.
124      */

125     public Device write(int c) throws IOException JavaDoc {
126         getStream().write(c);
127         return this;
128     }
129
130     /**
131      * Writes b.length bytes from the specified byte array to this
132      * output stream.
133      */

134     public Device write(byte b[]) throws IOException JavaDoc {
135         getStream().write(b);
136         return this;
137     }
138
139     /**
140      * Writes len bytes from the specified byte array starting at offset
141      * off to this output stream.
142      */

143     public Device write(byte b[], int off, int len) throws IOException JavaDoc {
144         getStream().write(b, off, len);
145         return this;
146     }
147 }
148
149
150
Popular Tags