@@ -67,6 +67,44 @@ public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode m
67
67
/// and advances the position within the stream by the number of bytes read.
68
68
/// </summary>
69
69
public override int Read ( byte [ ] buffer , int offset , int count )
70
+ {
71
+ // If we have buffered data, read that first
72
+ int nBytes = ReadDataFromBuffer ( buffer , ref offset , ref count ) ;
73
+
74
+ // If we've read the requested amount of data, return just that
75
+ if ( count == 0 )
76
+ return nBytes ;
77
+
78
+ // Read more data from the input, if available
79
+ if ( _slideBuffer != null )
80
+ {
81
+ nBytes += ReadAndTransformAsync ( buffer , offset , count , false , default ) . GetAwaiter ( ) . GetResult ( ) ;
82
+ }
83
+
84
+ return nBytes ;
85
+ }
86
+
87
+ /// <inheritdoc/>
88
+ public override async Task < int > ReadAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
89
+ {
90
+ // If we have buffered data, read that first
91
+ int nBytes = ReadDataFromBuffer ( buffer , ref offset , ref count ) ;
92
+
93
+ // If we've read the requested amount of data, return just that
94
+ if ( count == 0 )
95
+ return nBytes ;
96
+
97
+ // Read more data from the input, if available
98
+ if ( _slideBuffer != null )
99
+ {
100
+ nBytes += await ReadAndTransformAsync ( buffer , offset , count , true , cancellationToken ) . ConfigureAwait ( false ) ;
101
+ }
102
+
103
+ return nBytes ;
104
+ }
105
+
106
+ // Read up to the requested amount of data from the buffer
107
+ private int ReadDataFromBuffer ( byte [ ] buffer , ref int offset , ref int count )
70
108
{
71
109
// Nothing to do
72
110
if ( count == 0 )
@@ -78,30 +116,15 @@ public override int Read(byte[] buffer, int offset, int count)
78
116
{
79
117
nBytes = ReadBufferedData ( buffer , offset , count ) ;
80
118
81
- // Read all requested data from the buffer
82
- if ( nBytes == count )
83
- return nBytes ;
84
-
85
119
offset += nBytes ;
86
120
count -= nBytes ;
87
121
}
88
122
89
- // Read more data from the input, if available
90
- if ( _slideBuffer != null )
91
- nBytes += ReadAndTransform ( buffer , offset , count ) ;
92
-
93
123
return nBytes ;
94
124
}
95
125
96
- /// <inheritdoc/>
97
- public override Task < int > ReadAsync ( byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
98
- {
99
- var readCount = Read ( buffer , offset , count ) ;
100
- return Task . FromResult ( readCount ) ;
101
- }
102
-
103
126
// Read data from the underlying stream and decrypt it
104
- private int ReadAndTransform ( byte [ ] buffer , int offset , int count )
127
+ private async Task < int > ReadAndTransformAsync ( byte [ ] buffer , int offset , int count , bool useAsync , CancellationToken cancellationToken )
105
128
{
106
129
int nBytes = 0 ;
107
130
while ( nBytes < count )
@@ -126,7 +149,11 @@ private int ReadAndTransform(byte[] buffer, int offset, int count)
126
149
_slideBufFreePos -= _slideBufStartPos ; // Note the -=
127
150
_slideBufStartPos = 0 ;
128
151
}
129
- int obtained = StreamUtils . ReadRequestedBytes ( _stream , _slideBuffer , _slideBufFreePos , lengthToRead ) ;
152
+
153
+ int obtained = useAsync ?
154
+ await StreamUtils . ReadRequestedBytesAsync ( _stream , _slideBuffer , _slideBufFreePos , lengthToRead , cancellationToken ) . ConfigureAwait ( false ) :
155
+ StreamUtils . ReadRequestedBytes ( _stream , _slideBuffer , _slideBufFreePos , lengthToRead ) ;
156
+
130
157
_slideBufFreePos += obtained ;
131
158
132
159
// Recalculate how much data we now have
0 commit comments