Writing to and reading from a data stream