Skip to content

Commit 0199b1c

Browse files
Copilotstephentoub
andcommitted
Remove ZipHelper.ReadBytes wrapper methods and use Stream.ReadAtLeast directly
Co-authored-by: stephentoub <[email protected]>
1 parent 8d02926 commit 0199b1c

File tree

4 files changed

+8
-26
lines changed

4 files changed

+8
-26
lines changed

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.Async.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ internal async Task LoadCompressedBytesIfNeededAsync(CancellationToken cancellat
172172

173173
for (int i = 0; i < _compressedBytes.Length - 1; i++)
174174
{
175-
await ZipHelper.ReadBytesAsync(_archive.ArchiveStream, _compressedBytes[i], maxSingleBufferSize, cancellationToken).ConfigureAwait(false);
175+
await _archive.ArchiveStream.ReadAtLeastAsync(_compressedBytes[i], maxSingleBufferSize, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
176176
}
177-
await ZipHelper.ReadBytesAsync(_archive.ArchiveStream, _compressedBytes[_compressedBytes.Length - 1], (int)(_compressedSize % maxSingleBufferSize), cancellationToken).ConfigureAwait(false);
177+
await _archive.ArchiveStream.ReadAtLeastAsync(_compressedBytes[_compressedBytes.Length - 1], (int)(_compressedSize % maxSingleBufferSize), throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
178178
}
179179
}
180180

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,9 +678,9 @@ internal void LoadCompressedBytesIfNeeded()
678678

679679
for (int i = 0; i < _compressedBytes.Length - 1; i++)
680680
{
681-
ZipHelper.ReadBytes(_archive.ArchiveStream, _compressedBytes[i], maxSingleBufferSize);
681+
_archive.ArchiveStream.ReadAtLeast(_compressedBytes[i], maxSingleBufferSize, throwOnEndOfStream: true);
682682
}
683-
ZipHelper.ReadBytes(_archive.ArchiveStream, _compressedBytes[_compressedBytes.Length - 1], (int)(_compressedSize % maxSingleBufferSize));
683+
_archive.ArchiveStream.ReadAtLeast(_compressedBytes[_compressedBytes.Length - 1], (int)(_compressedSize % maxSingleBufferSize), throwOnEndOfStream: true);
684684
}
685685
}
686686

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.Async.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ namespace System.IO.Compression;
1010

1111
internal static partial class ZipHelper
1212
{
13-
/// <summary>
14-
/// Asynchronously reads exactly bytesToRead out of stream, unless it is out of bytes.
15-
/// </summary>
16-
internal static async Task<int> ReadBytesAsync(Stream stream, Memory<byte> buffer, int bytesToRead, CancellationToken cancellationToken)
17-
{
18-
cancellationToken.ThrowIfCancellationRequested();
19-
20-
return await stream.ReadAtLeastAsync(buffer, bytesToRead, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
21-
}
22-
2313
// Asynchronously assumes all bytes of signatureToFind are non zero, looks backwards from current position in stream,
2414
// assumes maxBytesToRead is positive, ensures to not read beyond the provided max number of bytes,
2515
// if the signature is found then returns true and positions stream at first byte of signature
@@ -108,14 +98,14 @@ private static async Task<int> SeekBackwardsAndReadAsync(Stream stream, Memory<b
10898
{
10999
Debug.Assert(overlap <= buffer.Length);
110100
stream.Seek(-(buffer.Length - overlap), SeekOrigin.Current);
111-
bytesRead = await ReadBytesAsync(stream, buffer, buffer.Length, cancellationToken).ConfigureAwait(false);
101+
bytesRead = await stream.ReadAtLeastAsync(buffer, buffer.Length, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
112102
stream.Seek(-buffer.Length, SeekOrigin.Current);
113103
}
114104
else
115105
{
116106
int bytesToRead = (int)stream.Position;
117107
stream.Seek(0, SeekOrigin.Begin);
118-
bytesRead = await ReadBytesAsync(stream, buffer, bytesToRead, cancellationToken).ConfigureAwait(false);
108+
bytesRead = await stream.ReadAtLeastAsync(buffer, bytesToRead, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
119109
stream.Seek(0, SeekOrigin.Begin);
120110
}
121111

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ internal static Encoding GetEncoding(string text)
3232
return Encoding.ASCII;
3333
}
3434

35-
/// <summary>
36-
/// Reads exactly bytesToRead out of stream, unless it is out of bytes
37-
/// </summary>
38-
internal static int ReadBytes(Stream stream, Span<byte> buffer, int bytesToRead)
39-
{
40-
return stream.ReadAtLeast(buffer, bytesToRead, throwOnEndOfStream: true);
41-
}
42-
4335
// will silently return InvalidDateIndicator if the uint is not a valid Dos DateTime
4436
internal static DateTime DosTimeToDateTime(uint dateTime)
4537
{
@@ -178,14 +170,14 @@ private static int SeekBackwardsAndRead(Stream stream, Span<byte> buffer, int ov
178170
{
179171
Debug.Assert(overlap <= buffer.Length);
180172
stream.Seek(-(buffer.Length - overlap), SeekOrigin.Current);
181-
bytesRead = ReadBytes(stream, buffer, buffer.Length);
173+
bytesRead = stream.ReadAtLeast(buffer, buffer.Length, throwOnEndOfStream: true);
182174
stream.Seek(-buffer.Length, SeekOrigin.Current);
183175
}
184176
else
185177
{
186178
int bytesToRead = (int)stream.Position;
187179
stream.Seek(0, SeekOrigin.Begin);
188-
bytesRead = ReadBytes(stream, buffer, bytesToRead);
180+
bytesRead = stream.ReadAtLeast(buffer, bytesToRead, throwOnEndOfStream: true);
189181
stream.Seek(0, SeekOrigin.Begin);
190182
}
191183

0 commit comments

Comments
 (0)