binary struct reading from file. How to?

I have a file written from MSVC++ 6.0 like:

typedef struct My_format{
short num_1;
char name_1[4];
short num_2;
char name_2[10];
}My_format;
This structure repeats every 256 bytes.
I have to read this from Java. The only class that has readShort() method is RandomAccessFile. Do I have to read the members of the struct one by one? Or is there any method to do buffered reading?
If I read in an array of bytes, how can I convert the corresponding bytes to short/char[] ?

Thanks.
wauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
If you use readShort (which is also in DataInputStream by the way) it cannot be guaranteed to work. In Java a short is guaranteed to be two bytes, and to have a specific byte order for the disk representation. Neither of these conditions is true in C. C does not guarantee that short is two bytes, only that it is not longer than an int. And there is no guarantees about byte order on disk whatsoever. That is machine dependent.
Also, a char in Java is a two byte quantity (to support Unicode).

So, assuming that all these things come together, and you have no portability concerns you can use readShort and readByte. The result of readShort would simply be assigned to a short variable, and the readByte would be assigned to a byte or character depending on your needs. You could achieve buffered reading by using a BufferedInputStream.

If they do not happen to correspond, I would recommend using the same BufferedInputStream and pulling characters with readByte and assembling them as needed, e.g.

BufferedInputStream bi=new BufferedInputStream(new FileInputStream("filename"));
DataInputStream di=new DataInputStream(bi);
short num=((short)di.readByte())<<8) | (((short)di.readByte())&0xff);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.