Home arrow static arrow Java Programming [Archive] - BufferedReader: Problems with reading with offset and length
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - BufferedReader: Problems with reading with offset and length
This topic has 1 reply on 1 page.

Posts:37
Registered: 9/17/03
BufferedReader: Problems with reading with offset and length  
Jul 9, 2004 6:31 PM



 
Hi

Can someone please tell me why the code below does not work?

The values for the fields are:
stringEntriesOffset+offsetToString=4421700
stringSize=10

----Code----
byte[] byBuff=new byte[10];
BufferedInputStream inputStreamValue=new BufferedInputStream(new FileInputStream( f ));

inputStreamValue.read(byBuff, stringEntriesOffset + offsetToString, stringSize);
----Code----

That means that i want to read at the position 4421700 the next ten bytes. When i take a look into BufferedInputStream i see following code:
if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
throw new IndexOutOfBoundsException();
}
Off und len are greater 0, but (b.length - (off + len)) is -4421700, also far less then 0. Ok, this is the place where the excetion is being thrown, but why this statement? That would mean, that i have to make the array about 4MB big even when i only need only 10 bytes...
Did i totatly missunderstand the conzept of "offsets" or whats the conzept behind this?
BTW: I found similar code in the "Buffer"- and DataInputStream-class.

Thanks
Matt
 

Posts:24,517
Registered: 98-02-27
Re: BufferedReader: Problems with reading with offset and length  
Jul 9, 2004 7:13 PM (reply 1 of 1)



 
You can't do this with a BufferedInputStream all the data is read sequentially. You've misunderstood the 'offset' paramenter. Here is the description from the API:

off - offset at which to start storing bytes.

Check out the RandomAccessFile. It should be able to do what you want.
 
This topic has 1 reply on 1 page.