i’m working in a project where i need to read some values and send through socket connection.
This is format of the package that i must create:

i must read these values (i don’t know what king of type it must be each value, int or string, etc):
type : type of operation (how get 4 bits only ? )
reserverd : i will fill with 0’s
origin: who is sending the message
receiver: who is to receive the message
algorithm: which algorithm is goona to be used to encrypt the message
padding: use it in the encrypt algorithm
mode : which mode to encrypt
i must read this values and create this package that must have 7 bytes only.
how can i do that ?
it must be something like this i think:
byte[] r = new byte[]{
type+reserverd,
origin_1_byte, origin_2_byte,
receiver_1_byte, receiver_2_byte,
algorithm+padding,
mode};
UPDATE:
ByteBuffer buffer = ByteBuffer.allocate(100);
// read data into buffer
buffer.rewind();
buffer.order(ByteOrder.LITTLE_ENDIAN);
// 0xf and co mask off sign extension -> handle byte as unsigned
int type = (buffer.get() >> 4) & 0xf; // 15 in decimal
buffer.rewind();
buffer.put((byte)(type << 4));
System.out.println("type = " + type);
output : 0 (why ?)
Any ideas ?
Just use a ByteBuffer and the nio package. That way you can easily read the data from the network and don’t have to worry about Endianess (that’s important! If you use streams you pretty much have to implement the shifting yourself – although that could be a good exercise). Don’t forget to set the buffers endianness to the correct value. We use ints internally because a) all mathematical operations return ints anyway and b) you are able to handle the values as unsigned.
to write the data back you pretty much do the reverse:
Edit:
Usually you’d read the data directly into a ByteBuffer from the Network channel and use the Java NIO package. A quick tutorial is here. But since you already have the Socket we’ll make it a bit easier. Note that I’ve ignored all error checking for breviety. Using an OutputStream and WritableByteChannel (with write) works in the other direction.