I am porting some C++ code over to java, and in my particular instance, i am writing data to a byte[] to be written to a file. The first portion, as defined in C++ is a structure consisting of a uint, and 3 ushorts. The second portion is the main part of the data, which i will just append on the end of the byte[] before i send it to the outputstream.
My question is this: What is the simplest way to write the header values to the byte[]? I know i can put 1 value in there, then offset the specific number of bytes, and repeat as necessary, but is this the best way to do it?
Also, how do i manage byte alignment? The C++ code appears to use the default values (4-byte?) for alignment.
Thanks,
Jason
You might find it easier to use
ByteBuffer, which is probably the nicest way in Java to organize byte-by-byte output.ByteBufferdoesn’t directly care about alignment, though, and I don’t know how C++ is aligning its output — but in a pinch, you can just advance it manually.