Collect a stream into a buffer.