Flatten serialization.

Fix a bug that occurs if InputStream.read() returns before reading the
full content of the stream.