简体   繁体   English

这13个字节有多长?

[英]How is this 13 bytes long?

Two quotes: 两个引号:

All of the remaining messages in the protocol take the form of <length prefix><message ID><payload> . 协议中的所有剩余消息采用<length prefix><message ID><payload> The length prefix is a four byte big-endian value. 长度前缀是一个四字节的大端值。 The message ID is a single decimal byte. 消息ID是单个十进制字节。 The payload is message dependent. 有效负载取决于消息。

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

The request message is fixed length, and is used to request a block. 请求消息是固定长度的,用于请求块。 The payload contains the following information: 有效负载包含以下信息:

  • index: integer specifying the zero-based piece index index:整数,指定从零开始的片段索引
  • begin: integer specifying the zero-based byte offset within the piece begin:整数,指定片段中从零开始的字节偏移量
  • length: integer specifying the requested length. length:指定请求长度的整数。

When I write everything it sums up to 5 bytes. 当我写所有内容时,它总计最多5个字节。 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 kind of pissed when i wrote it. 编辑:对不起我写的时候有点生气。 its the bittorent protocol. 它的bittorent协议。 Using this spec . 使用此规范

The write() method writes one byte. write()方法写入一个字节。

If you send it a char or int it just strips everything above the 8th bit with & 0xFF. 如果你发送一个char或int它只是用&0xFF剥离第8位以上的所有内容。

You have more options with DataOutputStream (writeInt, writeShort etc.) but it uses big endian byte order so you might need to perform an Integer.reverseBytes() (or Short.reverseBytes()) call before passing in the value to the writeXYZ() method. DataOutputStream(writeInt,writeShort等)有更多选项,但它使用大端字节顺序,因此您可能需要在将值传递给writeXYZ之前执行Integer.reverseBytes()(或Short.reverseBytes())调用( ) 方法。

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();

Remark: The spec doesn't state the length of index , begin and length . 备注:规范没有说明index的长度, beginlength I just wanted to give a sample of the available options. 我只想提供可用选项的示例。

Edit 2: Edited the sample based on D.Shawley's answer and a spec found here . 编辑2:根据D.Shawley的答案和此处的规范编辑样本。

I'm not sure what you are getting at here... the quoted text doesn't say what the length of <index> , <begin> , or <length> is. 我不确定你在这里得到什么...引用的文字没有说明<index><begin><length>的长度是多少。 The first quote states, rather clearly, that a message consists of a 4-byte length, followed by a 1-byte identifier, and an arbitrary payload. 第一个引用相当清楚地表明消息由4字节长度,后跟1字节标识符和任意有效负载组成。

The length of the payload is probably either the value specified as <length> or <length> +5 depending on exactly what <length> means. 有效负载的长度可能是指定为<length><length> +5的值,具体取决于<length>含义。 The second quote looks like the definition of whatever message is identified by the 1-byte identifier of 0x06. 第二个引用看起来像是由1字节标识符0x06标识的任何消息的定义。 I would guess that: 我猜是这样的:

  1. the payload, the bytes that make up <index><begin><length> , is probably 14 bytes long 有效载荷,构成<index><begin><length>字节大概是14个字节长
  2. the length is being displayed in hex so 0x0013 is 19 decimal 长度以十六进制显示,因此0x0013是十进制的19

In any case, the byte stream that you are generating does not seem to match the message definition AND the message definition lacks clarity. 在任何情况下,您生成的字节流似乎与消息定义不匹配并且消息定义缺乏清晰度。

write() writes bytes. write()写入字节。 5 write()'s produces 5 bytes. 5 write()产生5个字节。

See write(int b) . write(int b)

Writes the specified byte to this output stream. 将指定的字节写入此输出流。 The general contract for write is that one byte is written to the output stream. 写入的一般合同是将一个字节写入输出流。 The byte to be written is the eight low-order bits of the argument b. 要写入的字节是参数b的八个低位。 The 24 high-order bits of b are ignored. b的24个高位被忽略。

Subclasses of OutputStream must provide an implementation for this method. OutputStream子类必须为此方法提供实现。

Parameters: b - the byte. 参数:b - 字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM