FileInputStream read from offset

H

HowTo

Hello,
I just ran into a probelm when reading a binary file.
The binary file contains a bunch of data which are to be assigned to
different buffers.
Now, there are occassions where a single byte of the file needs to be
twice, as it contains part of information for buffer A and another
part of information for buffer B - I know its stupid, but I didn't
design the file layout, I only have to somehow parse it....
Anyway, I use the FileInputStream class for reading the binary file
and that normally works fine. However the read (byte[], offset,
length) routine, does not really seems to position at the given
offset ... at least it does not seem to make any diffrence if I insert
(offset -1), (offset) or (offset+1).

Now, did anyone already experience a similar problem? And if you did,
how did you solve it?
Many thanks in advance
 
D

Daniel Pitts

HowTo said:
Hello,
I just ran into a probelm when reading a binary file.
The binary file contains a bunch of data which are to be assigned to
different buffers.
Now, there are occassions where a single byte of the file needs to be
twice, as it contains part of information for buffer A and another
part of information for buffer B - I know its stupid, but I didn't
design the file layout, I only have to somehow parse it....
Anyway, I use the FileInputStream class for reading the binary file
and that normally works fine. However the read (byte[], offset,
length) routine, does not really seems to position at the given
offset ... at least it does not seem to make any diffrence if I insert
(offset -1), (offset) or (offset+1).

Now, did anyone already experience a similar problem? And if you did,
how did you solve it?
Many thanks in advance
Read the javadoc for mark()
 
D

Daniele Futtorovic

HowTo said:
Hello,
I just ran into a probelm when reading a binary file.
The binary file contains a bunch of data which are to be assigned to
different buffers.
Now, there are occassions where a single byte of the file needs to be
twice, as it contains part of information for buffer A and another
part of information for buffer B - I know its stupid, but I didn't
design the file layout, I only have to somehow parse it....
Anyway, I use the FileInputStream class for reading the binary file
and that normally works fine. However the read (byte[], offset,
length) routine, does not really seems to position at the given
offset ... at least it does not seem to make any diffrence if I insert
(offset -1), (offset) or (offset+1).

Now, did anyone already experience a similar problem? And if you did,
how did you solve it?
Many thanks in advance

Read the javadoc for mark()

java.io.PushbackInputStream seems a tad more simple in this case.
<http://java.sun.com/javase/6/docs/api/java/io/PushbackInputStream.html>
 
A

Arne Vajhøj

HowTo said:
I just ran into a probelm when reading a binary file.
The binary file contains a bunch of data which are to be assigned to
different buffers.
Now, there are occassions where a single byte of the file needs to be
twice, as it contains part of information for buffer A and another
part of information for buffer B - I know its stupid, but I didn't
design the file layout, I only have to somehow parse it....
Anyway, I use the FileInputStream class for reading the binary file
and that normally works fine. However the read (byte[], offset,
length) routine, does not really seems to position at the given
offset ... at least it does not seem to make any diffrence if I insert
(offset -1), (offset) or (offset+1).

Now, did anyone already experience a similar problem? And if you did,
how did you solve it?

If you need full flexibility then go for java.io.RandomAccessFile
it has a seek method.

Arne
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top