Flatten serialization.

Fix a bug that occurs if InputStream.read() returns before reading the
requested number of bytes.