Best way to encode minimalistic messages

I need to create a little client/server application that should transfer data like this:

statistic <- command type identifier

15.23.63.12 <- Statistic for this IP address

increase <- the kind of action that should be done with the address's statistic

6 <- Just some other parameters...

So there has to be one string that identifies the type of the command and then there should be some parameters depending on the command type. This parameters are different but always primitive data types. Most probably String, Short, Byte, Integer, and so on...

So there are instruction sets of different primitive data types.

My question is: Is it the best way to wrap the socket's streams in DataInput/OutputStreams and just read/write from them? Or is it better to save the messages into a byte array and then wrap this byte array in a ByteArrayInputStream and wrap the ByteArrayInputStream in a DataInputStream that I can read from? Or should I wrap the byte array in a ByteBuffer?

And if I wanted to encrypt my messages, would I have to save them as a byte array, then decrypt the byte array and then wrap it into some kind of data reader?


ANSWERS:


You could do this more "efficient" if that's what you're asking.

I would model the command type modifier with bytes, provided that you don't have more than 255 distinct modes:

byte cmd_statistic = 0;
byte cmd_nonstatistic = 1;

Then each ip address could be modeled as 4 bytes, like this:

byte[] ip0 = new byte[]{15, 23, 63, 12};
byte[] ip1 = new byte[]{15, 23, 63, 13};

The action could also be bytes:

byte action_increase = 0;
byte action_decrease = 1;

And if you could model the last parameters as bytes you could get away with just using InputStreams (is) and OutputStreams like this:

// Code for reading, writing is very similar
byte cmd = (byte)is.read();

byte[] ip = new byte[4];
is.read(ip, 0, 4);

byte action = (byte)is.read();
byte extra  = (byte)is.read();

This is also easy to keep in a large byte[] and that is easier to use for encryption



 MORE:


 ? Writing to a file with accented char
 ? Base64 encoding difference between iOS and .NET
 ? Php unlink() returning errors with encoded filepaths containing spaces
 ? Converting string in host character encoding to Unicode in C
 ? Arithmetic Encoding and Decoding Algorithm Python
 ? what's the definition of "encoding-agnostic"?
 ? Run length encoding of arbitrary length substrings
 ? How to Encode a web address
 ? KeyError Pandas Dataframe (encoding index)
 ? Encoding warning in Maven Selenium WebDriver project executing in IntelliJ IDEA