套接字和DataInputStream

zig*_*ggy 3 java sockets datainputstream

我试图理解这段代码

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


        int     messageLength   = stream.readInt();
        char    recordType      = (char) stream.readByte();
        byte    padding         = stream.readByte();
        short   numberRecords   = stream.readShort();
Run Code Online (Sandbox Code Playgroud)

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

第一个语句不会从字节数组的开头读取一个int而下一个语句从字节数组的开头读取一个字节吗?究竟是如何知道从哪个点读取字节,整数,短路等?

NPE*_*NPE 5

文档:

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

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