1. package Streams.Buffers is 
  2.  
  3.     -- A Buffer_Stream is a stream implementation backed by a read-only buffer. 
  4.     -- The stream can be wrapped around an existing buffer to read its contents 
  5.     -- (use Stream constructor function), or a buffer can be consumed into the 
  6.     -- stream and freed automatically when the stream is closed (use Open_Stream 
  7.     -- constructor procedure). 
  8.     type Buffer_Stream is new Root_Stream_Type with private; 
  9.     type A_Buffer_Stream is access all Buffer_Stream'Class; 
  10.  
  11.     -- Creates a Buffer_Stream backed by 'buffer', which is consumed by this 
  12.     -- procedure. Closing the stream will deallocate the buffer. 
  13.     procedure Open_Stream( buffer : in out A_SEA; stream : out A_Buffer_Stream ); 
  14.     pragma Precondition( buffer /= null ); 
  15.     pragma Postcondition( buffer = null ); 
  16.     pragma Postcondition( stream /= null ); 
  17.  
  18.     -- Creates a Buffer_Stream wrapper around 'buffer'. The stream depends on 
  19.     -- the existance of buffer so be sure to call Close on the stream before 
  20.     -- deallocating the buffer. 
  21.     function Stream( buffer : not null A_SEA ) return A_Buffer_Stream; 
  22.     pragma Postcondition( Stream'Result /= null ); 
  23.  
  24.     -- Closes the stream and deallocates its buffer (if it had ownership of it, 
  25.     -- given by calling Open_Stream.) 
  26.     procedure Close( stream : in out A_Buffer_Stream ); 
  27.     pragma Postcondition( stream = null ); 
  28.  
  29. private 
  30.  
  31.     type Buffer_Stream is new Root_Stream_Type with record 
  32.         buffer  : A_SEA := null; 
  33.         freeBuf : Boolean := False;  -- True if the stream's underlying 
  34.                                      -- buffer was consumed by the stream and 
  35.                                      -- should be freed when the stream is 
  36.                                      -- closed. 
  37.         readPos : Stream_Element_Offset; 
  38.     end record; 
  39.  
  40.     -- 'last' will be set to item'First - 1 when the stream is empty. 
  41.     procedure Read( stream : in out Buffer_Stream; 
  42.                     item   : out Stream_Element_Array; 
  43.                     last   : out Stream_Element_Offset ); 
  44.  
  45.     -- Writing is not supported. Raises STREAM_ERROR. 
  46.     procedure Write( stream : in out Buffer_Stream; 
  47.                      item   : Stream_Element_Array ); 
  48.  
  49. end Streams.Buffers;