@@ -67,6 +67,44 @@ public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode m
6767 /// and advances the position within the stream by the number of bytes read.
6868 /// </summary>
6969 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 )
70108 {
71109 // Nothing to do
72110 if ( count == 0 )
@@ -78,30 +116,15 @@ public override int Read(byte[] buffer, int offset, int count)
78116 {
79117 nBytes = ReadBufferedData ( buffer , offset , count ) ;
80118
81- // Read all requested data from the buffer
82- if ( nBytes == count )
83- return nBytes ;
84-
85119 offset += nBytes ;
86120 count -= nBytes ;
87121 }
88122
89- // Read more data from the input, if available
90- if ( _slideBuffer != null )
91- nBytes += ReadAndTransform ( buffer , offset , count ) ;
92-
93123 return nBytes ;
94124 }
95125
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-
103126 // 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 )
105128 {
106129 int nBytes = 0 ;
107130 while ( nBytes < count )
@@ -126,7 +149,11 @@ private int ReadAndTransform(byte[] buffer, int offset, int count)
126149 _slideBufFreePos -= _slideBufStartPos ; // Note the -=
127150 _slideBufStartPos = 0 ;
128151 }
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+
130157 _slideBufFreePos += obtained ;
131158
132159 // Recalculate how much data we now have
0 commit comments