Skip to content

Commit

Permalink
Handle the partial-byte-reads-in-stream breaking change in .NET6 for …
Browse files Browse the repository at this point in the history
  • Loading branch information
Panos Kousidis committed Jul 25, 2023
1 parent e6f1573 commit 339ac82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Directory.build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup Condition="$(MSBuildProjectName.StartsWith('ExcelDataReader'))">
<VersionPrefix>3.7.0</VersionPrefix>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<AssemblyOriginatorKeyFile>..\ExcelDataReader.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down
19 changes: 15 additions & 4 deletions src/ExcelDataReader/Core/OpenXmlFormat/BinaryFormat/BiffReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Text;

using ExcelDataReader.Core.OpenXmlFormat.Records;

#nullable enable
Expand All @@ -27,9 +26,21 @@ public BiffReader(Stream stream)
return null;

byte[] buffer = recordLength < _buffer.Length ? _buffer : new byte[recordLength];
if (Stream.Read(buffer, 0, (int)recordLength) != recordLength)
int totalRead = 0;
#if NET6_0_OR_GREATER
// https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/partial-byte-reads-in-streams
while (totalRead < buffer.Length)
{
int bytesRead = Stream.Read(buffer, 0, (int)recordLength - totalRead);
if (bytesRead == 0)
break;
totalRead += bytesRead;
}
#else
totalRead = Stream.Read(buffer, 0, (int)recordLength);
#endif
if (totalRead != recordLength)
return null;

return ReadOverride(buffer, recordId, recordLength);
}

Expand Down Expand Up @@ -154,6 +165,6 @@ private bool TryReadVariableValue(out uint value)
value = ((uint)(b4 & 0x7F) << 21) | value;

return true;
}
}
}
}

0 comments on commit 339ac82

Please sign in to comment.