benefits of NIO

Discussion in 'Java' started by Roedy Green, Jan 22, 2006.

  1. Roedy Green

    Roedy Green Guest

    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.
    Roedy Green, Jan 22, 2006
    1. Advertisements

  2. Not quite, sadly. You still need to flip() and compact() the buffer, so
    your snipped becomes:;
    Stefan Schulz, Jan 22, 2006
    1. Advertisements

  3. EJP

    EJP Guest

    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).
    EJP, Jan 23, 2006
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jezwold

    [O.T] SystemC benefits?

    Jezwold, Feb 7, 2005, in forum: VHDL
    Feb 8, 2005
  2. Chak

    .NET 2.0 benefits

    Chak, Feb 23, 2006, in forum: ASP .Net
    Matt Adamson
    Apr 20, 2006
  3. Stijn Verrept

    Benefits ASP.NET over CGI

    Stijn Verrept, Jan 19, 2004, in forum: ASP .Net
  4. Aaron

    ASP.NET benefits for ASP

    Aaron, Jan 24, 2004, in forum: ASP .Net
    Hermit Dave
    Jan 24, 2004
  5. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Jun 18, 2004

Share This Page