Streams in Programming
A data stream is a countable infinite sequence of items and is used to represent data items that are available over time. Examples are sensor readings in an environmental monitoring application, stock prices in financial applications, or network data in computer monitoring.
A stream can be read-only, write-only, or read-write.
The contents of a stream is said to be consumed or added to, all in sequential order.
Sometimes random access is provided through cursors into a stream.
There are a few key characteristics of streams:
- Data elements are produced and made available one at a time, rather than all at once.
- The data elements in a stream are typically processed and consumed as they are produced, rather than being stored for later processing.
- Streams can be unbounded, meaning they have no fixed end, or they can be finite, with a fixed number of data elements.
Types of Streams
There are many types of streams in computer systems, including file streams, network streams, and memory streams. File streams allow you to read and write data to and from files on a storage device. Network streams allow you to read and write data over a network connection. Memory streams allow you to read and write data to and from a memory buffer.
According to the nature of their items, streams are classified as:
- Byte streams
- Byte streams are streams that transfer data in the form of bytes, which are the basic unit of data in a computer system. Byte streams are the most general-purpose type of stream, and can be used to transfer data of any type.
- Character streams
- Character streams are streams that transfer data in the form of characters, which are used to represent text. Character streams are often used for working with text data, and can automatically handle tasks such as character encoding and decoding.
- Buffered streams
- Buffered streams are streams that use a buffer to store data temporarily, in order to improve the efficiency of data transfer. Buffered streams can reduce the number of read and write operations that are performed, and can improve the performance of a system.
- Filtered streams
- Filtered streams are streams that apply a transformation to the data that is being transferred. Filtered streams can be used to perform tasks such as compression, encryption, and decryption.
- Object streams
- Object streams are streams that transfer data in the form of objects, which are complex data structures that can contain a variety of data types. Object streams can be used to transfer objects between different parts of a system, or between systems.
- Parallel processing
- It allows you to create a pipeline of streams that can be used to process data in parallel