@@ -76,6 +76,37 @@ public void DelayedHeaderWriteNoData()
7676 Assert . IsTrue ( data . Length > 0 ) ;
7777 }
7878
79+
80+ /// <summary>
81+ /// Variant of DelayedHeaderWriteNoData testing flushing for https://github.com/icsharpcode/SharpZipLib/issues/382
82+ /// </summary>
83+ [ Test ]
84+ [ Category ( "GZip" ) ]
85+ public void DelayedHeaderWriteFlushNoData ( )
86+ {
87+ var ms = new MemoryStream ( ) ;
88+ Assert . AreEqual ( 0 , ms . Length ) ;
89+
90+ using ( GZipOutputStream outStream = new GZipOutputStream ( ms ) { IsStreamOwner = false } )
91+ {
92+ // #382 - test flushing the stream before writing to it.
93+ outStream . Flush ( ) ;
94+ }
95+
96+ ms . Seek ( 0 , SeekOrigin . Begin ) ;
97+
98+ // Test that the gzip stream can be read
99+ var readStream = new MemoryStream ( ) ;
100+ using ( GZipInputStream inStream = new GZipInputStream ( ms ) )
101+ {
102+ inStream . CopyTo ( readStream ) ;
103+ }
104+
105+ byte [ ] data = readStream . ToArray ( ) ;
106+
107+ Assert . That ( data , Is . Empty , "Should not have any decompressed data" ) ;
108+ }
109+
79110 /// <summary>
80111 /// Writing GZip headers is delayed so that this stream can be used with HTTP/IIS.
81112 /// </summary>
@@ -99,6 +130,38 @@ public void DelayedHeaderWriteWithData()
99130 Assert . IsTrue ( data . Length > 0 ) ;
100131 }
101132
133+ /// <summary>
134+ /// variant of DelayedHeaderWriteWithData to test https://github.com/icsharpcode/SharpZipLib/issues/382
135+ /// </summary>
136+ [ Test ]
137+ [ Category ( "GZip" ) ]
138+ public void DelayedHeaderWriteFlushWithData ( )
139+ {
140+ var ms = new MemoryStream ( ) ;
141+ Assert . AreEqual ( 0 , ms . Length ) ;
142+ using ( GZipOutputStream outStream = new GZipOutputStream ( ms ) { IsStreamOwner = false } )
143+ {
144+ Assert . AreEqual ( 0 , ms . Length ) ;
145+
146+ // #382 - test flushing the stream before writing to it.
147+ outStream . Flush ( ) ;
148+ outStream . WriteByte ( 45 ) ;
149+ }
150+
151+ ms . Seek ( 0 , SeekOrigin . Begin ) ;
152+
153+ // Test that the gzip stream can be read
154+ var readStream = new MemoryStream ( ) ;
155+ using ( GZipInputStream inStream = new GZipInputStream ( ms ) )
156+ {
157+ inStream . CopyTo ( readStream ) ;
158+ }
159+
160+ // Check that the data was read
161+ byte [ ] data = readStream . ToArray ( ) ;
162+ CollectionAssert . AreEqual ( new byte [ ] { 45 } , data , "Decompressed data should match initial data" ) ;
163+ }
164+
102165 [ Test ]
103166 [ Category ( "GZip" ) ]
104167 public void ZeroLengthInputStream ( )
0 commit comments