Advanced Byte-to-Character Conversion
In Example 6-4 we saw a
basic loop for copying bytes from one channel to another.
Another commonly seen loop in programs that use the New I/O API is one
that combines reading or writing bytes with decoding bytes to
characters, or encoding characters to bytes. In Example 6-3 we saw the Charset.decode( )
method for decoding a
buffer of bytes into a buffer of characters. This is actually a
high-level convenience method, and we’ll see similar convenience
methods elsewhere in this chapter. For better streaming performance,
however, you can use the lower-level CharsetDecoder
and CharsetEncoder
classes, as is done in Example 6-5. This example is the
ChannelToWriter
class, which
defines a single static copy( )
method. This method reads bytes from a specified channel, decodes them
to characters using the specified Charset
, and then writes them to the
specified Writer
. (Note that this
is not the same function performed by Channels.newReader( )
, Channels.newWriter( )
, or Channels.newChannel( )
. The factory methods
of the Channels
class allow you to
wrap a channel around a stream or a stream around a channel, but do
not perform a copy.)
The read/decode/write loop shown in this example is a common one
in java.nio
code, but is more complex than you might expect. One reason for the complexity is that in many character encodings, there is not a one-to-one correspondence between bytes and characters. This means that there is no guarantee that all bytes in a ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.