Equivalent of "fread" C function in Java

Discussion in 'Java' started by news.amnet.net.au, Jan 15, 2004.

  1. 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
     
    news.amnet.net.au, Jan 15, 2004
    #1
    1. Advertising

  2. "news.amnet.net.au" <> wrote in message
    news:newscache$6rvirh$u7a$...
    > 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
     
    Anthony Borla, Jan 15, 2004
    #2
    1. Advertising

  3. news.amnet.net.au

    ak Guest

    > 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.
     
    ak, Jan 15, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick
    Replies:
    6
    Views:
    867
    Roedy Green
    Jul 13, 2004
  2. marcinka

    Problem with fread function

    marcinka, Dec 8, 2003, in forum: C Programming
    Replies:
    5
    Views:
    454
    Mark A. Odell
    Dec 9, 2003
  3. Fernando Barsoba

    fread() function and reading binary files

    Fernando Barsoba, Oct 17, 2005, in forum: C Programming
    Replies:
    2
    Views:
    394
    Martin Ambuhl
    Oct 17, 2005
  4. wang
    Replies:
    11
    Views:
    1,237
    Alf P. Steinbach /Usenet
    Aug 10, 2010
  5. news.amnet.net.au

    Equivalent of "fread" C function in Java

    news.amnet.net.au, Jan 15, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    180
    Sherm Pendley
    Jan 15, 2004
Loading...

Share This Page