Javolution 6.0.0 java
AppendableWriter.java
Go to the documentation of this file.
1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2012 - 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 java.io.IOException;
12 import java.io.Writer;
13 import javolution.text.Text;
14 
22 public final class AppendableWriter extends Writer {
23 
27  private Appendable _output;
28 
35  public AppendableWriter() {}
36 
48  public AppendableWriter setOutput(Appendable output) {
49  if (_output != null)
50  throw new IllegalStateException("Writer not closed or reset");
51  _output = output;
52  return this;
53  }
54 
61  public void write(char c) throws IOException {
62  if (_output == null)
63  throw new IOException("Writer closed");
64  _output.append(c);
65  }
66 
74  public void write(int c) throws IOException {
75  if (_output == null)
76  throw new IOException("Writer closed");
77  _output.append((char) c);
78  }
79 
88  public void write(char cbuf[], int off, int len) throws IOException {
89  if (_output == null)
90  throw new IOException("Writer closed");
91  _tmpBuffer = cbuf;
92  _output.append(_tmpBufferAsCharSequence, off, off + len);
93  _tmpBuffer = null; // Removes temporary references.
94  }
95 
96  private char[] _tmpBuffer;
97 
98  private final CharSequence _tmpBufferAsCharSequence = new CharSequence() {
99  public int length() {
100  return _tmpBuffer.length;
101  }
102 
103  public char charAt(int index) {
104  return _tmpBuffer[index];
105  }
106 
107  public CharSequence subSequence(int start, int end) {
108  throw new UnsupportedOperationException();
109  }
110  };
111 
120  public void write(String str, int off, int len) throws IOException {
121  if (_output == null)
122  throw new IOException("Writer closed");
123  Object obj = str;
124  if (obj instanceof CharSequence) {
125  _output.append((CharSequence) obj);
126  } else {
127  _output.append(Text.valueOf(str));
128  }
129  }
130 
137  public void write(CharSequence csq) throws IOException {
138  if (_output == null)
139  throw new IOException("Writer closed");
140  _output.append(csq);
141  }
142 
146  public void flush() {
147  // Do nothing (no buffer).
148  }
149 
153  public void close() {
154  if (_output != null) {
155  reset();
156  }
157  }
158 
159  public void reset() {
160  _output = null;
161  _tmpBuffer = null;
162  }
163 }
javolution
javolution.text.Text.valueOf
static Text valueOf(Object obj)
Definition: Text.java:140
javolution.io.AppendableWriter._output
Appendable _output
Definition: AppendableWriter.java:27
javolution.io.AppendableWriter.write
void write(char c)
Definition: AppendableWriter.java:61
javolution.text.Text
Definition: Text.java:62
javolution.io.AppendableWriter.AppendableWriter
AppendableWriter()
Definition: AppendableWriter.java:35
javolution.io.AppendableWriter.reset
void reset()
Definition: AppendableWriter.java:159
javolution.io.AppendableWriter._tmpBufferAsCharSequence
final CharSequence _tmpBufferAsCharSequence
Definition: AppendableWriter.java:98
javolution.io.AppendableWriter.write
void write(int c)
Definition: AppendableWriter.java:74
javolution.io.AppendableWriter.write
void write(char cbuf[], int off, int len)
Definition: AppendableWriter.java:88
javolution.text
Definition: CharArray.java:9
javolution.io.AppendableWriter.write
void write(String str, int off, int len)
Definition: AppendableWriter.java:120
javolution.io.AppendableWriter
Definition: AppendableWriter.java:22
javolution.io.AppendableWriter.flush
void flush()
Definition: AppendableWriter.java:146
javolution.io.AppendableWriter._tmpBuffer
char[] _tmpBuffer
Definition: AppendableWriter.java:96
javolution.io.AppendableWriter.write
void write(CharSequence csq)
Definition: AppendableWriter.java:137
javolution.io.AppendableWriter.close
void close()
Definition: AppendableWriter.java:153
javolution.io.AppendableWriter.setOutput
AppendableWriter setOutput(Appendable output)
Definition: AppendableWriter.java:48