1 ----------------------------------------------------------------
2 -- ZLib for Ada thick binding. --
4 -- Copyright (C) 2002-2003 Dmitriy Anisimkov --
6 -- Open source license information is in the zlib.ads file. --
7 ----------------------------------------------------------------
9 -- $Id: zlib-streams.adb,v 1.10 2004/05/31 10:53:40 vagul Exp $
11 with Ada
.Unchecked_Deallocation
;
13 package body ZLib
.Streams
is
19 procedure Close
(Stream
: in out Stream_Type
) is
20 procedure Free
is new Ada
.Unchecked_Deallocation
21 (Stream_Element_Array
, Buffer_Access
);
23 if Stream
.Mode
= Out_Stream
or Stream
.Mode
= Duplex
then
24 -- We should flush the data written by the writer.
26 Flush
(Stream
, Finish
);
28 Close
(Stream
.Writer
);
31 if Stream
.Mode
= In_Stream
or Stream
.Mode
= Duplex
then
32 Close
(Stream
.Reader
);
42 (Stream
: out Stream_Type
;
43 Mode
: in Stream_Mode
;
44 Back
: in Stream_Access
;
45 Back_Compressed
: in Boolean;
46 Level
: in Compression_Level
:= Default_Compression
;
47 Strategy
: in Strategy_Type
:= Default_Strategy
;
48 Header
: in Header_Type
:= Default
;
49 Read_Buffer_Size
: in Ada
.Streams
.Stream_Element_Offset
50 := Default_Buffer_Size
;
51 Write_Buffer_Size
: in Ada
.Streams
.Stream_Element_Offset
52 := Default_Buffer_Size
)
55 subtype Buffer_Subtype
is Stream_Element_Array
(1 .. Read_Buffer_Size
);
58 (Filter
: in out Filter_Type
;
59 Compress
: in Boolean);
66 (Filter
: in out Filter_Type
;
67 Compress
: in Boolean) is
71 (Filter
, Level
, Strategy
, Header
=> Header
);
73 Inflate_Init
(Filter
, Header
=> Header
);
81 if Mode
= Out_Stream
or Mode
= Duplex
then
82 Init_Filter
(Stream
.Writer
, Back_Compressed
);
83 Stream
.Buffer_Size
:= Write_Buffer_Size
;
85 Stream
.Buffer_Size
:= 0;
88 if Mode
= In_Stream
or Mode
= Duplex
then
89 Init_Filter
(Stream
.Reader
, not Back_Compressed
);
91 Stream
.Buffer
:= new Buffer_Subtype
;
92 Stream
.Rest_First
:= Stream
.Buffer
'Last + 1;
93 Stream
.Rest_Last
:= Stream
.Buffer
'Last;
102 (Stream
: in out Stream_Type
;
103 Mode
: in Flush_Mode
:= Sync_Flush
)
105 Buffer
: Stream_Element_Array
(1 .. Stream
.Buffer_Size
);
106 Last
: Stream_Element_Offset
;
109 Flush
(Stream
.Writer
, Buffer
, Last
, Mode
);
111 Ada
.Streams
.Write
(Stream
.Back
.all, Buffer
(1 .. Last
));
113 exit when Last
< Buffer
'Last;
121 function Is_Open
(Stream
: Stream_Type
) return Boolean is
123 return Is_Open
(Stream
.Reader
) or else Is_Open
(Stream
.Writer
);
131 (Stream
: in out Stream_Type
;
132 Item
: out Stream_Element_Array
;
133 Last
: out Stream_Element_Offset
)
137 (Item
: out Stream_Element_Array
;
138 Last
: out Stream_Element_Offset
);
145 (Item
: out Stream_Element_Array
;
146 Last
: out Stream_Element_Offset
) is
148 Ada
.Streams
.Read
(Stream
.Back
.all, Item
, Last
);
151 procedure Read
is new ZLib
.Read
153 Buffer
=> Stream
.Buffer
.all,
154 Rest_First
=> Stream
.Rest_First
,
155 Rest_Last
=> Stream
.Rest_Last
);
158 Read
(Stream
.Reader
, Item
, Last
);
165 function Read_Total_In
(Stream
: in Stream_Type
) return Count
is
167 return Total_In
(Stream
.Reader
);
174 function Read_Total_Out
(Stream
: in Stream_Type
) return Count
is
176 return Total_Out
(Stream
.Reader
);
184 (Stream
: in out Stream_Type
;
185 Item
: in Stream_Element_Array
)
188 procedure Write
(Item
: in Stream_Element_Array
);
194 procedure Write
(Item
: in Stream_Element_Array
) is
196 Ada
.Streams
.Write
(Stream
.Back
.all, Item
);
199 procedure Write
is new ZLib
.Write
201 Buffer_Size
=> Stream
.Buffer_Size
);
204 Write
(Stream
.Writer
, Item
, No_Flush
);
211 function Write_Total_In
(Stream
: in Stream_Type
) return Count
is
213 return Total_In
(Stream
.Writer
);
216 ---------------------
217 -- Write_Total_Out --
218 ---------------------
220 function Write_Total_Out
(Stream
: in Stream_Type
) return Count
is
222 return Total_Out
(Stream
.Writer
);