Java – socket and datainputstream

I'm trying to understand this 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 initialized to a new byte [32768] through socket The read () method populates the What I don't understand is that once messagelength is initialized to stream Readint(), how will the second statement work, that is, recordtype?

Won't 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 do you know from which point to read bytes, integers, short circuits, etc?

Solution

From documentation:

In other words, datainputstream just reads from bytearrayinputstream, which remembers the current position in the byte array and advances it every time some data is read

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>