<p><code>readOject()</code>方法正在使用BlockedInputStream读取字节。如果检查<code>ObjectInputStream</code>的<code>readObject</code>,它正在调用</p>
<pre><code>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();
}
</code></pre>
<p>从流中读取的是使用<code>bin.readByte().</code>bin is <code>BlockiedDataInputStream</code>,后者反过来使用<code>PeekInputStream</code>来读取它。这个类最终正在使用输入流.read().
根据read方法的描述:</p>
^{pr2}$
<p>所以基本上它读取一个又一个字节,直到遇到-1。所以正如ejb提到的,它永远不会提前知道有多少字节需要读取。希望这能帮助你理解它。在</p>