9 Replies Latest reply: Jan 29, 2008 8:06 PM by EJP RSS

    Java NIO with ByteBuffer

    807603
      Hi guys,

      I search the forum and the web too, there are many pros and cons in relation to NIO, and on my free time I was trying to research about it, but there
      are time when some things don't make any sense at all.

      Without NIO:
          FileInputStream foin = new FileInputStream("/home/metitus/Desktop/test");
          FileOutputStream fout = new FileOutputStream("/home/metitus/Desktop/test1");
      
          int character = 0;
      
          while((character = foin.read()) != -1)
          {
              fout.write(character);
          }
      with NIO:
      FileInputStream foin = new FileInputStream("/home/metitus/Desktop/test");
          FileOutputStream fout = new FileOutputStream("/home/metitus/Desktop/test1");
      
          FileChannel fci = foin.getChannel();
          FileChannel fco = fout.getChannel();
      
          ByteBuffer buffer = ByteBuffer.allocate(1);
      
          while(fci.read(buffer) != -1)
          {
              buffer.flip();
              fout.write(buffer.array());
              buffer.clear();
          }
      So guys where is the gain here? Why do I need a ByteBuffer always; it might be useful in some cases but definitely not in all.

      MeTitus
        • 1. Re: Java NIO with ByteBuffer
          807603
          First of all, it's pretty silly to be using a 1 byte buffer to copy a stream. Why would you use that as an example?

          One advantage of ByteBuffer is that it can use direct allocation, which reduces overhead when transferring large amounts of data. Another is that ByteBuffer encapsulates a lot of operations that are normally done with a byte[] and an int.
          Why do I need a ByteBuffer always;
          Who said you need a ByteBuffer always?
          • 2. Re: Java NIO with ByteBuffer
            807603
            From what I could see, all the write methods of the Channel require a ByteBuffer, am I mistaken?

            The example I showed was just an example, nothing else...

            MeTitus
            • 3. Re: Java NIO with ByteBuffer
              807603
              Me_Titus wrote:
              From what I could see, all the write methods of the Channel require a ByteBuffer, am I mistaken?
              Yes, but if you're just copying a file, the java.io classes should be sufficient.
              • 4. Re: Java NIO with ByteBuffer
                807603
                What if I have to write to a socket while I am also reading from another socket?
                What if I have to reply to another socket the bytes I am getting, one by one?

                It is not efficient that way, but I might be asked to do that way. Then I would have to use a ByteBuffer... that why I said that I always have to use them.

                thanks for your replies paul.miner

                MeTitus
                • 5. Re: Java NIO with ByteBuffer
                  807603
                  paul.miner wrote:
                  Me_Titus wrote:
                  From what I could see, all the write methods of the Channel require a ByteBuffer, am I mistaken?
                  Yes, but if you're just copying a file, the java.io classes should be sufficient.
                  Yes, but using FileChannel is an alternative: http://www.exampledepot.com/egs/java.nio/File2File.html
                  • 6. Re: Java NIO with ByteBuffer
                    807603
                    Guys I know that...

                    Perhaps I should have shown an example with sockets ;)

                    MeTitus
                    • 7. Re: Java NIO with ByteBuffer
                      EJP
                      BTW your example is incorrect. buffer.clear() should be buffer.compact(). Your way there is a risk of losing data.
                      • 8. Re: Java NIO with ByteBuffer
                        807603
                        I want to clean the buffer because there is only one position, how would compact help there?

                        MeTitus
                        • 9. Re: Java NIO with ByteBuffer
                          EJP
                          Well there should be more than one position. You're doing it the slowest way possible. There should be 8192, or 16384, or some large number. And when you raise the size you have to use compact(): buffer.clear() is valid if and only if the buffer size is 1; buffer.compact() is valid for all buffer sizes.

                          In fact the loop should look like this to take care of the last write:
                          while ((count = in.read(buffer)) >= 0 || buffer.position() > 0)
                          {
                            buffer.flip();
                            out.write(buffer);
                            buffer.compact();
                          }