Skip to content

Commit 74f36a4

Browse files
committed
Add an async version of ReadRequestedBytes
1 parent e1e1a91 commit 74f36a4

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

src/ICSharpCode.SharpZipLib/Core/StreamUtils.cs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,8 @@ public static void ReadFully(Stream stream, byte[] buffer, int offset, int count
6666
}
6767
}
6868

69-
/// <summary>
70-
/// Read as much data as possible from a <see cref="Stream"/>", up to the requested number of bytes
71-
/// </summary>
72-
/// <param name="stream">The stream to read data from.</param>
73-
/// <param name="buffer">The buffer to store data in.</param>
74-
/// <param name="offset">The offset at which to begin storing data.</param>
75-
/// <param name="count">The number of bytes of data to store.</param>
76-
/// <exception cref="ArgumentNullException">Required parameter is null</exception>
77-
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> and or <paramref name="count"/> are invalid.</exception>
78-
public static 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)
7971
{
8072
if (stream == null)
8173
{
@@ -97,7 +89,23 @@ public static int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
9789
{
9890
throw new ArgumentOutOfRangeException(nameof(count));
9991
}
92+
}
10093

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+
public static 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
101109
int totalReadCount = 0;
102110
while (count > 0)
103111
{
@@ -114,6 +122,38 @@ public static int ReadRequestedBytes(Stream stream, byte[] buffer, int offset, i
114122
return totalReadCount;
115123
}
116124

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+
public static 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+
117157
/// <summary>
118158
/// Copy the contents of one <see cref="Stream"/> to another.
119159
/// </summary>

0 commit comments

Comments
 (0)