Link to home
Start Free TrialLog in
Avatar of jasminekwok
jasminekwok

asked on

Convert fread in C to java

I need to convert the fread function in C into java.

fread(buf,size, noOfItemsRead, stream)

The fread function stores the read bytes into the an area of memory specified by the arugments. How do i do this in Java?

In the C program that I have,
the variable, mat, is a struct.
&mat is passed into fread as the buffer.
The next thing I know, the object is already initialised with values. How is this done in Java?


Currently, I am using the read(byte[]) of the FileInputStream. How do i use the bytes stored into the array to initialise an Object type?

Avatar of bkrahmer
bkrahmer

I think you are trying to read the binary file which contains the C structures and make objects out of them.  If that's the case, you'll need to understand the exact format of the C structure (what each member type is, signed/unsigned, etc.)  Next, you would create a Java class that represents that structure.  Then, create a constructor for that class, and pass in the byte array as an argument.  I would only do this if the structure was a fixed size.  If the structure had a variable size, I would use a static factory method in that class to create the objects.  

After you've gotten rid of your C data files, you'll probably want to use ObjectInputStreams and ObjectOutputStreams.  They are very nice to work with.  Just be sure to version your objects if you go that route.

brian
Avatar of jasminekwok

ASKER

Thanks!
How do i determine whether the structure has a fixed size?
In the C program, the struct has unsigned char arrays.
The class that represents the structure has char[] as attributes.

public class MatStructure{
  private char[] a;
  private char[] b;
  ...
}

Even with the byte[] passed in as argument in the constructor, how do I initialise the contents of the char arrays? The contents of the file being read is actually the values to be placed into the char arrays. Do I just cast the byte into char?
If the C struct has unsigned char arrays, I'm guessing that there is either a size member that says how long the arrays are, or they are null terminated.  If they are null terminated, you would read until your byte==0.  Each java char is actually two bytes, but this is because java uses utf-8 encoding to handle languages with large character sets.  You are probably dealing with ascii chars, so it would probably be safe to cast each byte read into a java char.

To ensure your program can handle all of the possible input correctly, I would write some test cases that excercise boundary conditions.

brian
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
ASKER CERTIFIED SOLUTION
Avatar of bkrahmer
bkrahmer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
oops, i refreshed my screen, sorry for the repeated comment
oops, i refreshed my screen, sorry for the repeated comment
jasminekwok:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.