How is it 13 bytes?

Two quotes:

All other messages in the protocol take the form <length prefix><message ID><payload>. The length prefix is ​​the four-byte value of a large number. The message identifier is one decimal byte. The payload depends on the message.

request: <len=0013><id=6><index><begin><length> 

The request message is a fixed length and is used to request a block. The payload contains the following information:

  • index: an integer specifying the index of pieces based on zero
  • begin: an integer indicating the byte offset based on zero inside the fragment
  • length: an integer specifying the desired length.

When I write everything, it sums up to 5 bytes. Using

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byteStream.write( 13 );
byteStream.write( 6 );
byteStream.write( index );
byteStream.write( begin );
byteStream.write( length );

message = byteStream.toByteArray();

EDIT: Sorry, I was very angry when I wrote. its bitoral protocol. Using this spec .

+3
4

write() .

char int, 8- 0xFF.

DataOutputStream (writeInt, writeShort ..), , Integer.reverseBytes() ( Short.reverseBytes()) writeXYZ().

ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

DataOutputStream dout = new DataOutputStream(byteStream);

dout.writeInt( 0x13 ); // L:4
dout.write( 6 ); // L:5
dout.writeShort( index ); // guess, L:7
dout.writeLong( begin ); // >4GB support? L:15
dout.writeInt( length ); // clients accept below to 2^17, L:19

dout.flush(); // to be sure

message = byteStream.toByteArray();

. index, begin length. .

2: , D.Shawley, .

+6

, ... , <index>, <begin> <length>. , 4- , 1- .

, , , <length>, <length> +5 , <length>. , 1- 0x06. , :

  • , , <index><begin><length>, , 14
  • , 0x0013 19 .

, , , , .

+2

write() . 5 write() 5 .

+1

. write(int b).

. , . b. 24 b .

OutputStream .

:     b - .

+1

Source: https://habr.com/ru/post/1711300/


All Articles