Javolution 6.0.0 java
javolution.io.UTF8StreamWriter Class Reference
Inheritance diagram for javolution.io.UTF8StreamWriter:
[legend]
Collaboration diagram for javolution.io.UTF8StreamWriter:
[legend]

Public Member Functions

 UTF8StreamWriter ()
 
 UTF8StreamWriter (int capacity)
 
UTF8StreamWriter setOutput (OutputStream out)
 
void write (char c) throws IOException
 
void write (int code) throws IOException
 
void write (char cbuf[], int off, int len) throws IOException
 
void write (String str, int off, int len) throws IOException
 
void write (CharSequence csq) throws IOException
 
void flush () throws IOException
 
void close () throws IOException
 
void reset ()
 
UTF8StreamWriter setOutputStream (OutputStream out)
 

Private Member Functions

void write2 (int c) throws IOException
 
void flushBuffer () throws IOException
 

Private Attributes

OutputStream _outputStream
 
final byte[] _bytes
 
int _index
 
char _highSurrogate
 

Detailed Description

A UTF-8 stream writer.

This writer supports surrogate char pairs (representing characters in the range [U+10000 .. U+10FFFF]). It can also be used to write characters from their unicodes (31 bits) directly (ref. write(int)).

Instances of this class can be reused for different output streams and can be part of a higher level component (e.g. serializer) in order to avoid dynamic buffer allocation when the destination output changes. Also wrapping using a java.io.BufferedWriter is unnescessary as instances of this class embed their own data buffers.

Note: This writer is unsynchronized and always produces well-formed UTF-8 sequences.

Author
Jean-Marie Dautelle
Version
2.0, December 9, 2004

Definition at line 36 of file UTF8StreamWriter.java.

Constructor & Destructor Documentation

◆ UTF8StreamWriter() [1/2]

javolution.io.UTF8StreamWriter.UTF8StreamWriter ( )

Creates a UTF-8 writer having a byte buffer of moderate capacity (2048).

Definition at line 56 of file UTF8StreamWriter.java.

56  {
57  _bytes = new byte[2048];
58  }

References javolution.io.UTF8StreamWriter._bytes.

◆ UTF8StreamWriter() [2/2]

javolution.io.UTF8StreamWriter.UTF8StreamWriter ( int  capacity)

Creates a UTF-8 writer having a byte buffer of specified capacity.

Parameters
capacitythe capacity of the byte buffer.

Definition at line 65 of file UTF8StreamWriter.java.

65  {
66  _bytes = new byte[capacity];
67  }

References javolution.io.UTF8StreamWriter._bytes.

Member Function Documentation

◆ close()

void javolution.io.UTF8StreamWriter.close ( ) throws IOException

Closes and resets this writer for reuse.

Exceptions
IOExceptionif an I/O error occurs

Definition at line 306 of file UTF8StreamWriter.java.

306  {
307  if (_outputStream != null) {
308  flushBuffer();
309  _outputStream.close();
310  reset();
311  }
312  }

References javolution.io.UTF8StreamWriter._outputStream, javolution.io.UTF8StreamWriter.flushBuffer(), and javolution.io.UTF8StreamWriter.reset().

Referenced by javolution.xml.ws.WebServiceClient.invoke().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ flush()

void javolution.io.UTF8StreamWriter.flush ( ) throws IOException

Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

Exceptions
IOExceptionif an I/O error occurs.

Definition at line 296 of file UTF8StreamWriter.java.

296  {
297  flushBuffer();
298  _outputStream.flush();
299  }

References javolution.io.UTF8StreamWriter._outputStream, and javolution.io.UTF8StreamWriter.flushBuffer().

Here is the call graph for this function:

◆ flushBuffer()

void javolution.io.UTF8StreamWriter.flushBuffer ( ) throws IOException
private

Flushes the internal bytes buffer.

Exceptions
IOExceptionif an I/O error occurs

Definition at line 319 of file UTF8StreamWriter.java.

319  {
320  if (_outputStream == null)
321  throw new IOException("Stream closed");
322  _outputStream.write(_bytes, 0, _index);
323  _index = 0;
324  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, and javolution.io.UTF8StreamWriter._outputStream.

Referenced by javolution.io.UTF8StreamWriter.close(), javolution.io.UTF8StreamWriter.flush(), javolution.io.UTF8StreamWriter.write(), and javolution.io.UTF8StreamWriter.write2().

Here is the caller graph for this function:

◆ reset()

void javolution.io.UTF8StreamWriter.reset ( )

Definition at line 326 of file UTF8StreamWriter.java.

326  {
327  _highSurrogate = 0;
328  _index = 0;
329  _outputStream = null;
330  }

References javolution.io.UTF8StreamWriter._highSurrogate, javolution.io.UTF8StreamWriter._index, and javolution.io.UTF8StreamWriter._outputStream.

Referenced by javolution.io.UTF8StreamWriter.close(), javolution.xml.ws.WebServiceClient.invoke(), and javolution.xml.internal.stream.XMLStreamWriterImpl.reset().

Here is the caller graph for this function:

◆ setOutput()

UTF8StreamWriter javolution.io.UTF8StreamWriter.setOutput ( OutputStream  out)

Sets the output stream to use for writing until this writer is closed. For example:[code] Writer writer = new UTF8StreamWriter().setOutputStream(out); [/code] is equivalent but writes faster than [code] Writer writer = new java.io.OutputStreamWriter(out, "UTF-8"); [/code]

Parameters
outthe output stream.
Returns
this UTF-8 writer.
Exceptions
IllegalStateExceptionif this writer is being reused and it has not been closed or reset.

Definition at line 82 of file UTF8StreamWriter.java.

82  {
83  if (_outputStream != null)
84  throw new IllegalStateException("Writer not closed or reset");
85  _outputStream = out;
86  return this;
87  }

References javolution.io.UTF8StreamWriter._outputStream.

Referenced by javolution.xml.ws.WebServiceClient.invoke(), javolution.xml.internal.stream.XMLStreamWriterImpl.setOutput(), and javolution.io.UTF8StreamWriter.setOutputStream().

Here is the caller graph for this function:

◆ setOutputStream()

UTF8StreamWriter javolution.io.UTF8StreamWriter.setOutputStream ( OutputStream  out)
Deprecated:
Replaced by setOutput(OutputStream)

Definition at line 335 of file UTF8StreamWriter.java.

335  {
336  return this.setOutput(out);
337  }

References javolution.io.UTF8StreamWriter.setOutput().

Here is the call graph for this function:

◆ write() [1/5]

void javolution.io.UTF8StreamWriter.write ( char  c) throws IOException

Writes a single character. This method supports 16-bits character surrogates.

Parameters
cchar the character to be written (possibly a surrogate).
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 97 of file UTF8StreamWriter.java.

97  {
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  }

References javolution.io.UTF8StreamWriter._highSurrogate.

Referenced by javolution.io.UTF8StreamWriter.write().

Here is the caller graph for this function:

◆ write() [2/5]

void javolution.io.UTF8StreamWriter.write ( char  cbuf[],
int  off,
int  len 
) throws IOException

Writes a portion of an array of characters.

Parameters
cbufthe array of characters.
offthe offset from which to start writing characters.
lenthe number of characters to write.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 228 of file UTF8StreamWriter.java.

228  {
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  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, javolution.io.UTF8StreamWriter.flushBuffer(), and javolution.io.UTF8StreamWriter.write().

Here is the call graph for this function:

◆ write() [3/5]

void javolution.io.UTF8StreamWriter.write ( CharSequence  csq) throws IOException

Writes the specified character sequence.

Parameters
csqthe character sequence.
Exceptions
IOExceptionif an I/O error occurs

Definition at line 272 of file UTF8StreamWriter.java.

272  {
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  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, javolution.io.UTF8StreamWriter.flushBuffer(), and javolution.io.UTF8StreamWriter.write().

Here is the call graph for this function:

◆ write() [4/5]

void javolution.io.UTF8StreamWriter.write ( int  code) throws IOException

Writes a character given its 31-bits Unicode.

Parameters
codethe 31 bits Unicode of the character to be written.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 117 of file UTF8StreamWriter.java.

117  {
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  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, javolution.io.UTF8StreamWriter.flushBuffer(), and javolution.io.UTF8StreamWriter.write2().

Here is the call graph for this function:

◆ write() [5/5]

void javolution.io.UTF8StreamWriter.write ( String  str,
int  off,
int  len 
) throws IOException

Writes a portion of a string.

Parameters
stra String.
offthe offset from which to start writing characters.
lenthe number of characters to write.
Exceptions
IOExceptionif an I/O error occurs

Definition at line 251 of file UTF8StreamWriter.java.

251  {
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  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, javolution.io.UTF8StreamWriter.flushBuffer(), and javolution.io.UTF8StreamWriter.write().

Here is the call graph for this function:

◆ write2()

void javolution.io.UTF8StreamWriter.write2 ( int  c) throws IOException
private

Definition at line 128 of file UTF8StreamWriter.java.

128  {
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  }

References javolution.io.UTF8StreamWriter._bytes, javolution.io.UTF8StreamWriter._index, and javolution.io.UTF8StreamWriter.flushBuffer().

Referenced by javolution.io.UTF8StreamWriter.write().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _bytes

final byte [] javolution.io.UTF8StreamWriter._bytes
private

◆ _highSurrogate

char javolution.io.UTF8StreamWriter._highSurrogate
private

◆ _index

int javolution.io.UTF8StreamWriter._index
private

◆ _outputStream

OutputStream javolution.io.UTF8StreamWriter._outputStream
private

The documentation for this class was generated from the following file:
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._index
int _index
Definition: UTF8StreamWriter.java:51
javolution.io.UTF8StreamWriter.write2
void write2(int c)
Definition: UTF8StreamWriter.java:128
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