# buffered_streams.ads -rw-r--r-- 2.6 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pragma Ada_2012;

with Ada.IO_Exceptions;
with Ada.Streams;

-- Implements a stream type that wraps another stream, buffering the inputs and
-- outputs.
package Buffered_Streams with Pure is

	-- Object representing a buffered stream.
	-- Buffer_Size is the size of the underlying buffers; note that there are
	-- two buffers that are both Buffer_Size: one for buffering incoming data
	-- and one for buffering outgoing data.
	-- Stream is the stream to wrap.
	type Buffered_Stream
		(Buffer_Size : Ada.Streams.Stream_Element_Offset;
		Stream : not null access Ada.Streams.Root_Stream_Type'Class)
		is new Ada.Streams.Root_Stream_Type with private;

	-- Make Buffer wrap the given Stream.  Flushes any existing buffers and
	-- resets all internal state.
	-- procedure Wrap
	-- 	(Buffer : in out Buffered_Stream;
	-- 	Stream : not null access Ada.Streams.Root_Stream_Type'Class);

	-- Flush the output buffer to the underlying stream.  The write is done
	-- with one single Ada.Streams.Write call.
	procedure Flush (Stream : in out Buffered_Stream);

	-- Refill the input buffer by reading from the underlying stream.
	-- Preserves data that has not been Read from the buffer yet.
	-- Not automatically called when initializing a Buffered_Stream, so if
	-- neither this nor Stream.Refill_When_Empty(True) are called before the
	-- first Read, the stream will appear empty.
	procedure Refill (Stream : in out Buffered_Stream);

	-- If the output buffer should be flushed to the underlying stream when the
	-- buffer is full.
	-- If this is never called then defaults to False.
	procedure Flush_When_Full
		(Stream : in out Buffered_Stream; Flush : Boolean := True);

	-- If when the input buffer runs empty it should be automatically refilled.
	-- If this is never called then defaults to False.
	procedure Refill_When_Empty
		(Stream : in out Buffered_Stream; Refill : Boolean := True);

	-- Read from the input buffer.  If Refill_When_Empty is False, then the
	-- stream will appear empty once the underlying buffer is empty.
	overriding procedure Read
		(Stream : in out Buffered_Stream;
		Item : out Ada.Streams.Stream_Element_Array;
		Last : out Ada.Streams.Stream_Element_Offset);

	-- Write to the output buffer.  If Flush_When_Full is False, then End_Error
	-- will be raised once the underlying buffer is full.
	overriding procedure Write
		(Stream : in out Buffered_Stream;
		Item : Ada.Streams.Stream_Element_Array);

	-- Raised by Write if the output buffer is full but Flush_When_Full was not
	-- set to True.
	End_Error : exception renames Ada.IO_Exceptions.End_Error;


private
	-- <>

end Buffered_Streams;