Solved

How to read binary files in Java and typecast to a object type?

Posted on 2011-03-10
10
1,344 Views
Last Modified: 2013-11-24
Experts,

I created a binary file using C file apis and written a structure say
struct A
{  char a;
    int b;
    long c;
};

And Now a Java application is reading this file,
So, in java we created the same object type similar to above structure and reading to the object from file.
I am getting an execption here.
Is this not possible?

To check this we tried creating a binary file writing the same data which we updated in C to compare both the binary files (in c and in java)
to identify any difference in file data.
And we observed that both the files are not identical.
Please comment.
0
Comment
Question by:srimallikarthik
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35105838
need to see how you are reading it
0
 

Author Comment

by:srimallikarthik
ID: 35105877
Here is my java code which is reading from the binary file :

package javaapplication9;

/**
 *
 * @author karthik.kp
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {
  public static void main(String[] args) throws Exception {
    Junk obj1 = new Junk(1,'2',3);
 int counter = 0;


/* ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(
    new FileOutputStream("C:\\Users\\karthik.kp\\Documents\\Visual Studio 2008\\Projects\\structtest\\structtest\\javabin.bin")));
for ( counter=1; counter <= 2; counter++)
            {
                  objectOut.writeObject(obj1);
                       
            }

objectOut.close();
  */

    //ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(
       // new FileOutputStream("C:/JunkObjects.bin")));
    //objectOut.writeObject(obj1); // Write object
   // objectOut.close(); // Close the output stream

    ObjectInputStream objectIn = null;
   
    Junk object = null;

    objectIn = new ObjectInputStream(new BufferedInputStream(new FileInputStream(
        "C:\\Users\\karthik.kp\\Documents\\Visual Studio 2008\\Projects\\structtest\\structtest\\ex3.bin")));

    // Read from the stream until we hit the end
    //while (objectCount < 3) {
    //  object = (Junk) objectIn.readObject();
    //  objectCount++;
    //  System.out.println(object);
    //}
   
for ( counter=1; counter <= 2; counter++)
            {
                  object = (Junk) objectIn.readObject();
                        System.out.println(object);
            }
    objectIn.close();

  }
}

class Junk implements Serializable {
  int x;
  char y;
  long z;

  public Junk(int x,char y, long z) {
    this.x=x;
    this.y=y;
    this.z=z;
  }

}
0
 
LVL 92

Expert Comment

by:objects
ID: 35105915
you can't use ObjectInputStream, it is only for object written with ObjectOutputStream
You'll need to read it 'manually', reading the data as it has been written by your C app
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 35105938
A couple things that come to mind:

1. Java uses 2 bytes for a char, in C is just 1 byte

2. Java uses Big Endian for integers, in C depends on the machine (on x86/x64 is Little Endian)
0
 

Author Comment

by:srimallikarthik
ID: 35105965
Is it possible to read the bin file data mannually like in C app and typecast it to the type of C struct which i am writing to file.

I understand that data types differ in c and java from the above post.

Will there is any way to do this labourous job easy way ? (i.e.  interpreting the read C type struct data from binary file to java object data types)
0
 
LVL 92

Expert Comment

by:objects
ID: 35105980
No short cuts really, the following may give you some insight
http://stackoverflow.com/questions/277944/best-way-to-read-structured-binary-files-with-java

you're better off writing to something like xml and reading that from java
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35105996
If you have control over the C code, it's probably easier to change it to write the data in the Java binary format.
0
 

Author Comment

by:srimallikarthik
ID: 35106010
Yes , i do have control over the C code layer,

Can you please add little more info to make me understand how i can change it easily to java  binary format? ( one example would be of great help)
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 35106121
easier to use an interchange format such as xml
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 250 total points
ID: 35106174
Assuming you are running the C code on a Little Endian machine, the int and long values must be converted to Big Endian before writing then to the file.  The link below has the code for the conversion functions, which I reproduce below.

http://www.codeguru.com/forum/showthread.php?t=292902
inline void endian_swap(unsigned short& x)  // 16-bit 'short'
{
    x = (x>>8) | 
        (x<<8);
}

inline void endian_swap(unsigned int& x)  // 32-bit 'int'
{
    x = (x>>24) | 
        ((x<<8) & 0x00FF0000) |
        ((x>>8) & 0x0000FF00) |
        (x<<24);
}

// __int64 for MSVC, "long long" for gcc
inline void endian_swap(unsigned __int64& x)  // 64-bit (possibly 'long')
{
    x = (x>>56) | 
        ((x<<40) & 0x00FF000000000000) |
        ((x<<24) & 0x0000FF0000000000) |
        ((x<<8)  & 0x000000FF00000000) |
        ((x>>8)  & 0x00000000FF000000) |
        ((x>>24) & 0x0000000000FF0000) |
        ((x>>40) & 0x000000000000FF00) |
        (x<<56);
}

Open in new window


Then on the Java side, you should read the variables one at a time in the same sequence they were written in C.  Something like this:

import java.io.*;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        new Main().Run();
    }

    private void Run() throws Exception
    {
        ObjectInputStream in = new ObjectInputStream(
                        new BufferedInputStream(
                        new FileInputStream("C:\\Users\\karthik.kp\\Documents\\Visual Studio 2008\\Projects\\structtest\\structtest\\ex3.bin")));

        try
        {
            for(;;)
            {
                Junk obj = new Junk();

                obj.x = in.readInt(); // 32-bit 'int'
                obj.y = (char)in.readByte();
                obj.z = in.readLong(); // 64-bit 'long'

                System.out.println(obj);
            }
        }
        catch(EOFException ex)
        {
        }
        finally
        {
            in.close();
        }
    }

    public class Junk implements Serializable
    {
        public int x;
        public char y;
        public long z;

        public String ToString()
        {
            return "" + x + "," + y + "," + x;
        }
    }
}

Open in new window


I hope this helps.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

737 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