0

I am working on one project in which data has to read in binary format but one can put into a structure format. In java Structure is not available. I have send the data in byte array. It also has some structure.

For Example

Packet

int length 
int sequence no
MessageData

MessageData MessageHeader MessageBody

MessageHeader int data1 long data2 char[] data3//2bytes char[] data4//3 bytes

Same for MessageBody

But data should be packed in byte array.

One can do in CPP as

#pragma(2) //for word alignment

struct Packet{
    int length 
    int sequence no
    MessageData msgdata

}

I have to implement in java.

for reading data I am using Preon library

But for writing data I am still using normal method for packaging data in byte array with help of some function Anybody have work on same kind project or have better idea

Riduidel
  • 22,052
  • 14
  • 85
  • 185
Kamahire
  • 2,149
  • 3
  • 21
  • 50
  • If you are reading using the Preon library, I suggest you write using it as well. Otherwise you need to know how it expects your data to be structured. My guess is you intend to use a byte[] for the text rather than a char[] (char is 16-bit in Java) – Peter Lawrey Jan 05 '11 at 09:14
  • Yes, it is in byte[]. I don't know how write using Preon lib. If you have code then please post it. I didn't find any help or documentation on net. – Kamahire Jan 05 '11 at 09:17

1 Answers1

0

The current codebase does contain some bytes for writing data, however, it's not completed yet. Having said that, it might work for your current case:

Codec<Packet> codec = Codecs.create(Packet.class);
Packet packet = ...;
OutputStream out = ...;
codecs.encode(packet, codec, out);
Wilfred Springer
  • 10,869
  • 4
  • 55
  • 69