11using System ;
22using System . IO ;
3+ using System . Threading ;
4+ using System . Threading . Tasks ;
35
46namespace ICSharpCode . SharpZipLib . Core
57{
@@ -64,16 +66,8 @@ static public void ReadFully(Stream stream, byte[] buffer, int offset, int count
6466 }
6567 }
6668
67- /// <summary>
68- /// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
69- /// </summary>
70- /// <param name="stream">The stream to read data from.</param>
71- /// <param name="buffer">The buffer to store data in.</param>
72- /// <param name="offset">The offset at which to begin storing data.</param>
73- /// <param name="count">The number of bytes of data to store.</param>
74- /// <exception cref="ArgumentNullException">Required parameter is null</exception>
75- /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
76- static public int ReadRequestedBytes ( Stream stream , byte [ ] buffer , int offset , int count )
69+ // A helper function to share between the async and sync versions of ReadRequestedBytes
70+ private static void ValidateArgumentsForRead ( Stream stream , byte [ ] buffer , int offset , int count )
7771 {
7872 if ( stream == null )
7973 {
@@ -95,7 +89,23 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
9589 {
9690 throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
9791 }
92+ }
9893
94+ /// <summary>
95+ /// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
96+ /// </summary>
97+ /// <param name="stream">The stream to read data from.</param>
98+ /// <param name="buffer">The buffer to store data in.</param>
99+ /// <param name="offset">The offset at which to begin storing data.</param>
100+ /// <param name="count">The number of bytes of data to store.</param>
101+ /// <exception cref="ArgumentNullException">Required parameter is null</exception>
102+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
103+ static public int ReadRequestedBytes ( Stream stream , byte [ ] buffer , int offset , int count )
104+ {
105+ // Common validation function
106+ ValidateArgumentsForRead ( stream , buffer , offset , count ) ;
107+
108+ // read the data using Read
99109 int totalReadCount = 0 ;
100110 while ( count > 0 )
101111 {
@@ -112,6 +122,38 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
112122 return totalReadCount ;
113123 }
114124
125+ /// <summary>
126+ /// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
127+ /// </summary>
128+ /// <param name="stream">The stream to read data from.</param>
129+ /// <param name="buffer">The buffer to store data in.</param>
130+ /// <param name="offset">The offset at which to begin storing data.</param>
131+ /// <param name="count">The number of bytes of data to store.</param>
132+ /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
133+ /// <exception cref="ArgumentNullException">Required parameter is null</exception>
134+ /// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
135+ static public async Task < int > ReadRequestedBytesAsync ( Stream stream , byte [ ] buffer , int offset , int count , CancellationToken cancellationToken )
136+ {
137+ // Common validation function
138+ ValidateArgumentsForRead ( stream , buffer , offset , count ) ;
139+
140+ // read the data using ReadAsync
141+ int totalReadCount = 0 ;
142+ while ( count > 0 )
143+ {
144+ int readCount = await stream . ReadAsync ( buffer , offset , count , cancellationToken ) . ConfigureAwait ( false ) ;
145+ if ( readCount <= 0 )
146+ {
147+ break ;
148+ }
149+ offset += readCount ;
150+ count -= readCount ;
151+ totalReadCount += readCount ;
152+ }
153+
154+ return totalReadCount ;
155+ }
156+
115157 /// <summary>
116158 /// Copy the contents of one <see cref="Stream"/> to another.
117159 /// </summary>
0 commit comments