Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Craig - 02 Feb 2007 20:32 GMT
        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
Daniel Pitts - 02 Feb 2007 20:59 GMT
>                 java.io.FileInputStream zipStream = new java.io.FileInputStream
> (path);
[quoted text clipped - 22 lines]
> 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>
Craig - 02 Feb 2007 21:09 GMT
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.
Daniel Pitts - 02 Feb 2007 22:38 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.