Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > benefits of NIO

Thread Tools

benefits of NIO

Roedy Green
Posts: n/a
I have been looking an nio trying to figure out how it works, and
hence where it would pay to flip over to it.

It uses an underlying InputStream. So if you read the whole
InputFileStream into a byte array in one i/o, NIO would do you no
good. It uses the same methods you do to get raw bytes.

However, if you used nio to read an encoded character file, you have
to do the decoding yourself, but the slick thing is, the output can go
to a CharBuffer. This saves quite a bit of overhead. decode does not
need to know in advance precisely how may chars the stream will decode
to. That saves a copy for trimming to size. And since it effectively
ends up with a char[] instead of a string, than saves yet another
copy. Bravo! The catch is CharBuffer is not quite as convenient as
String for processing.

I love the code to copy from one stream to another with a chunk buffer
in the middle. So simple compared with doing it yourself as I do in
FileTransfer. see
The buffers magically handle the housekeeping to deal with short
reads, and the partial block at the end.

The inner loop for a copy is just: buffer );
outchannel.write( buffer );

There is no copying. You can even arrange that physical i/o happens
from the buffer array.

The basic idea is, ordinary i/o appears as a stream of individual
bytes or chars. NIO, i/o occurs in buffer fulls, and you are acutely
away of the buffer and can peek and poke it yourself. It is a little
bit like PL/1 based i/o.
Canadian Mind Products, Roedy Green. Java custom programming, consulting and coaching.
Reply With Quote
Stefan Schulz
Posts: n/a
Not quite, sadly. You still need to flip() and compact() the buffer, so
your snipped becomes:;

Reply With Quote
Posts: n/a
Roedy Green wrote:

> It uses an underlying InputStream.

No it doesn't.

> The inner loop for a copy is just:
> buffer );
> outchannel.write( buffer );
> There is no copying. You can even arrange that physical i/o happens
> from the buffer array.

The inner loop for a copy is:

while ((count = > 0 || buffer.position() > 0)

A copy operation is implicit in buffer.compact(). I don't know how else
physical I/O could operate other than from the buffer array, but perhaps
what is meant is that you can even arrange that the underlying byte[]
array of the buffer is on the JNI side rather than the Java side, so the
data doesn't have to enter the Java address space at all, i.e. it
doesn't have to cross the Java/JNI boundary twice.

You can also do inchannel.transferTo(outChannel); or
outChannel.transferFrom(inchannel) which is even simpler (but very
subject to OS and VM limitations, beware).
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[O.T] SystemC benefits? Jezwold VHDL 2 02-08-2005 02:51 PM
MCSE Benefits... what benefits. Johnny Yi MCSE 51 01-24-2005 02:05 PM
DBA Certification Benefits?...And Advice Ryan Gagliano Microsoft Certification 2 01-15-2005 07:00 AM
NIO with timeouts != NIO? iksrazal Java 1 06-18-2004 02:28 PM