@@ -11,32 +11,34 @@ internal class WrappedStream : Stream
11
11
#region fields
12
12
13
13
private readonly Stream _baseStream ;
14
- private readonly EventHandler _onClosed ;
15
- private Boolean _canRead , _canWrite , _canSeek ;
16
- private Boolean _isDisposed ;
17
14
private readonly Boolean _closeBaseStream ;
18
15
16
+ // Delegate that will be invoked on stream disposing
17
+ private readonly Action < ZipArchiveEntry > _onClosed ;
18
+
19
+ // Instance that will be passed to _onClose delegate
20
+ private readonly ZipArchiveEntry _zipArchiveEntry ;
21
+ private Boolean _isDisposed ;
22
+
19
23
#endregion
20
24
21
25
#region constructors
22
26
23
- internal WrappedStream ( Stream baseStream , Boolean canRead , Boolean canWrite , Boolean canSeek , EventHandler onClosed )
24
- : this ( baseStream , canRead , canWrite , canSeek , false , onClosed )
27
+ internal WrappedStream ( Stream baseStream , Boolean closeBaseStream )
28
+ : this ( baseStream , closeBaseStream , null , null )
25
29
{ }
26
30
27
- internal WrappedStream ( Stream baseStream , Boolean canRead , Boolean canWrite , Boolean canSeek , Boolean closeBaseStream , EventHandler onClosed )
31
+ private WrappedStream ( Stream baseStream , Boolean closeBaseStream , ZipArchiveEntry entry , Action < ZipArchiveEntry > onClosed )
28
32
{
29
33
_baseStream = baseStream ;
34
+ _closeBaseStream = closeBaseStream ;
30
35
_onClosed = onClosed ;
31
- _canRead = canRead ;
32
- _canSeek = canSeek ;
33
- _canWrite = canWrite ;
36
+ _zipArchiveEntry = entry ;
34
37
_isDisposed = false ;
35
- _closeBaseStream = closeBaseStream ;
36
38
}
37
39
38
- internal WrappedStream ( Stream baseStream , EventHandler onClosed )
39
- : this ( baseStream , true , true , true , onClosed )
40
+ internal WrappedStream ( Stream baseStream , ZipArchiveEntry entry , Action < ZipArchiveEntry > onClosed )
41
+ : this ( baseStream , false , entry , onClosed )
40
42
{ }
41
43
42
44
#endregion
@@ -68,11 +70,11 @@ public override long Position
68
70
}
69
71
}
70
72
71
- public override bool CanRead { get { return _canRead && _baseStream . CanRead ; } }
73
+ public override bool CanRead { get { return ! _isDisposed && _baseStream . CanRead ; } }
72
74
73
- public override bool CanSeek { get { return _canSeek && _baseStream . CanSeek ; } }
75
+ public override bool CanSeek { get { return ! _isDisposed && _baseStream . CanSeek ; } }
74
76
75
- public override bool CanWrite { get { return _canWrite && _baseStream . CanWrite ; } }
77
+ public override bool CanWrite { get { return ! _isDisposed && _baseStream . CanWrite ; } }
76
78
77
79
#endregion
78
80
@@ -145,14 +147,11 @@ protected override void Dispose(bool disposing)
145
147
if ( disposing && ! _isDisposed )
146
148
{
147
149
if ( _onClosed != null )
148
- _onClosed ( this , null ) ;
150
+ _onClosed ( _zipArchiveEntry ) ;
149
151
150
152
if ( _closeBaseStream )
151
153
_baseStream . Dispose ( ) ;
152
154
153
- _canRead = false ;
154
- _canWrite = false ;
155
- _canSeek = false ;
156
155
_isDisposed = true ;
157
156
}
158
157
base . Dispose ( disposing ) ;
@@ -317,10 +316,11 @@ internal class CheckSumAndSizeWriteStream : Stream
317
316
318
317
//this is the position in BaseBaseStream
319
318
private Int64 _initialPosition ;
320
-
319
+ private readonly ZipArchiveEntry _zipArchiveEntry ;
320
+ private readonly EventHandler _onClose ;
321
321
// Called when the stream is closed.
322
- // parameters are initialPosition, currentPosition, checkSum
323
- private readonly Action < Int64 , Int64 , UInt32 > _saveCrcAndSizes ;
322
+ // parameters are initialPosition, currentPosition, checkSum, baseBaseStream, zipArchiveEntry and onClose handler
323
+ private readonly Action < Int64 , Int64 , UInt32 , Stream , ZipArchiveEntry , EventHandler > _saveCrcAndSizes ;
324
324
325
325
#endregion
326
326
@@ -329,10 +329,14 @@ internal class CheckSumAndSizeWriteStream : Stream
329
329
/* parameters to saveCrcAndSizes are
330
330
* initialPosition (initialPosition in baseBaseStream),
331
331
* currentPosition (in this CheckSumAndSizeWriteStream),
332
- * checkSum (of data passed into this CheckSumAndSizeWriteStream)
332
+ * checkSum (of data passed into this CheckSumAndSizeWriteStream),
333
+ * baseBaseStream it's a backingStream, passed here so as to avoid closure allocation,
334
+ * zipArchiveEntry passed here so as to avoid closure allocation,
335
+ * onClose handler passed here so as to avoid closure allocation
333
336
*/
334
- public CheckSumAndSizeWriteStream ( Stream baseStream , Stream baseBaseStream ,
335
- Boolean leaveOpenOnClose , Action < Int64 , Int64 , UInt32 > saveCrcAndSizes )
337
+ public CheckSumAndSizeWriteStream ( Stream baseStream , Stream baseBaseStream , Boolean leaveOpenOnClose ,
338
+ ZipArchiveEntry entry , EventHandler onClose ,
339
+ Action < Int64 , Int64 , UInt32 , Stream , ZipArchiveEntry , EventHandler > saveCrcAndSizes )
336
340
{
337
341
_baseStream = baseStream ;
338
342
_baseBaseStream = baseBaseStream ;
@@ -342,6 +346,8 @@ public CheckSumAndSizeWriteStream(Stream baseStream, Stream baseBaseStream,
342
346
_canWrite = true ;
343
347
_isDisposed = false ;
344
348
_initialPosition = 0 ;
349
+ _zipArchiveEntry = entry ;
350
+ _onClose = onClose ;
345
351
_saveCrcAndSizes = saveCrcAndSizes ;
346
352
}
347
353
@@ -458,7 +464,7 @@ protected override void Dispose(Boolean disposing)
458
464
if ( ! _leaveOpenOnClose )
459
465
_baseStream . Dispose ( ) ; // Close my super-stream (flushes the last data)
460
466
if ( _saveCrcAndSizes != null )
461
- _saveCrcAndSizes ( _initialPosition , Position , _checksum ) ;
467
+ _saveCrcAndSizes ( _initialPosition , Position , _checksum , _baseBaseStream , _zipArchiveEntry , _onClose ) ;
462
468
_isDisposed = true ;
463
469
}
464
470
base . Dispose ( disposing ) ;
0 commit comments