Java Reference
In-Depth Information
Overflow: It indicates that the process has produced more output than the capacity of the
output buffer. You need to empty the output buffer and call the encode()/decode() method
again to get more output. You can test for this condition by using the isOverflow() method of
the CoderResult object. You can also test for this condition by comparing the return value of
the encode() or decode() method with CoderResult.OVERFLOW object as follows:
CoderResult result = encoder.encode(input, output, eoi);
if (result == CoderResult.OVERFLOW) {
// Empty output buffer to make some room for more output
}
apart from reporting buffer underflow and overflow, a CoderResult object is also capable of reporting a
malformed-input error and an unmappable-character error. You can also customize the default action of the encoding/
decoding engine for these error conditions by using their onMalformedInput() and onUnmappableCharacter()
methods.
Tip
The last argument to the encode()/decode() method is a boolean value indicating the end of the input. You
should pass true for the end of the input argument when you pass the last chunk of data for encoding or decoding.
After passing the last chunk of data, you need to call the flush() method to flush the internal buffer of the
engine. It returns an object of CoderResult that can indicate underflow or overflow. If there is an overflow, you need
to empty the output buffer and call the flush() method again. You need to keep calling the flush() method until
its return value indicates an underflow. The flush() method call should be placed in a loop, so you get all of the
encoded/decoded data.
Listings 9-4 and 9-5 demonstrate how to use a character set encoder/decoder. The DataSourceSink class serves
as a data source and a data sink. I have created this class only for illustration purposes; you would not need a class like
this in a real-world application. It supplies a stanza from the poem Lucy by William Wordsworth in a character buffer.
The getCharData() method fills the character buffer. It returns -1 when there are no more characters to supply. You
use this method during the encoding process. The storeByteData() method is used to accumulate the encoded bytes
during encoding process. The getByteData() method is used during the decoding process to supply the encoded
bytes in chunks that you accumulate during the encoding process. The encode() and decode() methods of the
CharEncoderDecoder class have the encoding and decoding logic. This example displays the decoded characters on
the standard output.
Listing 9-4. A Data Source and Sink that Supplies Character Data, Stores and Supplies Byte Data
// DataSourceSink.java
package com.jdojo.nio;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
public class DataSourceSink {
private CharBuffer cBuffer = null;
private ByteBuffer bBuffer = null;
public DataSourceSink() {
String text = getText();
cBuffer = CharBuffer.wrap(text);
}
 
 
Search WWH ::




Custom Search