3

struct is necessary when you try to parse some file format like ELF, etc...

Is there something like C's struct in Java?

Or can Java be used to parse ELF/binary format directly in the first place?

lexer
  • 1,027
  • 3
  • 14
  • 18

4 Answers4

2

If you need a struct for grouping different data of the same type, Java has a class, and a class is better in logically grouping data than a struct because it includes operations on the data as well.

If you want to format ELF, then you may have to look at the "The ELF Parser" section in http://www.icsa.inf.ed.ac.uk/research/groups/hase/manuals/design/javahase.html. See also LibElf and GElf - A Library to Manipulate ELf Files (an old article)

Nivas
  • 18,126
  • 4
  • 62
  • 76
  • 2
    In C, you can read a binary blob directly into a struct. This is the reason the OP asks for a struct, to easily parse a series of bytes into an ELF structure. I assume the OP knows about Java classes, but in Java it is not as easy to deserialize a series of bytes into a class. – Sjoerd Aug 11 '11 at 12:05
  • @Sjoerd, this was not clear in the question. But yes, this very well could be his intention, especially considering the answer he has accepted... – Nivas Aug 11 '11 at 12:51
1

There's ByteBuffer.

Edit

This is just to answer how you might parse the ELF format, which seemed to be what the OP was actually asking for.

For example (I assume this is the same format, apologies if it's a completely different ELF format, either way, it shows the same process):

http://jumdbrowser.googlecode.com/svn-history/r3/trunk/UmdBrowser/src/jpcsp/format/Elf32.java

SimonC
  • 6,590
  • 1
  • 23
  • 40
1

Unfortunatly there is no decent support to read binary structured data in java.

This example reads image header into a byte array and assembles the required information.

stacker
  • 68,052
  • 28
  • 140
  • 210
-1

Edit: answer to the first question

Java classes

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134