java -nio- reading a file- whats wrong with this

Discussion in 'Java' started by Craig, Feb 2, 2007.

  1. Craig

    Craig Guest

    java.io.FileInputStream zipStream = new java.io.FileInputStream
    (path);
    File file=new File(path);
    System.out.println(file.length());
    FileChannel fc= zipStream.getChannel();
    ByteBuffer buffer=ByteBuffer.allocate((int)file.length());
    buffer.clear();
    System.out.println(buffer.capacity());
    fc.read(buffer);
    fc.close();
    zipStream.close();
    byte[] b=new byte[buffer.capacity()];
    System.out.println(b.length);
    buffer.get(b,0,b.length);
    return b;

    I am getting a BufferUnderflowException when I do buffer.get(b);
    I don't know why this is wrong because all the three print statements
    gives the same value: 9932

    The second question I have is,how can I avoid int casting when I do
    bytebuffer.allocate. I am concerned about handling larger files.

    Kindly Reply.
    Thanks,
    Craig
    Craig, Feb 2, 2007
    #1
    1. Advertising

  2. Craig

    Daniel Pitts Guest

    Re: java -nio- reading a file- whats wrong with this

    On Feb 2, 12:32 pm, "Craig" <> wrote:
    > java.io.FileInputStream zipStream = new java.io.FileInputStream
    > (path);
    > File file=new File(path);
    > System.out.println(file.length());
    > FileChannel fc= zipStream.getChannel();
    > ByteBuffer buffer=ByteBuffer.allocate((int)file.length());
    > buffer.clear();
    > System.out.println(buffer.capacity());
    > fc.read(buffer);
    > fc.close();
    > zipStream.close();
    > byte[] b=new byte[buffer.capacity()];
    > System.out.println(b.length);
    > buffer.get(b,0,b.length);
    > return b;
    >
    > I am getting a BufferUnderflowException when I do buffer.get(b);
    > I don't know why this is wrong because all the three print statements
    > gives the same value: 9932
    >
    > The second question I have is,how can I avoid int casting when I do
    > bytebuffer.allocate. I am concerned about handling larger files.
    >
    > Kindly Reply.
    > Thanks,
    > Craig



    I'm reminded of Bradley Video's little message on there video
    cassettes. "Be kind, please rewind"
    <sscce>
    import java.io.File;
    import java.io.FileInputStream;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;

    public class TestNIO {
    public static void main(String...args) throws Exception {
    final File file = new File("test.dat");
    final FileInputStream input = new FileInputStream(file);
    final FileChannel channel = input.getChannel();
    final ByteBuffer byteBuffer = ByteBuffer.allocate((int)
    file.length());
    channel.read(byteBuffer);
    channel.close();
    input.close();
    final byte[] bytes = new byte[byteBuffer.capacity()];
    byteBuffer.rewind();
    byteBuffer.get(bytes);
    }
    }
    </sscce>
    Daniel Pitts, Feb 2, 2007
    #2
    1. Advertising

  3. Craig

    Craig Guest

    Re: java -nio- reading a file- whats wrong with this

    Thank you Daniel, That helped!

    How can I avoid casting long to int when I am allocating to
    ByteBuffer? This can be a problem when i am reading larger files.
    Craig, Feb 2, 2007
    #3
  4. Craig

    Daniel Pitts Guest

    Re: java -nio- reading a file- whats wrong with this

    On Feb 2, 1:09 pm, "Craig" <> wrote:
    > Thank you Daniel, That helped!
    >
    > How can I avoid casting long to int when I am allocating to
    > ByteBuffer? This can be a problem when i am reading larger files.


    The problem is that you can only allocate an "int" number of bytes.
    You'll have to split the reads into multiple byte buffers.

    I might suggest a for loop and a fixed buffer size.
    Imagine trying to allocate a ByteBuffer of 8gigabyte. You'd almost
    certainly crash the computer. As it stands, int is big enough for 2
    gig.
    You should probably check the file size <= Integer.MAXINT, or change
    your algorithm.

    Or, possibly using FileChannel.map instead.
    Daniel Pitts, Feb 2, 2007
    #4
    1. Advertising

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. =?Utf-8?B?UGF1bA==?=

    whats wrong with this java script?

    =?Utf-8?B?UGF1bA==?=, Jun 29, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    465
    =?Utf-8?B?UGF1bA==?=
    Jun 29, 2005
  2. Hugh Mackay

    Reading text files with java.nio.*

    Hugh Mackay, Feb 3, 2004, in forum: Java
    Replies:
    1
    Views:
    11,667
    Chris Smith
    Feb 3, 2004
  3. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,229
    iksrazal
    Jun 18, 2004
  4. K R
    Replies:
    1
    Views:
    435
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Nov 10, 2004
  5. Adrian
    Replies:
    7
    Views:
    504
    James Kanze
    Oct 5, 2007
Loading...

Share This Page