Javolution 6.0.0 java
UTF8StreamWriter.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.CharConversionException;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.Writer;
15 
36 public final class UTF8StreamWriter extends Writer {
37 
41  private OutputStream _outputStream;
42 
46  private final byte[] _bytes;
47 
51  private int _index;
52 
56  public UTF8StreamWriter() {
57  _bytes = new byte[2048];
58  }
59 
65  public UTF8StreamWriter(int capacity) {
66  _bytes = new byte[capacity];
67  }
68 
82  public UTF8StreamWriter setOutput(OutputStream out) {
83  if (_outputStream != null)
84  throw new IllegalStateException("Writer not closed or reset");
85  _outputStream = out;
86  return this;
87  }
88 
97  public void write(char c) throws IOException {
98  if ((c < 0xd800) || (c > 0xdfff)) {
99  write((int) c);
100  } else if (c < 0xdc00) { // High surrogate.
101  _highSurrogate = c;
102  } else { // Low surrogate.
103  int code = ((_highSurrogate - 0xd800) << 10) + (c - 0xdc00)
104  + 0x10000;
105  write(code);
106  }
107  }
108 
109  private char _highSurrogate;
110 
117  public void write(int code) throws IOException {
118  if ((code & 0xffffff80) == 0) {
119  _bytes[_index] = (byte) code;
120  if (++_index >= _bytes.length) {
121  flushBuffer();
122  }
123  } else { // Writes more than one byte.
124  write2(code);
125  }
126  }
127 
128  private void write2(int c) throws IOException {
129  if ((c & 0xfffff800) == 0) { // 2 bytes.
130  _bytes[_index] = (byte) (0xc0 | (c >> 6));
131  if (++_index >= _bytes.length) {
132  flushBuffer();
133  }
134  _bytes[_index] = (byte) (0x80 | (c & 0x3f));
135  if (++_index >= _bytes.length) {
136  flushBuffer();
137  }
138  } else if ((c & 0xffff0000) == 0) { // 3 bytes.
139  _bytes[_index] = (byte) (0xe0 | (c >> 12));
140  if (++_index >= _bytes.length) {
141  flushBuffer();
142  }
143  _bytes[_index] = (byte) (0x80 | ((c >> 6) & 0x3f));
144  if (++_index >= _bytes.length) {
145  flushBuffer();
146  }
147  _bytes[_index] = (byte) (0x80 | (c & 0x3f));
148  if (++_index >= _bytes.length) {
149  flushBuffer();
150  }
151  } else if ((c & 0xff200000) == 0) { // 4 bytes.
152  _bytes[_index] = (byte) (0xf0 | (c >> 18));
153  if (++_index >= _bytes.length) {
154  flushBuffer();
155  }
156  _bytes[_index] = (byte) (0x80 | ((c >> 12) & 0x3f));
157  if (++_index >= _bytes.length) {
158  flushBuffer();
159  }
160  _bytes[_index] = (byte) (0x80 | ((c >> 6) & 0x3f));
161  if (++_index >= _bytes.length) {
162  flushBuffer();
163  }
164  _bytes[_index] = (byte) (0x80 | (c & 0x3f));
165  if (++_index >= _bytes.length) {
166  flushBuffer();
167  }
168  } else if ((c & 0xf4000000) == 0) { // 5 bytes.
169  _bytes[_index] = (byte) (0xf8 | (c >> 24));
170  if (++_index >= _bytes.length) {
171  flushBuffer();
172  }
173  _bytes[_index] = (byte) (0x80 | ((c >> 18) & 0x3f));
174  if (++_index >= _bytes.length) {
175  flushBuffer();
176  }
177  _bytes[_index] = (byte) (0x80 | ((c >> 12) & 0x3f));
178  if (++_index >= _bytes.length) {
179  flushBuffer();
180  }
181  _bytes[_index] = (byte) (0x80 | ((c >> 6) & 0x3f));
182  if (++_index >= _bytes.length) {
183  flushBuffer();
184  }
185  _bytes[_index] = (byte) (0x80 | (c & 0x3f));
186  if (++_index >= _bytes.length) {
187  flushBuffer();
188  }
189  } else if ((c & 0x80000000) == 0) { // 6 bytes.
190  _bytes[_index] = (byte) (0xfc | (c >> 30));
191  if (++_index >= _bytes.length) {
192  flushBuffer();
193  }
194  _bytes[_index] = (byte) (0x80 | ((c >> 24) & 0x3f));
195  if (++_index >= _bytes.length) {
196  flushBuffer();
197  }
198  _bytes[_index] = (byte) (0x80 | ((c >> 18) & 0x3f));
199  if (++_index >= _bytes.length) {
200  flushBuffer();
201  }
202  _bytes[_index] = (byte) (0x80 | ((c >> 12) & 0x3F));
203  if (++_index >= _bytes.length) {
204  flushBuffer();
205  }
206  _bytes[_index] = (byte) (0x80 | ((c >> 6) & 0x3F));
207  if (++_index >= _bytes.length) {
208  flushBuffer();
209  }
210  _bytes[_index] = (byte) (0x80 | (c & 0x3F));
211  if (++_index >= _bytes.length) {
212  flushBuffer();
213  }
214  } else {
215  throw new CharConversionException("Illegal character U+"
216  + Integer.toHexString(c));
217  }
218  }
219 
228  public void write(char cbuf[], int off, int len) throws IOException {
229  final int off_plus_len = off + len;
230  for (int i = off; i < off_plus_len;) {
231  char c = cbuf[i++];
232  if (c < 0x80) {
233  _bytes[_index] = (byte) c;
234  if (++_index >= _bytes.length) {
235  flushBuffer();
236  }
237  } else {
238  write(c);
239  }
240  }
241  }
242 
251  public void write(String str, int off, int len) throws IOException {
252  final int off_plus_len = off + len;
253  for (int i = off; i < off_plus_len;) {
254  char c = str.charAt(i++);
255  if (c < 0x80) {
256  _bytes[_index] = (byte) c;
257  if (++_index >= _bytes.length) {
258  flushBuffer();
259  }
260  } else {
261  write(c);
262  }
263  }
264  }
265 
272  public void write(CharSequence csq) throws IOException {
273  final int length = csq.length();
274  for (int i = 0; i < length;) {
275  char c = csq.charAt(i++);
276  if (c < 0x80) {
277  _bytes[_index] = (byte) c;
278  if (++_index >= _bytes.length) {
279  flushBuffer();
280  }
281  } else {
282  write(c);
283  }
284  }
285  }
286 
296  public void flush() throws IOException {
297  flushBuffer();
298  _outputStream.flush();
299  }
300 
306  public void close() throws IOException {
307  if (_outputStream != null) {
308  flushBuffer();
309  _outputStream.close();
310  reset();
311  }
312  }
313 
319  private void flushBuffer() throws IOException {
320  if (_outputStream == null)
321  throw new IOException("Stream closed");
322  _outputStream.write(_bytes, 0, _index);
323  _index = 0;
324  }
325 
326  public void reset() {
327  _highSurrogate = 0;
328  _index = 0;
329  _outputStream = null;
330  }
331 
335  public UTF8StreamWriter setOutputStream(OutputStream out) {
336  return this.setOutput(out);
337  }
338 }
javolution.io.UTF8StreamWriter.setOutput
UTF8StreamWriter setOutput(OutputStream out)
Definition: UTF8StreamWriter.java:82
javolution.io.UTF8StreamWriter.flushBuffer
void flushBuffer()
Definition: UTF8StreamWriter.java:319
javolution.io.UTF8StreamWriter.write
void write(String str, int off, int len)
Definition: UTF8StreamWriter.java:251
javolution.io.UTF8StreamWriter
Definition: UTF8StreamWriter.java:36
javolution.io.UTF8StreamWriter.write
void write(int code)
Definition: UTF8StreamWriter.java:117
javolution.io.UTF8StreamWriter._index
int _index
Definition: UTF8StreamWriter.java:51
javolution.io.UTF8StreamWriter.write
void write(CharSequence csq)
Definition: UTF8StreamWriter.java:272
javolution.io.UTF8StreamWriter.write2
void write2(int c)
Definition: UTF8StreamWriter.java:128
javolution.io.UTF8StreamWriter.setOutputStream
UTF8StreamWriter setOutputStream(OutputStream out)
Definition: UTF8StreamWriter.java:335
javolution.io.UTF8StreamWriter.close
void close()
Definition: UTF8StreamWriter.java:306
javolution.io.UTF8StreamWriter._outputStream
OutputStream _outputStream
Definition: UTF8StreamWriter.java:41
javolution.io.UTF8StreamWriter.reset
void reset()
Definition: UTF8StreamWriter.java:326
javolution.io.UTF8StreamWriter._bytes
final byte[] _bytes
Definition: UTF8StreamWriter.java:46
javolution.io.UTF8StreamWriter.write
void write(char c)
Definition: UTF8StreamWriter.java:97
javolution.io.UTF8StreamWriter._highSurrogate
char _highSurrogate
Definition: UTF8StreamWriter.java:109
javolution.io.UTF8StreamWriter.flush
void flush()
Definition: UTF8StreamWriter.java:296
javolution.io.UTF8StreamWriter.UTF8StreamWriter
UTF8StreamWriter(int capacity)
Definition: UTF8StreamWriter.java:65
javolution.io.UTF8StreamWriter.write
void write(char cbuf[], int off, int len)
Definition: UTF8StreamWriter.java:228
javolution.io.UTF8StreamWriter.UTF8StreamWriter
UTF8StreamWriter()
Definition: UTF8StreamWriter.java:56