?
Solved

Convert fread in C to java

Posted on 2003-03-09
10
Medium Priority
?
811 Views
Last Modified: 2006-11-17
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
Comment
Question by:jasminekwok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8100661
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
 

Author Comment

by:jasminekwok
ID: 8100777
Thanks!
How do i determine whether the structure has a fixed size?
0
 

Author Comment

by:jasminekwok
ID: 8101008
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 2

Expert Comment

by:bkrahmer
ID: 8101089
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
 

Author Comment

by:jasminekwok
ID: 8108534
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
0
 
LVL 2

Accepted Solution

by:
bkrahmer earned 40 total points
ID: 8108923
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
 

Author Comment

by:jasminekwok
ID: 8108942
If a char is 2 bytes, then how do I combine 2 elements of the byte[] into a char?
0
 

Author Comment

by:jasminekwok
ID: 8108945
oops, i refreshed my screen, sorry for the repeated comment
0
 

Author Comment

by:jasminekwok
ID: 8109189
oops, i refreshed my screen, sorry for the repeated comment
0
 

Expert Comment

by:CleanupPing
ID: 9058830
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question