Skip to content

Commit

Permalink
Fix ReadAsByteArray
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvk committed Feb 12, 2021
1 parent 5c65cb0 commit 99b9665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 14 additions & 10 deletions CodeJam.Main/IO/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP // PUBLIC_API_CHANGES

#if NET45_OR_GREATER || TARGETS_NETSTANDARD || TARGETS_NETCOREAPP // PUBLIC_API_CHANGES
using System;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -83,9 +84,7 @@ public static string ReadAsString([NotNull] this Stream stream, [CanBeNull] Enco
{
// DO NOT dispose the reader
using (var reader = stream.ToStreamReader(encoding, true))
{
return reader.ReadToEnd();
}
}

/// <summary>
Expand All @@ -100,9 +99,7 @@ public static async Task<string> ReadAsStringAsync(
{
// DO NOT dispose the reader
using (var reader = stream.ToStreamReader(encoding, true))
{
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
}

/// <summary>
Expand All @@ -112,13 +109,20 @@ public static async Task<string> ReadAsStringAsync(
[NotNull]
public static byte[] ReadAsByteArray([NotNull] this Stream stream)
{
// DO NOT dispose the reader
using (var reader = stream.ToBinaryReader(null, true))
if (stream.CanSeek)
// DO NOT dispose underlying stream
using (var reader = stream.ToBinaryReader(null, true))
{
var readCount = checked((int)(stream.Length - stream.Position));
return reader.ReadBytes(readCount);
}
using (var tempStream = new MemoryStream())
{
var readCount = checked((int)(stream.Length - stream.Position));
return reader.ReadBytes(readCount);
stream.CopyTo(tempStream);
return tempStream.ToArray();
}
}
}
}
#endif

#endif
6 changes: 5 additions & 1 deletion CodeJam.Main/Readme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CodeJam 3.3.0 Release Notes
CodeJam 3.3.1 Release Notes
---------------------------

What's new in 3.3.1
-------------------
* Fix StreamExtensions.ReadAsByteArray (non-seekable stream support)

What's new in 3.3.0
-------------------
* Add more string Invariant and Ordinal methods.
Expand Down

0 comments on commit 99b9665

Please sign in to comment.