# Ada Programming/Libraries/Ada.Streams.Stream IO

This language feature is available from Ada 95 on.
Ada.Streams.Stream_IO is a unit of the Predefined Language Environment since Ada 95.

## Description

“The packages Sequential_IO and Direct_IO have not proved to be sufficiently flexible for some applications because they process only homogenous files.” Ada 95 Rationale: Annex A.4.1 Stream Input and Output
“[Serialization is] the ability to write the complete state of an object (including any objects it refers to) to an output stream, and then recreate that object at some later time by reading its serialized state from an input stream.” Java in a Nutshell, David Flanagan, 2nd ed, O’Reilly & Associates, Inc., 1997, p. 97.
A stream is an entity that can consume or produce elements of raw data, typically bytes.

### Ada's Solution

Like Java, Ada 95 provides package Ada.Streams to allow a sequence of elements comprising values from possibly different types and allowing sequential access to these values.
* procedure Write
* procedure Read
Every type has a default encode–and–write procedure named by the attribute T'Write and a default read–and–decode procedure named by the attribute T'Read.
For example, suppose an input file mixed.data contains several groups of Float values in internal form, with each group preceded by an Integer value giving the number of values in the group. The following program writes the sum of each group in textual form to the standard output file (Ada as a Second Language, 2nd ed., Cohen, p 794 – corrected from Errata):

## Streams for Limited or User-defined Types

The programmer can override the 'Write and 'Read attributes for types whose default procedures do not provide the desired behavior. These types are typically access types or composite types with access–type subcomponents.
Typically, the programmer does not call the Read and Write procedures manipulating sequences of raw data directly. Instead, the programmer:
* Creates read and write procedures for types
* Associates procedures with corresponding attributes

### Example

Encoding a date record (from Ada 95 Rationale, Annex A.4.1, p A–27)
Then, the programmer creates an output file normally, and obtains access to associated date stream.
To output the data, the programmer invokes the Write attribute for the Date_Record on the value of the date to be written:
For a simple record such as Date_Record the predefined Write attribute simply calls the write attributes for each component in order.
The programmer can provide a version of Write to output the month name as, say, an integer:
First, define the date write procedure and associate 'Write attribute
Note that, instead of redefining Date_Record'Write, we could have simply redefined Month_Name'Write, and this would be called indirectly.

## See also
### Wikibook

* Ada Programming/Libraries/Ada

### External examples

* Search for examples of Ada.Streams.Stream_IO in: Rosetta Code, GitHub (gists), any Alire crate or this Wikibook.
* Search for posts related to Ada.Streams.Stream_IO in: Stack Overflow, comp.lang.ada or any Ada related page.

### Ada Reference Manual
#### Ada 95

* A.12.1: The Package Streams.Stream_IO

#### Ada 2005

* A.12.1: The Package Streams.Stream_IO

#### Ada 2012

* A.12.1: The Package Streams.Stream_IO

### Open-Source Implementations

FSF GNAT
* Specification: a-ststio.ads
* Body: a-ststio.adb
drake
* Specification: streams/a-ststio.ads
* Body: streams/a-ststio.adb