Can constructive constructs be used in Java?

I am considering using Java for a large project, but I could not find anything that remotely represented Java structures. I need to be able to convert network packets into structures / classes that can be used in the application.

I know that you can use RandomAccessFile , but this method is NOT acceptable. Therefore, I am curious if it is possible to "overlay" a set of bytes on a structure, as I could do in C. If this is not possible, I cannot use Java.

So, the question I ask is, is it possible to expose aligned data to a class without any extra effort besides specifying alignment and data types?

+7
source share
8 answers

Basically you ask if you can use a C-specific solution to the problem in another language. The answer is expected to be no.

However, it is entirely possible to build a class that accepts a set of bytes in its constructor and creates the corresponding instance.

 class Foo { int someField; String anotherField; public Foo(byte[] bytes) { someField = someFieldFromBytes(bytes); anotherField = anotherFieldFromBytes(bytes); etc. } } 

You can make sure that one-to-one mapping of class instances to byte arrays. Add the toBytes () method to serialize the instance in bytes.

+9
source

Not. You cannot pass an array of bytes to a class object.

With this you can use java.nio.Buffer and easily retrieve the fields needed for this object:

 class Packet { private final int type; private final float data1; private final short data2; public Packet(byte[] bytes) { ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.BIG_ENDIAN); // or LITTLE_ENDIAN type = bb.getInt(); data1 = bb.getFloat(); data2 = bb.getShort(); } } 
+15
source

No, you cannot do this. Java simply does not have the same concepts as C.

You can create a class that behaves like a structure:

 public class Structure { public int field1; public String field2; } 

and you can have a constructor that takes an array or bytes or a DataInput to read bytes:

 public class Structure { ... public Structure(byte[] data) { this(new DataInputStream(new ByteArrayInputStream(data))); } public Structure(DataInput in) { field1 = in.readInt(); field2 = in.readUTF(); } } 

then read the bytes from the wire and transfer them to Structures :

 byte[] bytes = network.read(); DataInputStream stream = new DataInputStream(new ByteArrayInputStream(bytes)); Structure structure1 = new Structure(stream); Structure structure2 = new Structure(stream); ... 

It is not as concise as C, but it is pretty close. Please note that the DataInput interface cleanly removes any beats with integrity on your behalf, so there is definitely an advantage over C.

+4
source

There is nothing that fits your description.

The closest thing to a structure in Java is a simple class that contains the values ​​available through its fields or set / get methods.

A typical conversion tool between instances of the Java class and wired views is Java serialization, which can be heavily customized as needed. This is what the Java Remote Method Invocation API uses and works very well.

+1
source

As Joshua says, serialization is a typical way to do such things. However, there are other binary protocols such as MessagePack, ProtocolBuffers, and AvRO.

If you want to play with bytecode structures, look at ASM and CGLIB; they are very common in Java applications.

+1
source
 ByteBuffer.wrap(new byte[] {}).getDouble(); 
+1
source

No, It is Immpossible. You are trying to use Java as C, which can cause complications. Either learn how to do things in Java, or go back to C.

In this case, the Java path is likely to include a DataInputStream and / or DataOutputStream .

0
source

You cannot use an array of bytes for an instance of a class. But you can do much more with java. Java has an internal, very strong and very flexible serialization engine. That's what you need. You can read and write the object to / from the stream.

If both sides are written in java, there is no problem. If one of the parties is not java, you can customeze your serialization. Start by reading javadoc java.util.Serializable.

0
source

All Articles