套接字和数据输入流

发布于 2024-11-27 20:03:41 字数 596 浏览 2 评论 0原文

我试图理解这段代码

        DataInputStream stream = 
          new DataInputStream(
            new ByteArrayInputStream(messageBuffer));


        int     messageLength   = stream.readInt();
        char    recordType      = (char) stream.readByte();
        byte    padding         = stream.readByte();
        short   numberRecords   = stream.readShort();

messageBuffer 被初始化为新字节[32768],通过 Socket.read() 方法填充。 我不明白的是,一旦 messageLength 被初始化为stream.readInt(),第二条语句将如何工作,即recordType?

第一个语句不会从字节数组的开头读取一个 int ,而下一个语句不会从字节数组的开头读取一个字节吗?它到底如何知道从哪个点读取字节、整数、短整型等?

I am trying to understand this snippet of code

        DataInputStream stream = 
          new DataInputStream(
            new ByteArrayInputStream(messageBuffer));


        int     messageLength   = stream.readInt();
        char    recordType      = (char) stream.readByte();
        byte    padding         = stream.readByte();
        short   numberRecords   = stream.readShort();

messageBuffer is initialised as new byte[32768] as is populated via a Socket.read() method.
What i dont understand is once messageLength has been initialised to stream.readInt(), how will the second second statement work i.e. recordType?

Wouldnt the first statement read an int from the beginning of the byte array and the next statement read a byte from the beginning of the byte array? How exactly does it know from which point to read the bytes, ints, shorts etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

来世叙缘 2024-12-04 20:03:41

来自文档

ByteArrayInputStream 包含一个包含字节的内部缓冲区
可以从流中读取。 内部计数器跟踪
read 方法提供的下一个字节。

换句话说,DataInputStream 只是从 ByteArrayInputStream 读取,而后者记住字节数组中的当前位置,并在每次读取某些数据时前进。

From the documentation:

A ByteArrayInputStream contains an internal buffer that contains bytes
that may be read from the stream. An internal counter keeps track of
the next byte to be supplied by the read method.

In other words, DataInputStream simply reads from the ByteArrayInputStream, while the latter remembers the current position in the byte array and advances it every time some data has been read.

顾冷 2024-12-04 20:03:41

readX() 不会从流的开头读取。事实上,术语用于表示随着时间的推移而可用的数据序列。这意味着从流中的后续读取将检索不同的元素。

将流视为信息传送带而不是数组。

readX() doesn't read from the beginning of the stream. In fact the term stream is used to denote a sequence of data made available over time. That means subsequent reads from a stream will retrieve different elements.

Think of a stream as a conveyor belt of information rather than an array.

手心的温暖 2024-12-04 20:03:41

DataInputStream.read* 方法消耗底层输入流中的字节。在这种情况下,read* 方法读取 ByteArrayInputStream 提供的下一个可用字节,该字节将跟踪数组中的当前位置。


作为旁注,您可能需要考虑使用 ByteBuffer.wrap 和各种 ByteBuffer.read 方法:

ByteBuffer msgBuf = ByteBuffer.wrap(messageBuffer);
int messageLength = msgBuf.getInt();
char recordType   = msgBuf.getChar();
...

The DataInputStream.read* methods consume bytes from the underlying input stream. In this case the read* methods read the next available bytes provided by the ByteArrayInputStream which will keep track of the current position in the array.


As a side-note, you may want to consider using ByteBuffer.wrap and the various ByteBuffer.read methods:

ByteBuffer msgBuf = ByteBuffer.wrap(messageBuffer);
int messageLength = msgBuf.getInt();
char recordType   = msgBuf.getChar();
...
空心↖ 2024-12-04 20:03:41

Socket.read() 将读取可用的字节。最小为一字节!最大值是缓冲区大小,其中可以包含任意数量的消息。

使用 DataInputStream/BufferedInputStream 比手动读取缓冲区更安全、更简单、更高效。

// create an input stream once per socket.
DataInputStream stream = 
      new DataInputStream(
        new BufferedInputStream(socket.getInputStream()));


int     messageLength   = stream.readInt();
char    recordType      = (char) stream.readByte();
byte    padding         = stream.readByte();
short   numberRecords   = stream.readShort();

Socket.read() will read the bytes which are available. The minimum is one byte! The maximum is the buffer size which could have any number of messages in it.

Instead of reading a buffer manually, it is safer, simpler and more efficient to use a DataInputStream/BufferedInputStream.

// create an input stream once per socket.
DataInputStream stream = 
      new DataInputStream(
        new BufferedInputStream(socket.getInputStream()));


int     messageLength   = stream.readInt();
char    recordType      = (char) stream.readByte();
byte    padding         = stream.readByte();
short   numberRecords   = stream.readShort();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文