Optimize ByteArrayOutputStream usage, close #1168