Equivalent of "fread" C function in Java

N

news.amnet.net.au

Hi

I have an Oracle parser which works well on String values but which I would
like to use also to read in binary files. The aim is extract particular
bytes (based on a known offset, up to a point further on), then converting
the selected bytes to sensible string values.

I know there is a function in C called "fread" which is able to sort binary
content, allowing for big and little endian swaps and also able to make
sense of mantissa and exponent for floating numbers.

Is there an equivalent function in Java, and if so, how would one use it?

At the moment, my parser gets an html input stream, like this:

<snip>
String documentContent = "";
BufferedReader dataStream =
new BufferedReader(htmlStream);

for (String line = dataStream.readLine();line != null;
line = dataStream.readLine())
{
documentContent = documentContent + line + "\n";
}

<snip>

.... so I end up with all the data in a String object called documentContent.
I am actually not sure if this is appropriate for capturing binary data.

Anyway, I would like to have a function where I read in the binary data, but
then extract the data back to strings, depending on the offset I specify. I
would need something similar to fread i.e. something which not only converts
bytes to characters but also tranlates the characters back to sensible unit
strings.

Can anyone help me with a code example, something like a function which is
called up (so I can also use it as a bean), e.g:

String mySensibleString = parseByte(documentContent);

and then the function itself:

private String parseByte (String parseString){

....something like the C function "fread" here with the ability to read
bytecode at an offset point up to a point further along
then a conversion to a string value, which then can be returned.

return resultString;

}

Any help will be greatly appreciated.

Thanks very much

Hugo
 
A

Anthony Borla

news.amnet.net.au said:
Hi

I have an Oracle parser which works well on String values
but which I would like to use also to read in binary files.
The aim is extract particular bytes (based on a known
offset, up to a point further on), then converting
the selected bytes to sensible string values.

I know there is a function in C called "fread" which is able
to sort binary content, allowing for big and little endian
swaps and also able to make sense of mantissa and exponent
for floating numbers.

Is there an equivalent function in Java, and if so, how
would one use it?

Take a look at the documentation for:

java.nio.ByteBuffer;

It sounds like the type of functionality you require. Google for related
tutorials / examples.

I hope this helps.

Anthony Borla
 
A

ak

I know there is a function in C called "fread" which is able to sort
binary
content, allowing for big and little endian swaps and also able to make
sense of mantissa and exponent for floating numbers.

DataInputStream#readLine(); - however no big/little endian swapping
performed.

I have written UnifiedIO which can read big/little endian - currently it is
a part of ImageroReader,
later it will be available as standalone package. see
http://reader.imagero.com for more info.

____________

http://reader.imagero.com the best java image reader.
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top