001: /*
002: * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
003: * Copyright (C) 2006 - Javolution (http://javolution.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package javolution.io;
010:
011: import j2me.lang.CharSequence;
012: import j2me.lang.IllegalStateException;
013: import j2me.lang.UnsupportedOperationException;
014:
015: import java.io.IOException;
016: import java.io.Writer;
017: import javolution.lang.Reusable;
018: import javolution.text.Appendable;
019: import javolution.text.Text;
020:
021: /**
022: * <p> This class allows any <code>Appendable</code> to be used as
023: * a writer.</p>
024: *
025: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
026: * @version 3.8, May 8, 2006
027: */
028: public final class AppendableWriter extends Writer implements Reusable {
029:
030: /**
031: * Holds the current appendable output or <code>null</code> if closed.
032: */
033: private Appendable _output;
034:
035: /**
036: * Creates a new appendable writer for which the appendable output
037: * is not set.
038: *
039: * @see #setOutput(Appendable)
040: */
041: public AppendableWriter() {
042: }
043:
044: /**
045: * Sets the appendable output being written to.
046: * For example:[code]
047: * Writer writer = new AppendableWriter().setOutput(new TextBuilder());
048: * [/code]
049: *
050: * @param output the appendable written to.
051: * @return this writer.
052: * @throws IllegalStateException if this writer is being reused and
053: * it has not been {@link #close closed} or {@link #reset reset}.
054: */
055: public AppendableWriter setOutput(Appendable output) {
056: if (_output != null)
057: throw new IllegalStateException(
058: "Writer not closed or reset");
059: _output = output;
060: return this ;
061: }
062:
063: /**
064: * Writes a single character.
065: *
066: * @param c <code>char</code> the character to be written.
067: * @throws IOException if an I/O error occurs.
068: */
069: public void write(char c) throws IOException {
070: if (_output == null)
071: throw new IOException("Writer closed");
072: _output.append(c);
073: }
074:
075: /**
076: * Writes the 16 low-order bits of the given integer value;
077: * the 16 high-order bits are ignored.
078: *
079: * @param c the value of the character to be written.
080: * @throws IOException if an I/O error occurs.
081: */
082: public void write(int c) throws IOException {
083: if (_output == null)
084: throw new IOException("Writer closed");
085: _output.append((char) c);
086: }
087:
088: /**
089: * Writes a portion of an array of characters.
090: *
091: * @param cbuf the array of characters.
092: * @param off the offset from which to start writing characters.
093: * @param len the number of characters to write.
094: * @throws IOException if an I/O error occurs.
095: */
096: public void write(char cbuf[], int off, int len) throws IOException {
097: if (_output == null)
098: throw new IOException("Writer closed");
099: _tmpBuffer = cbuf;
100: _output.append(_tmpBufferAsCharSequence, off, off + len);
101: _tmpBuffer = null; // Removes temporary references.
102: }
103:
104: private char[] _tmpBuffer;
105:
106: private final CharSequence _tmpBufferAsCharSequence = new CharSequence() {
107: public int length() {
108: return _tmpBuffer.length;
109: }
110:
111: public char charAt(int index) {
112: return _tmpBuffer[index];
113: }
114:
115: public CharSequence subSequence(int start, int end) {
116: throw new UnsupportedOperationException();
117: }
118: };
119:
120: /**
121: * Writes a portion of a string.
122: *
123: * @param str a String.
124: * @param off the offset from which to start writing characters.
125: * @param len the number of characters to write.
126: * @throws IOException if an I/O error occurs
127: */
128: public void write(String str, int off, int len) throws IOException {
129: if (_output == null)
130: throw new IOException("Writer closed");
131: Object obj = str;
132: if (obj instanceof CharSequence) {
133: _output.append((CharSequence) obj);
134: } else {
135: _output.append(Text.valueOf(str));
136: }
137: }
138:
139: /**
140: * Writes the specified character sequence.
141: *
142: * @param csq the character sequence.
143: * @throws IOException if an I/O error occurs
144: */
145: public void write(CharSequence csq) throws IOException {
146: if (_output == null)
147: throw new IOException("Writer closed");
148: _output.append(csq);
149: }
150:
151: /**
152: * Flushes the stream.
153: */
154: public void flush() {
155: // Do nothing (no buffer).
156: }
157:
158: /**
159: * Closes and {@link #reset resets} this writer for reuse.
160: */
161: public void close() {
162: if (_output != null) {
163: reset();
164: }
165: }
166:
167: // Implements Reusable.
168: public void reset() {
169: _output = null;
170: _tmpBuffer = null;
171: }
172: }
|