• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 833
  • Last Modified:

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?

0
jasminekwok
Asked:
jasminekwok
  • 6
  • 3
1 Solution
 
bkrahmerCommented:
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
0
 
jasminekwokAuthor Commented:
Thanks!
How do i determine whether the structure has a fixed size?
0
 
jasminekwokAuthor Commented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bkrahmerCommented:
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
0
 
jasminekwokAuthor Commented:
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
0
 
bkrahmerCommented:
I don't know if you are referring to the two bytes that make up the java char, or if you are dealing with unicode characters.

I'll assume the former.

for each java char
  assign java_char[i] = c_char[i];
next

They're two bytes, but you don't think of them that way.

brian
0
 
jasminekwokAuthor Commented:
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
0
 
jasminekwokAuthor Commented:
oops, i refreshed my screen, sorry for the repeated comment
0
 
jasminekwokAuthor Commented:
oops, i refreshed my screen, sorry for the repeated comment
0
 
CleanupPingCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now