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

Public Member Functions

 UTF8ByteBufferWriter ()
 
UTF8ByteBufferWriter setOutput (ByteBuffer byteBuffer)
 
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 ()
 
UTF8ByteBufferWriter setByteBuffer (ByteBuffer byteBuffer)
 

Private Member Functions

void write2 (int c) throws IOException
 

Private Attributes

ByteBuffer _byteBuffer
 
char _highSurrogate
 

Detailed Description

A UTF-8 java.nio.ByteBuffer 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
See also
UTF8ByteBufferReader

Definition at line 37 of file UTF8ByteBufferWriter.java.

Constructor & Destructor Documentation

◆ UTF8ByteBufferWriter()

javolution.io.UTF8ByteBufferWriter.UTF8ByteBufferWriter ( )

Default constructor.

Definition at line 47 of file UTF8ByteBufferWriter.java.

47 {}

Member Function Documentation

◆ close()

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

Closes and resets this writer for reuse.

Exceptions
IOExceptionif an I/O error occurs

Definition at line 205 of file UTF8ByteBufferWriter.java.

205  {
206  if (_byteBuffer != null) {
207  reset();
208  }
209  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter.reset().

Here is the call graph for this function:

◆ flush()

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

Flushes the stream (this method has no effect, the data is always directly written to the ByteBuffer).

Exceptions
IOExceptionif an I/O error occurs.

Definition at line 196 of file UTF8ByteBufferWriter.java.

196  {
197  if (_byteBuffer == null) { throw new IOException("Writer closed"); }
198  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer.

◆ reset()

void javolution.io.UTF8ByteBufferWriter.reset ( )

Definition at line 212 of file UTF8ByteBufferWriter.java.

212  {
213  _byteBuffer = null;
214  _highSurrogate = 0;
215  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter._highSurrogate.

Referenced by javolution.io.UTF8ByteBufferWriter.close(), and javolution.io.Struct.UTF8String.set().

Here is the caller graph for this function:

◆ setByteBuffer()

UTF8ByteBufferWriter javolution.io.UTF8ByteBufferWriter.setByteBuffer ( ByteBuffer  byteBuffer)
Deprecated:
Replaced by setOutput(ByteBuffer)

Definition at line 220 of file UTF8ByteBufferWriter.java.

220  {
221  return this.setOutput(byteBuffer);
222  }

References javolution.io.UTF8ByteBufferWriter.setOutput().

Here is the call graph for this function:

◆ setOutput()

UTF8ByteBufferWriter javolution.io.UTF8ByteBufferWriter.setOutput ( ByteBuffer  byteBuffer)

Sets the byte buffer to use for writing until this writer is closed.

Parameters
byteBufferthe destination byte buffer.
Returns
this UTF-8 writer.
Exceptions
IllegalStateExceptionif this writer is being reused and it has not been closed or reset.

Definition at line 57 of file UTF8ByteBufferWriter.java.

57  {
58  if (_byteBuffer != null)
59  throw new IllegalStateException("Writer not closed or reset");
60  _byteBuffer = byteBuffer;
61  return this;
62  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer.

Referenced by javolution.io.Struct.UTF8String.set(), and javolution.io.UTF8ByteBufferWriter.setByteBuffer().

Here is the caller graph for this function:

◆ write() [1/5]

void javolution.io.UTF8ByteBufferWriter.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 72 of file UTF8ByteBufferWriter.java.

72  {
73  if ((c < 0xd800) || (c > 0xdfff)) {
74  write((int) c);
75  } else if (c < 0xdc00) { // High surrogate.
76  _highSurrogate = c;
77  } else { // Low surrogate.
78  int code = ((_highSurrogate - 0xd800) << 10) + (c - 0xdc00)
79  + 0x10000;
80  write(code);
81  }
82  }

References javolution.io.UTF8ByteBufferWriter._highSurrogate.

Referenced by javolution.io.Struct.UTF8String.set(), and javolution.io.UTF8ByteBufferWriter.write().

Here is the caller graph for this function:

◆ write() [2/5]

void javolution.io.UTF8ByteBufferWriter.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 140 of file UTF8ByteBufferWriter.java.

140  {
141  final int off_plus_len = off + len;
142  for (int i = off; i < off_plus_len;) {
143  char c = cbuf[i++];
144  if (c < 0x80) {
145  _byteBuffer.put((byte) c);
146  } else {
147  write(c);
148  }
149  }
150  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter.write().

Here is the call graph for this function:

◆ write() [3/5]

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

Writes the specified character sequence.

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

Definition at line 178 of file UTF8ByteBufferWriter.java.

178  {
179  final int length = csq.length();
180  for (int i = 0; i < length;) {
181  char c = csq.charAt(i++);
182  if (c < 0x80) {
183  _byteBuffer.put((byte) c);
184  } else {
185  write(c);
186  }
187  }
188  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter.write().

Here is the call graph for this function:

◆ write() [4/5]

void javolution.io.UTF8ByteBufferWriter.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 92 of file UTF8ByteBufferWriter.java.

92  {
93  if ((code & 0xffffff80) == 0) {
94  _byteBuffer.put((byte) code);
95  } else { // Writes more than one byte.
96  write2(code);
97  }
98  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter.write2().

Here is the call graph for this function:

◆ write() [5/5]

void javolution.io.UTF8ByteBufferWriter.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 160 of file UTF8ByteBufferWriter.java.

160  {
161  final int off_plus_len = off + len;
162  for (int i = off; i < off_plus_len;) {
163  char c = str.charAt(i++);
164  if (c < 0x80) {
165  _byteBuffer.put((byte) c);
166  } else {
167  write(c);
168  }
169  }
170  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer, and javolution.io.UTF8ByteBufferWriter.write().

Here is the call graph for this function:

◆ write2()

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

Definition at line 100 of file UTF8ByteBufferWriter.java.

100  {
101  if ((c & 0xfffff800) == 0) { // 2 bytes.
102  _byteBuffer.put((byte) (0xc0 | (c >> 6)));
103  _byteBuffer.put((byte) (0x80 | (c & 0x3f)));
104  } else if ((c & 0xffff0000) == 0) { // 3 bytes.
105  _byteBuffer.put((byte) (0xe0 | (c >> 12)));
106  _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
107  _byteBuffer.put((byte) (0x80 | (c & 0x3f)));
108  } else if ((c & 0xff200000) == 0) { // 4 bytes.
109  _byteBuffer.put((byte) (0xf0 | (c >> 18)));
110  _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3f)));
111  _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
112  _byteBuffer.put((byte) (0x80 | (c & 0x3f)));
113  } else if ((c & 0xf4000000) == 0) { // 5 bytes.
114  _byteBuffer.put((byte) (0xf8 | (c >> 24)));
115  _byteBuffer.put((byte) (0x80 | ((c >> 18) & 0x3f)));
116  _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3f)));
117  _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3f)));
118  _byteBuffer.put((byte) (0x80 | (c & 0x3f)));
119  } else if ((c & 0x80000000) == 0) { // 6 bytes.
120  _byteBuffer.put((byte) (0xfc | (c >> 30)));
121  _byteBuffer.put((byte) (0x80 | ((c >> 24) & 0x3f)));
122  _byteBuffer.put((byte) (0x80 | ((c >> 18) & 0x3f)));
123  _byteBuffer.put((byte) (0x80 | ((c >> 12) & 0x3F)));
124  _byteBuffer.put((byte) (0x80 | ((c >> 6) & 0x3F)));
125  _byteBuffer.put((byte) (0x80 | (c & 0x3F)));
126  } else {
127  throw new CharConversionException("Illegal character U+"
128  + Integer.toHexString(c));
129  }
130  }

References javolution.io.UTF8ByteBufferWriter._byteBuffer.

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

Here is the caller graph for this function:

Member Data Documentation

◆ _byteBuffer

◆ _highSurrogate

char javolution.io.UTF8ByteBufferWriter._highSurrogate
private

The documentation for this class was generated from the following file:
javolution.io.UTF8ByteBufferWriter._byteBuffer
ByteBuffer _byteBuffer
Definition: UTF8ByteBufferWriter.java:42
javolution.io.UTF8ByteBufferWriter._highSurrogate
char _highSurrogate
Definition: UTF8ByteBufferWriter.java:84
javolution.io.UTF8ByteBufferWriter.reset
void reset()
Definition: UTF8ByteBufferWriter.java:212
javolution.io.UTF8ByteBufferWriter.setOutput
UTF8ByteBufferWriter setOutput(ByteBuffer byteBuffer)
Definition: UTF8ByteBufferWriter.java:57
javolution.io.UTF8ByteBufferWriter.write
void write(char c)
Definition: UTF8ByteBufferWriter.java:72
javolution.io.UTF8ByteBufferWriter.write2
void write2(int c)
Definition: UTF8ByteBufferWriter.java:100