KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > io > AppendableWriter


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.io;
10
11 import j2me.lang.CharSequence;
12 import j2me.lang.IllegalStateException;
13 import j2me.lang.UnsupportedOperationException;
14
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17 import javolution.lang.Reusable;
18 import javolution.text.Appendable;
19 import javolution.text.Text;
20
21 /**
22  * <p> This class allows any <code>Appendable</code> to be used as
23  * a writer.</p>
24  *
25  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
26  * @version 3.8, May 8, 2006
27  */

28 public final class AppendableWriter extends Writer JavaDoc implements Reusable {
29
30     /**
31      * Holds the current appendable output or <code>null</code> if closed.
32      */

33     private Appendable JavaDoc _output;
34
35     /**
36      * Creates a new appendable writer for which the appendable output
37      * is not set.
38      *
39      * @see #setOutput(Appendable)
40      */

41     public AppendableWriter() {
42     }
43
44     /**
45      * Sets the appendable output being written to.
46      * For example:[code]
47      * Writer writer = new AppendableWriter().setOutput(new TextBuilder());
48      * [/code]
49      *
50      * @param output the appendable written to.
51      * @return this writer.
52      * @throws IllegalStateException if this writer is being reused and
53      * it has not been {@link #close closed} or {@link #reset reset}.
54      */

55     public AppendableWriter setOutput(Appendable JavaDoc output) {
56         if (_output != null)
57             throw new IllegalStateException JavaDoc("Writer not closed or reset");
58         _output = output;
59         return this;
60     }
61
62     /**
63      * Writes a single character.
64      *
65      * @param c <code>char</code> the character to be written.
66      * @throws IOException if an I/O error occurs.
67      */

68     public void write(char c) throws IOException JavaDoc {
69         if (_output == null)
70             throw new IOException JavaDoc("Writer closed");
71         _output.append(c);
72     }
73
74     /**
75      * Writes the 16 low-order bits of the given integer value;
76      * the 16 high-order bits are ignored.
77      *
78      * @param c the value of the character to be written.
79      * @throws IOException if an I/O error occurs.
80      */

81     public void write(int c) throws IOException JavaDoc {
82         if (_output == null)
83             throw new IOException JavaDoc("Writer closed");
84         _output.append((char) c);
85     }
86
87     /**
88      * Writes a portion of an array of characters.
89      *
90      * @param cbuf the array of characters.
91      * @param off the offset from which to start writing characters.
92      * @param len the number of characters to write.
93      * @throws IOException if an I/O error occurs.
94      */

95     public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
96         if (_output == null)
97             throw new IOException JavaDoc("Writer closed");
98         _tmpBuffer = cbuf;
99         _output.append(_tmpBufferAsCharSequence, off, off + len);
100         _tmpBuffer = null; // Removes temporary references.
101
}
102
103     private char[] _tmpBuffer;
104
105     private final CharSequence JavaDoc _tmpBufferAsCharSequence = new CharSequence JavaDoc() {
106         public int length() {
107             return _tmpBuffer.length;
108         }
109
110         public char charAt(int index) {
111             return _tmpBuffer[index];
112         }
113
114         public CharSequence JavaDoc subSequence(int start, int end) {
115             throw new UnsupportedOperationException JavaDoc();
116         }
117     };
118
119     /**
120      * Writes a portion of a string.
121      *
122      * @param str a String.
123      * @param off the offset from which to start writing characters.
124      * @param len the number of characters to write.
125      * @throws IOException if an I/O error occurs
126      */

127     public void write(String JavaDoc str, int off, int len) throws IOException JavaDoc {
128         if (_output == null)
129             throw new IOException JavaDoc("Writer closed");
130         Object JavaDoc obj = str;
131         if (obj instanceof CharSequence JavaDoc) {
132             _output.append((CharSequence JavaDoc) obj);
133         } else {
134             _output.append(Text.valueOf(str));
135         }
136     }
137
138     /**
139      * Writes the specified character sequence.
140      *
141      * @param csq the character sequence.
142      * @throws IOException if an I/O error occurs
143      */

144     public void write(CharSequence JavaDoc csq) throws IOException JavaDoc {
145         if (_output == null)
146             throw new IOException JavaDoc("Writer closed");
147         _output.append(csq);
148     }
149
150     /**
151      * Flushes the stream.
152      *
153      * @throws IOException if an I/O error occurs.
154      */

155     public void flush() throws IOException JavaDoc {
156         // Do nothing (no buffer).
157
}
158
159     /**
160      * Closes and {@link #reset resets} this writer for reuse.
161      *
162      * @throws IOException if an I/O error occurs
163      */

164     public void close() throws IOException JavaDoc {
165         if (_output != null) {
166             reset();
167         }
168     }
169
170     // Implements Reusable.
171
public void reset() {
172         _output = null;
173         _tmpBuffer = null;
174     }
175 }
Popular Tags