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?
 
Ad

Advertisements

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.
 
Ad

Advertisements

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
 
Ad

Advertisements


Top