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

Public Member Functions

 UTF8ByteBufferReader ()
 
UTF8ByteBufferReader setInput (ByteBuffer byteBuffer)
 
boolean ready () throws IOException
 
void close () throws IOException
 
int read () throws IOException
 
int read (char cbuf[], int off, int len) throws IOException
 
void read (Appendable dest) throws IOException
 
void reset ()
 
UTF8ByteBufferReader setByteBuffer (ByteBuffer byteBuffer)
 

Private Member Functions

int read2 (byte b) throws IOException
 

Private Attributes

ByteBuffer _byteBuffer
 
int _code
 
int _moreBytes
 

Detailed Description

A UTF-8 java.nio.ByteBuffer reader.

This reader can be used for efficient decoding of native byte buffers (e.g. MappedByteBuffer), high-performance messaging (no intermediate buffer), etc.

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

Each invocation of one of the read() methods may cause one or more bytes to be read from the underlying byte buffer. The end of stream is reached when the byte buffer position and limit coincide.

Author
Jean-Marie Dautelle
Version
2.0, December 9, 2004
See also
UTF8ByteBufferWriter

Definition at line 39 of file UTF8ByteBufferReader.java.

Constructor & Destructor Documentation

◆ UTF8ByteBufferReader()

javolution.io.UTF8ByteBufferReader.UTF8ByteBufferReader ( )

Default constructor.

Definition at line 49 of file UTF8ByteBufferReader.java.

49 {}

Member Function Documentation

◆ close()

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

Closes and resets this reader for reuse.

Exceptions
IOExceptionif an I/O error occurs.

Definition at line 87 of file UTF8ByteBufferReader.java.

87  {
88  if (_byteBuffer != null) {
89  reset();
90  }
91  }

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

Here is the call graph for this function:

◆ read() [1/3]

int javolution.io.UTF8ByteBufferReader.read ( ) throws IOException

Reads a single character. This method does not block, -1 is returned if the buffer's limit has been reached.

Returns
the 31-bits Unicode of the character read, or -1 if there is no more remaining bytes to be read.
Exceptions
IOExceptionif an I/O error occurs (e.g. incomplete character sequence being read).

Definition at line 102 of file UTF8ByteBufferReader.java.

102  {
103  if (_byteBuffer != null) {
104  if (_byteBuffer.hasRemaining()) {
105  byte b = _byteBuffer.get();
106  return (b >= 0) ? b : read2(b);
107  } else {
108  return -1;
109  }
110  } else {
111  throw new IOException("Reader closed");
112  }
113  }

References javolution.io.UTF8ByteBufferReader._byteBuffer, and javolution.io.UTF8ByteBufferReader.read2().

Referenced by javolution.io.Struct.UTF8String.get().

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

◆ read() [2/3]

void javolution.io.UTF8ByteBufferReader.read ( Appendable  dest) throws IOException

Reads characters into the specified appendable. This method does not block.

Note: Characters between U+10000 and U+10FFFF are represented by surrogate pairs (two char).

Parameters
destthe destination buffer.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 231 of file UTF8ByteBufferReader.java.

231  {
232  if (_byteBuffer == null)
233  throw new IOException("Reader closed");
234  while (_byteBuffer.hasRemaining()) {
235  byte b = _byteBuffer.get();
236  if (b >= 0) {
237  dest.append((char) b); // Most common case.
238  } else {
239  int code = read2(b);
240  if (code < 0x10000) {
241  dest.append((char) code);
242  } else if (code <= 0x10ffff) { // Surrogates.
243  dest.append((char) (((code - 0x10000) >> 10) + 0xd800));
244  dest.append((char) (((code - 0x10000) & 0x3ff) + 0xdc00));
245  } else {
246  throw new CharConversionException("Cannot convert U+"
247  + Integer.toHexString(code)
248  + " to char (code greater than U+10FFFF)");
249  }
250  }
251  }
252  }

References javolution.io.UTF8ByteBufferReader._byteBuffer, and javolution.io.UTF8ByteBufferReader.read2().

Here is the call graph for this function:

◆ read() [3/3]

int javolution.io.UTF8ByteBufferReader.read ( char  cbuf[],
int  off,
int  len 
) throws IOException

Reads characters into a portion of an array. This method does not block.

Note: Characters between U+10000 and U+10FFFF are represented by surrogate pairs (two char).

Parameters
cbufthe destination buffer.
offthe offset at which to start storing characters.
lenthe maximum number of characters to read
Returns
the number of characters read, or -1 if there is no more byte remaining.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 181 of file UTF8ByteBufferReader.java.

181  {
182  if (_byteBuffer == null)
183  throw new IOException("Reader closed");
184  final int off_plus_len = off + len;
185  int remaining = _byteBuffer.remaining();
186  if (remaining <= 0)
187  return -1;
188  for (int i = off; i < off_plus_len;) {
189  if (remaining-- > 0) {
190  byte b = _byteBuffer.get();
191  if (b >= 0) {
192  cbuf[i++] = (char) b; // Most common case.
193  } else {
194  if (i < off_plus_len - 1) { // Up to two 'char' can be read.
195  int code = read2(b);
196  remaining = _byteBuffer.remaining(); // Recalculates.
197  if (code < 0x10000) {
198  cbuf[i++] = (char) code;
199  } else if (code <= 0x10ffff) { // Surrogates.
200  cbuf[i++] = (char) (((code - 0x10000) >> 10) + 0xd800);
201  cbuf[i++] = (char) (((code - 0x10000) & 0x3ff) + 0xdc00);
202  } else {
203  throw new CharConversionException(
204  "Cannot convert U+"
205  + Integer.toHexString(code)
206  + " to char (code greater than U+10FFFF)");
207  }
208  } else { // Not enough space in destination (go back).
209  _byteBuffer.position(_byteBuffer.position() - 1);
210  remaining++;
211  return i - off;
212  }
213  }
214  } else {
215  return i - off;
216  }
217  }
218  return len;
219  }

References javolution.io.UTF8ByteBufferReader._byteBuffer, and javolution.io.UTF8ByteBufferReader.read2().

Here is the call graph for this function:

◆ read2()

int javolution.io.UTF8ByteBufferReader.read2 ( byte  b) throws IOException
private

Definition at line 116 of file UTF8ByteBufferReader.java.

116  {
117  try {
118  // Decodes UTF-8.
119  if ((b >= 0) && (_moreBytes == 0)) {
120  // 0xxxxxxx
121  return b;
122  } else if (((b & 0xc0) == 0x80) && (_moreBytes != 0)) {
123  // 10xxxxxx (continuation byte)
124  _code = (_code << 6) | (b & 0x3f); // Adds 6 bits to code.
125  if (--_moreBytes == 0) {
126  return _code;
127  } else {
128  return read2(_byteBuffer.get());
129  }
130  } else if (((b & 0xe0) == 0xc0) && (_moreBytes == 0)) {
131  // 110xxxxx
132  _code = b & 0x1f;
133  _moreBytes = 1;
134  return read2(_byteBuffer.get());
135  } else if (((b & 0xf0) == 0xe0) && (_moreBytes == 0)) {
136  // 1110xxxx
137  _code = b & 0x0f;
138  _moreBytes = 2;
139  return read2(_byteBuffer.get());
140  } else if (((b & 0xf8) == 0xf0) && (_moreBytes == 0)) {
141  // 11110xxx
142  _code = b & 0x07;
143  _moreBytes = 3;
144  return read2(_byteBuffer.get());
145  } else if (((b & 0xfc) == 0xf8) && (_moreBytes == 0)) {
146  // 111110xx
147  _code = b & 0x03;
148  _moreBytes = 4;
149  return read2(_byteBuffer.get());
150  } else if (((b & 0xfe) == 0xfc) && (_moreBytes == 0)) {
151  // 1111110x
152  _code = b & 0x01;
153  _moreBytes = 5;
154  return read2(_byteBuffer.get());
155  } else {
156  throw new CharConversionException("Invalid UTF-8 Encoding");
157  }
158  } catch (BufferUnderflowException e) {
159  throw new CharConversionException("Incomplete Sequence");
160  }
161  }

References javolution.io.UTF8ByteBufferReader._byteBuffer, javolution.io.UTF8ByteBufferReader._code, and javolution.io.UTF8ByteBufferReader._moreBytes.

Referenced by javolution.io.UTF8ByteBufferReader.read().

Here is the caller graph for this function:

◆ ready()

boolean javolution.io.UTF8ByteBufferReader.ready ( ) throws IOException

Indicates if this stream is ready to be read.

Returns
true if the byte buffer has remaining bytes to read; false otherwise.
Exceptions
IOExceptionif an I/O error occurs.

Definition at line 74 of file UTF8ByteBufferReader.java.

74  {
75  if (_byteBuffer != null) {
76  return _byteBuffer.hasRemaining();
77  } else {
78  throw new IOException("Reader closed");
79  }
80  }

References javolution.io.UTF8ByteBufferReader._byteBuffer.

◆ reset()

void javolution.io.UTF8ByteBufferReader.reset ( )

Definition at line 254 of file UTF8ByteBufferReader.java.

254  {
255  _byteBuffer = null;
256  _code = 0;
257  _moreBytes = 0;
258  }

References javolution.io.UTF8ByteBufferReader._byteBuffer, javolution.io.UTF8ByteBufferReader._code, and javolution.io.UTF8ByteBufferReader._moreBytes.

Referenced by javolution.io.UTF8ByteBufferReader.close(), and javolution.io.Struct.UTF8String.get().

Here is the caller graph for this function:

◆ setByteBuffer()

UTF8ByteBufferReader javolution.io.UTF8ByteBufferReader.setByteBuffer ( ByteBuffer  byteBuffer)
Deprecated:
Replaced by setInput(ByteBuffer)

Definition at line 263 of file UTF8ByteBufferReader.java.

263  {
264  return this.setInput(byteBuffer);
265  }

References javolution.io.UTF8ByteBufferReader.setInput().

Here is the call graph for this function:

◆ setInput()

UTF8ByteBufferReader javolution.io.UTF8ByteBufferReader.setInput ( ByteBuffer  byteBuffer)

Sets the ByteBuffer to use for reading available bytes from current buffer position.

Parameters
byteBufferthe ByteBuffer source.
Returns
this UTF-8 reader.
Exceptions
IllegalStateExceptionif this reader is being reused and it has not been closed or reset.

Definition at line 60 of file UTF8ByteBufferReader.java.

60  {
61  if (_byteBuffer != null)
62  throw new IllegalStateException("Reader not closed or reset");
63  _byteBuffer = byteBuffer;
64  return this;
65  }

References javolution.io.UTF8ByteBufferReader._byteBuffer.

Referenced by javolution.io.Struct.UTF8String.get(), and javolution.io.UTF8ByteBufferReader.setByteBuffer().

Here is the caller graph for this function:

Member Data Documentation

◆ _byteBuffer

◆ _code

int javolution.io.UTF8ByteBufferReader._code
private

◆ _moreBytes

int javolution.io.UTF8ByteBufferReader._moreBytes
private

The documentation for this class was generated from the following file:
javolution.io.UTF8ByteBufferReader._code
int _code
Definition: UTF8ByteBufferReader.java:163
javolution.io.UTF8ByteBufferReader.read2
int read2(byte b)
Definition: UTF8ByteBufferReader.java:116
javolution.io.UTF8ByteBufferReader.reset
void reset()
Definition: UTF8ByteBufferReader.java:254
javolution.io.UTF8ByteBufferReader._moreBytes
int _moreBytes
Definition: UTF8ByteBufferReader.java:165
javolution.io.UTF8ByteBufferReader.setInput
UTF8ByteBufferReader setInput(ByteBuffer byteBuffer)
Definition: UTF8ByteBufferReader.java:60
javolution.io.UTF8ByteBufferReader._byteBuffer
ByteBuffer _byteBuffer
Definition: UTF8ByteBufferReader.java:44