简体   繁体   English

java如何在objectinputstream中的readObject知道要读取多少字节?

[英]java how does readObject in objectinputstream knows how many bytes to read?

In socket I/O, may I know how does a objectinputstream readObject knows how many bytes to read? 在套接字I / O中,我是否可以知道objectinputstream readObject如何知道要读取多少字节? Is the content length encapsulated inside the bytes itself or does it simply reads all the available bytes in the buffer itself? 内容长度是封装在字节本身内部还是仅读取缓冲区本身中的所有可用字节?

I am asking this because I was referring to the Python socket how-to and it says 我问这个问题是因为我指的是Python套接字的使用方法,它说

Now if you think about that a bit, you'll come to realize a fundamental truth of sockets: messages must either be fixed length (yuck), or be delimited (shrug), or indicate how long they are (much better), or end by shutting down the connection. 现在,如果您考虑一下,您将认识到套接字的基本原理:消息必须是固定长度(讨厌)或定界(耸耸肩),或者指出它们有多长(更好),或者通过关闭连接来结束。 The choice is entirely yours, (but some ways are righter than others). 选择完全是您的选择(但某些方法比其他方法更正确)。

However in another SO answer , @DavidCrawshaw mentioned that ` 但是,在另一个SO 答案中 ,@ DavidCrawshaw提到了`

So readObject() does not know how much data it will read, so it does not know how many objects are available. 因此readObject()不知道它将读取多少数据,因此也不知道有多少可用对象。

I am interested to know how it works... 我有兴趣知道它是如何工作的...

You're over-interpreting the answer you cited. 您过度解释了您引用的答案。 readObject() doesn't know how many bytes it will read, ahead of time, but once it starts reading it is just parsing an input stream according to a protocol, that consists of tags, primitive values, and objects, which in turn consist of tags, primitive values, and other objects. readObject()不知道提前读取多少字节,但是一旦开始读取,它只是根据协议来解析输入流,该协议由标签,原始值和对象组成,而这些对象又由标签,原始值和对象组成标签,原始值和其他对象的集合。 It doesn't have to know ahead of time. 它不必提前知道。 Consider the similar-ish case of XML. 考虑类似XML的情况。 You don't know how long the document will be ahead of time, or each element, but you know when you've read it all, because the protocol tells you. 您不知道文档或每个元素要提前多长时间,但是您知道何时阅读所有文档,因为协议会告诉您。

The readOject() method is using BlockedInputStream to read the byte.If you check the readObject of ObjectInputStream , it is calling readOject()方法使用BlockedInputStream读取字节。如果检查ObjectInputStreamreadObject ,则在调用

readObject0(false).

private Object readObject0(boolean unshared) throws IOException {
    boolean oldMode = bin.getBlockDataMode();
    if (oldMode) {
        int remain = bin.currentBlockRemaining();
        if (remain > 0) {
        throw new OptionalDataException(remain);
        } else if (defaultDataEnd) {
        /*
         * Fix for 4360508: stream is currently at the end of a field
         * value block written via default serialization; since there
         * is no terminating TC_ENDBLOCKDATA tag, simulate
         * end-of-custom-data behavior explicitly.
         */
        throw new OptionalDataException(true);
        }
        bin.setBlockDataMode(false);
    }

    byte tc;
    while ((tc = bin.peekByte()) == TC_RESET) {
        bin.readByte();
        handleReset();
    }

which is reading from the stream is using bin.readByte(). 从流中读取的是使用bin.readByte(). bin is BlockiedDataInputStream which in turns use PeekInputStream to read it.This class finally is using InputStream.read(). bin是BlockiedDataInputStream ,它BlockiedDataInputStream使用PeekInputStream读取它。 PeekInputStream最终使用InputStream.read()。 From the description of the read method: 从read方法的描述中:

/**
     * Reads the next byte of data from the input stream. The value byte is
     * returned as an <code>int</code> in the range <code>0</code> to
     * <code>255</code>. If no byte is available because the end of the stream
     * has been reached, the value <code>-1</code> is returned. This method
     * blocks until input data is available, the end of the stream is detected,
     * or an exception is thrown.

So basically it reads byte after byte until it encounters -1.So As EJP mentioned, it never know ahead of time how many bytes are there to be read. 因此,基本上它会逐字节读取直到遇到-1。因此正如EJP所述,它永远不会提前知道要读取多少个字节。 Hope this will help you in understanfing it. 希望这对您有所帮助。

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

相关问题 Java如何用ObjectInputStream读取 - Java how to read with ObjectInputStream 如何在ObjectInputStream.readObject中绕过ClassNotFoundException? - How to bypass ClassNotFoundException while ObjectInputStream.readObject? Java ObjectInputStream readObject引发NullPointerException - Java ObjectInputStream readObject Throws NullPointerException 如何使用Java中的ObjectInputStream读取不同的Object - How to read different Object using ObjectInputStream in Java 如何使用 ObjectInputStream 读取 java 中的多个对象? - How to read multiple objects in java using ObjectInputStream? BufferedReader使用.read()方法真正读取多少字节? - How many bytes does BufferedReader really read using .read() method? ObjectInputStream.readObject 不检索对象 - ObjectInputStream.readObject does not retrieve object Java序列化,ObjectInputStream.readObject(),检查是否会阻塞 - Java serialization, ObjectInputStream.readObject(), check if will block 在 Java 套接字中使用 ObjectInputStream 不读取任何内容 - does not read anything using ObjectInputStream in Java socket 如何在Java中使用ObjectInputStream从文件中读取对象列表? - How to read a List of List of Objects from a file using ObjectInputStream in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM