Java: How to create a package with bytes?

I work in a project where I need to read some values ​​and send through a socket connection. This is the package format I have to create: enter image description here

I have to read these values ​​(I don’t know what kind of king it should be each value, int or string, etc.): type: type of operation (how to get only 4 bits?) Reserverd: I will write 0 origin: who sends message recipient: who should receive the message algorithm: which algorithm is goona to encrypt the message padding: use it in the encryption algorithm mode: which mode is for encryption

I have to read these values ​​and create this package, which should have only 7 bytes.

How can i do this?

it should 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?

+3
5

ByteBuffer nio. , Endianess ( ! , , ). . ints , a) ints b) unsigned.

    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; 
    int origin = buffer.getShort() & 0xffff;
    int receiver = buffer.getShort() & 0xffff;
    // and so on

, :

    ByteBuffer buffer = ByteBuffer.allocate(100);
    buffer.rewind();
    buffer.put((byte)(type << 4));
    buffer.putShort((short)origin);
    // and so on

: ByteBuffer Java NIO. . Socket, . , . OutputStream WritableByteChannel ( ) .

InputStream is = socket.getInputStream();
ReadableByteChannel source = Channels.newChannel(istream);
ByteBuffer buffer = ByteBuffer.allocateDirect(headerSize);
source.read(buffer);
// now header is in buffer and can be used as seen above. 
+2

ByteBuffer.

, "shifting". 3 type 7 reserved, 8 , byte, .

byte typeReserved = (3 << 4) | 7;

?

byte type = (typeReserved >> 4) & 0xF;
byte reserved = typeReserved & 0xF;
0

, , Java " ", -128 127, . , , 255.

I am sure that there is a way out, but at that time I simply did not have enough time to understand this.

0
source

you use bit shifts (using <<) and bit shifts or (using |)

new byte[]{
           type<<4|reserverd, 
           origin_1_byte, origin_2_byte, 
           receiver_1_byte, receiver_2_byte, 
           algorithm<<4 | padding, 
           mode};

to extract them, you need masks and shifts ( buff[0]&0xf0)>>>4will extract the type, for example)

0
source

ByteBuffer, as suggested in other answers, or DataOutputStream.

0
source

All Articles