Which class is better for reading binary data?

R

Royan

Recently I have faced a problem of choosing a proper class from
java.io package (my goal is to use standard java.io package). So I had
to find an alias of BufferedReader but designed specifically for
reading raw binary data. I wonder what could you advice me on that?
 
A

Arne Vajhøj

Royan said:
Recently I have faced a problem of choosing a proper class from
java.io package (my goal is to use standard java.io package). So I had
to find an alias of BufferedReader but designed specifically for
reading raw binary data. I wonder what could you advice me on that?

The class for reading binary data is DataInputStream (any InputStream
are OK for reading bytes, but if you need to read other simple data
types then DataInputStream is it).

BufferedReader are for reading lines of text.

Arne
 
A

Alex.From.Ohio.Java

Recently I have faced a problem of choosing a proper class from
java.io package (my goal is to use standard java.io package). So I had
to find an alias of BufferedReader but designed specifically for
reading raw binary data. I wonder what could you advice me on that?

Don't use BufferedReader (I meand Reader) because it converts bytes
from local stream to unicode characters and so can't be used for raw
data.

It's not clear what you try to achieve bit it it looks like yo need
buffer for speed (BufferedInputStream) and raw data access
(DataInputStream). Java is wonderful language and it can combine and
create stream exactly what you need. So, based on what I think it
would be like this:

new DataInputStream(new BufferedInputStream(new
FileInputStream("abc.raw")))

So you will have speed of buffer and data conversion ability of data
stream to read raw data.
And, of course, you always can use "low" level of any stream to read
real raw data byte by byte or all of them in one buffer.

Alex.
http://www.myjavaserver.com/~alexfromohio/
 
R

Royan

Don't use BufferedReader (I meand Reader) because it converts bytes
from local stream to unicode characters and so can't be used for raw
data.

It's not clear what you try to achieve bit it it looks like yo need
buffer for speed (BufferedInputStream) and raw data access
(DataInputStream). Java is wonderful language and it can combine and
create stream exactly what you need. So, based on what I think it
would be like this:

new DataInputStream(new BufferedInputStream(new
FileInputStream("abc.raw")))

So you will have speed of buffer and data conversion ability of data
stream to read raw data.
And, of course, you always can use "low" level of any stream to read
real raw data byte by byte or all of them in one buffer.

Alex.http://www.myjavaserver.com/~alexfromohio/

Arne and Lew thanks!
Alex, I think you have answered my question. I'll use a combination of
DataInputStream, BufferedInputStream and FileInputStream just as
you've suggested.
 
R

Roedy Green

Recently I have faced a problem of choosing a proper class from
java.io package (my goal is to use standard java.io package). So I had
to find an alias of BufferedReader but designed specifically for
reading raw binary data. I wonder what could you advice me on that?

Your choices are DataInputStream for big-endian
LEDataInputStream for little-endian
and possibly nio for low level high performance.

see http://mindprod.com/applet/fileio.html
for sample code for DataInputStream and LEDataInputStream
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top