1
1
using System ;
2
2
using System . IO ;
3
+ using System . Threading ;
4
+ using System . Threading . Tasks ;
3
5
4
6
namespace ICSharpCode . SharpZipLib . Core
5
7
{
@@ -64,16 +66,8 @@ static public void ReadFully(Stream stream, byte[] buffer, int offset, int count
64
66
}
65
67
}
66
68
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 )
77
71
{
78
72
if ( stream == null )
79
73
{
@@ -95,7 +89,23 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
95
89
{
96
90
throw new ArgumentOutOfRangeException ( nameof ( count ) ) ;
97
91
}
92
+ }
98
93
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
99
109
int totalReadCount = 0 ;
100
110
while ( count > 0 )
101
111
{
@@ -112,6 +122,38 @@ static public int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
112
122
return totalReadCount ;
113
123
}
114
124
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
+
115
157
/// <summary>
116
158
/// Copy the contents of one <see cref="Stream"/> to another.
117
159
/// </summary>
0 commit comments