KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > serialization > StringBufferWriter


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.serialization;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 /**
32  * The same as StringWriter, but takes an existing StringBuffer in its
33  * constructor.
34  *
35  * @author Stephan J. Schmidt
36  * @version $Id: StringBufferWriter.java 645 2003-01-09 09:49:12Z stephan $
37  */

38
39 public class StringBufferWriter extends Writer JavaDoc {
40
41   private StringBuffer JavaDoc buf;
42
43   /**
44    * Flag indicating whether the stream has been closed.
45    */

46   private boolean isClosed = false;
47
48   /** Check to make sure that the stream has not been closed */
49   private void ensureOpen() {
50     /* This method does nothing for now. Once we add throws clauses
51  * to the I/O methods in this class, it will throw an IOException
52  * if the stream has been closed.
53  */

54   }
55
56   public StringBufferWriter(StringBuffer JavaDoc buffer) {
57     buf = buffer;
58     lock = buf;
59   }
60
61   /**
62    * Create a new string writer, using the default initial string-buffer
63    * size.
64    */

65   public StringBufferWriter() {
66     buf = new StringBuffer JavaDoc();
67     lock = buf;
68   }
69
70   /**
71    * Create a new string writer, using the specified initial string-buffer
72    * size.
73    *
74    * @param initialSize an int specifying the initial size of the buffer.
75    */

76   public StringBufferWriter(int initialSize) {
77     if (initialSize < 0) {
78       throw new IllegalArgumentException JavaDoc("Negative buffer size");
79     }
80     buf = new StringBuffer JavaDoc(initialSize);
81     lock = buf;
82   }
83
84   /**
85    * Write a single character.
86    */

87   public void write(int c) {
88     ensureOpen();
89     buf.append((char) c);
90   }
91
92   /**
93    * Write a portion of an array of characters.
94    *
95    * @param cbuf Array of characters
96    * @param off Offset from which to start writing characters
97    * @param len Number of characters to write
98    */

99   public void write(char cbuf[], int off, int len) {
100     ensureOpen();
101     if ((off < 0) || (off > cbuf.length) || (len < 0) ||
102         ((off + len) > cbuf.length) || ((off + len) < 0)) {
103       throw new IndexOutOfBoundsException JavaDoc();
104     } else if (len == 0) {
105       return;
106     }
107     buf.append(cbuf, off, len);
108   }
109
110   /**
111    * Write a string.
112    */

113   public void write(String JavaDoc str) {
114     ensureOpen();
115     buf.append(str);
116   }
117
118   /**
119    * Write a portion of a string.
120    *
121    * @param str String to be written
122    * @param off Offset from which to start writing characters
123    * @param len Number of characters to write
124    */

125   public void write(String JavaDoc str, int off, int len) {
126     ensureOpen();
127     buf.append(str.substring(off, off + len));
128   }
129
130   /**
131    * Return the buffer's current value as a string.
132    */

133   public String JavaDoc toString() {
134     return buf.toString();
135   }
136
137   /**
138    * Return the string buffer itself.
139    *
140    * @return StringBuffer holding the current buffer value.
141    */

142   public StringBuffer JavaDoc getBuffer() {
143     return buf;
144   }
145
146   /**
147    * Flush the stream.
148    */

149   public void flush() {
150     ensureOpen();
151   }
152
153   /**
154    * Close the stream. This method does not release the buffer, since its
155    * contents might still be required.
156    */

157   public void close() throws IOException JavaDoc {
158     isClosed = true;
159   }
160
161 }
162
Popular Tags