I have a requirement to connect to a third party system and retrieve some data, via TCP/socket. The data format that will be sent is in a fixed length format and in binary.
Example of a request:
short MessageID = 5;
int TransactionTrace = 1;
Basically, the I must send 6 bytes to the third party system. Data in Hex: 000500000001
I have tried to do the following in Java, but it doesn't work.
- Create a class with two variables and the correct data types (messageID & transactionTrace)
- Serialize the class to a byte array
The serialization returns far too many bytes.
Can someone please assist?
Thank you.
Java Class:
public final class MsgHeader implements Serializable {
public short MessageID;
public int TransactionTrace;
}
Serialization:
MsgHeader header = new MsgHeader();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try
{
out = new ObjectOutputStream(bos);
out.writeObject(header);
byte[] yourBytes = bos.toByteArray();
System.out.println("Hex Data : " + getHex(yourBytes));
}