Skip to content

Commit a2ba998

Browse files
Copilotrzikmstephentoub
authored
Remove ZipHelper.ReadBytes wrapper methods and use Stream.ReadAtLeast directly (#118629)
* Initial plan * Simplify ZipHelper.ReadBytes by using Stream.ReadAtLeast default behavior Co-authored-by: rzikm <[email protected]> * Remove ZipHelper.ReadBytes wrapper methods and use Stream.ReadAtLeast directly Co-authored-by: stephentoub <[email protected]> --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: rzikm <[email protected]> Co-authored-by: stephentoub <[email protected]>
1 parent 438a8e7 commit a2ba998

File tree

4 files changed

+8
-37
lines changed

4 files changed

+8
-37
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 & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +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-
int bytesRead = await stream.ReadAtLeastAsync(buffer, bytesToRead, throwOnEndOfStream: false, cancellationToken).ConfigureAwait(false);
21-
22-
if (bytesRead < bytesToRead)
23-
{
24-
throw new IOException(SR.UnexpectedEndOfStream);
25-
}
26-
return bytesRead;
27-
}
28-
2913
// Asynchronously assumes all bytes of signatureToFind are non zero, looks backwards from current position in stream,
3014
// assumes maxBytesToRead is positive, ensures to not read beyond the provided max number of bytes,
3115
// if the signature is found then returns true and positions stream at first byte of signature
@@ -114,14 +98,14 @@ private static async Task<int> SeekBackwardsAndReadAsync(Stream stream, Memory<b
11498
{
11599
Debug.Assert(overlap <= buffer.Length);
116100
stream.Seek(-(buffer.Length - overlap), SeekOrigin.Current);
117-
bytesRead = await ReadBytesAsync(stream, buffer, buffer.Length, cancellationToken).ConfigureAwait(false);
101+
bytesRead = await stream.ReadAtLeastAsync(buffer, buffer.Length, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
118102
stream.Seek(-buffer.Length, SeekOrigin.Current);
119103
}
120104
else
121105
{
122106
int bytesToRead = (int)stream.Position;
123107
stream.Seek(0, SeekOrigin.Begin);
124-
bytesRead = await ReadBytesAsync(stream, buffer, bytesToRead, cancellationToken).ConfigureAwait(false);
108+
bytesRead = await stream.ReadAtLeastAsync(buffer, bytesToRead, throwOnEndOfStream: true, cancellationToken).ConfigureAwait(false);
125109
stream.Seek(0, SeekOrigin.Begin);
126110
}
127111

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +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-
int bytesRead = stream.ReadAtLeast(buffer, bytesToRead, throwOnEndOfStream: false);
41-
if (bytesRead < bytesToRead)
42-
{
43-
throw new IOException(SR.UnexpectedEndOfStream);
44-
}
45-
return bytesRead;
46-
}
47-
4835
// will silently return InvalidDateIndicator if the uint is not a valid Dos DateTime
4936
internal static DateTime DosTimeToDateTime(uint dateTime)
5037
{
@@ -183,14 +170,14 @@ private static int SeekBackwardsAndRead(Stream stream, Span<byte> buffer, int ov
183170
{
184171
Debug.Assert(overlap <= buffer.Length);
185172
stream.Seek(-(buffer.Length - overlap), SeekOrigin.Current);
186-
bytesRead = ReadBytes(stream, buffer, buffer.Length);
173+
bytesRead = stream.ReadAtLeast(buffer, buffer.Length, throwOnEndOfStream: true);
187174
stream.Seek(-buffer.Length, SeekOrigin.Current);
188175
}
189176
else
190177
{
191178
int bytesToRead = (int)stream.Position;
192179
stream.Seek(0, SeekOrigin.Begin);
193-
bytesRead = ReadBytes(stream, buffer, bytesToRead);
180+
bytesRead = stream.ReadAtLeast(buffer, bytesToRead, throwOnEndOfStream: true);
194181
stream.Seek(0, SeekOrigin.Begin);
195182
}
196183

0 commit comments

Comments
 (0)