Ada Programming/Libraries/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 to allow a sequence of elements comprising values from possibly different types and allowing sequential access to these values. 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.
 * procedure Write
 * procedure 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  and   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:
 * 1) Creates read and write procedures for types
 * 2) 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  attribute for the   on the value of the date to be written:

For a simple record such as  the predefined   attribute simply calls the write attributes for each component in order.

The programmer can provide a version of  to output the month name as, say, an integer:

First, define the date write procedure and associate  attribute

Note that, instead of redefining, we could have simply redefined  , and this would be called indirectly.

Specification
Stream_Access   Root_Stream_Type File_Type File_Mode In_File Out_File Append_File Count           0  implementation_defined Positive_Count Count  1  Count Create File   File_Type Mode  File_Mode  Out_File Name  String     "" Form  String     "" Open File   File_Type Mode  File_Mode Name  String Form  String  "" Close File    File_Type Delete File   File_Type Reset File    File_Type Mode   File_Mode Reset File    File_Type Mode File  File_Type  File_Mode Name File  File_Type  String Form File  File_Type  String Is_Open    File   File_Type  Boolean End_Of_File File  File_Type  Boolean Stream File  File_Type  Stream_Access Read File   File_Type Item  Stream_Element_Array Last  Stream_Element_Offset From   Positive_Count Read File   File_Type Item  Stream_Element_Array Last  Stream_Element_Offset Write File  File_Type Item  Stream_Element_Array To    Positive_Count Write File  File_Type Item  Stream_Element_Array Set_IndexFile  File_Type To   Positive_Count IndexFile  File_Type  Positive_Count Size File  File_Type  Count Set_ModeFile   File_Type Mode   File_Mode FlushFile   File_Type Status_Error   IO_ExceptionsStatus_Error Mode_Error     IO_ExceptionsMode_Error Name_Error     IO_ExceptionsName_Error Use_Error      IO_ExceptionsUse_Error Device_Error   IO_ExceptionsDevice_Error End_Error      IO_ExceptionsEnd_Error Data_Error     IO_ExceptionsData_Error Import Ada File_Type AdaStreamsStream_IO

Wikibook

 * Ada Programming/Libraries/Ada