diff --git a/OpenMcdf.Tests/StreamExtensions.cs b/OpenMcdf.Tests/StreamExtensions.cs index a370949..341ec1a 100644 --- a/OpenMcdf.Tests/StreamExtensions.cs +++ b/OpenMcdf.Tests/StreamExtensions.cs @@ -1,7 +1,27 @@ -namespace OpenMcdf.Tests; +#if !NET7_0_OR_GREATER +using System.Buffers; +#endif + +namespace OpenMcdf.Tests; internal static class StreamExtensions { +#if !NET7_0_OR_GREATER + public static void ReadExactly(this Stream stream, Span buffer) + { + byte[] array = ArrayPool.Shared.Rent(buffer.Length); + try + { + stream.ReadExactly(array, 0, buffer.Length); + array.AsSpan(0, buffer.Length).CopyTo(buffer); + } + finally + { + ArrayPool.Shared.Return(array); + } + } +#endif + public static void Write(this Stream stream, byte[] buffer, WriteMode mode) { #if (!NETSTANDARD2_0 && !NETFRAMEWORK) diff --git a/OpenMcdf/CfbBinaryReader.cs b/OpenMcdf/CfbBinaryReader.cs index 354a07b..0f0b3d0 100644 --- a/OpenMcdf/CfbBinaryReader.cs +++ b/OpenMcdf/CfbBinaryReader.cs @@ -1,10 +1,6 @@ using System.Diagnostics; using System.Text; -#if NETSTANDARD2_0 -using System.Buffers; -#endif - namespace OpenMcdf; /// @@ -26,37 +22,11 @@ public long Position set => BaseStream.Position = value; } -#if NETSTANDARD2_0 - - public int Read(Span buffer) - { - byte[] array = ArrayPool.Shared.Rent(buffer.Length); - try - { - int bytesRead = BaseStream.Read(array, 0, buffer.Length); - array.AsSpan(0, bytesRead).CopyTo(buffer); - return bytesRead; - } - finally - { - ArrayPool.Shared.Return(array); - } - } - -#endif - void ReadExactly(byte[] buffer, int offset, int count) => BaseStream.ReadExactly(buffer, offset, count); public Guid ReadGuid() { - int bytesRead = 0; - do - { - int n = Read(guidBuffer, bytesRead, guidBuffer.Length - bytesRead); - if (n == 0) - throw new EndOfStreamException(); - bytesRead += n; - } while (bytesRead < guidBuffer.Length); + BaseStream.ReadExactly(guidBuffer, 0, guidBuffer.Length); return new Guid(guidBuffer); } diff --git a/OpenMcdf/Fat.cs b/OpenMcdf/Fat.cs index 6c8ad14..53efad4 100644 --- a/OpenMcdf/Fat.cs +++ b/OpenMcdf/Fat.cs @@ -57,7 +57,7 @@ void CacheCurrentSector() CfbBinaryReader reader = Context.Reader; reader.Position = current.Position; - reader.Read(cachedSectorBuffer); + reader.Read(cachedSectorBuffer, 0, cachedSectorBuffer.Length); cachedSector = current; } diff --git a/OpenMcdf/MiniFat.cs b/OpenMcdf/MiniFat.cs index 542f26e..c562de7 100644 --- a/OpenMcdf/MiniFat.cs +++ b/OpenMcdf/MiniFat.cs @@ -12,7 +12,7 @@ internal sealed class MiniFat : ContextBase, IEnumerable, IDisposable { private readonly FatChainEnumerator fatChainEnumerator; private readonly int ElementsPerSector; - private readonly byte[] sector; + private readonly byte[] cachedSectorBuffer; private bool isDirty; public MiniFat(RootContextSite rootContextSite) @@ -20,7 +20,7 @@ public MiniFat(RootContextSite rootContextSite) { ElementsPerSector = Context.SectorSize / sizeof(uint); fatChainEnumerator = new(Context.Fat, Context.Header.FirstMiniFatSectorId); - sector = new byte[Context.SectorSize]; + cachedSectorBuffer = new byte[Context.SectorSize]; } public void Dispose() @@ -36,7 +36,7 @@ public void Flush() { CfbBinaryWriter writer = Context.Writer; writer.Position = fatChainEnumerator.CurrentSector.Position; - writer.Write(sector); + writer.Write(cachedSectorBuffer); isDirty = false; } } @@ -74,7 +74,7 @@ bool TryMoveToSectorForKey(uint key, out long elementIndex) CfbBinaryReader reader = Context.Reader; reader.Position = fatChainEnumerator.CurrentSector.Position; - reader.Read(sector); + reader.Read(cachedSectorBuffer, 0, cachedSectorBuffer.Length); return true; } @@ -88,7 +88,7 @@ public bool TryGetValue(uint key, out uint value) return false; } - Span slice = sector.AsSpan((int)elementIndex * sizeof(uint)); + Span slice = cachedSectorBuffer.AsSpan((int)elementIndex * sizeof(uint)); value = BinaryPrimitives.ReadUInt32LittleEndian(slice); return true; } @@ -100,7 +100,7 @@ public bool TrySetValue(uint key, uint value) if (!TryMoveToSectorForKey(key, out long elementIndex)) return false; - Span slice = sector.AsSpan((int)elementIndex * sizeof(uint)); + Span slice = cachedSectorBuffer.AsSpan((int)elementIndex * sizeof(uint)); BinaryPrimitives.WriteUInt32LittleEndian(slice, value); isDirty = true; return true; diff --git a/OpenMcdf/StreamExtensions.cs b/OpenMcdf/StreamExtensions.cs index e65b7e4..6bce7e3 100644 --- a/OpenMcdf/StreamExtensions.cs +++ b/OpenMcdf/StreamExtensions.cs @@ -11,20 +11,6 @@ internal static class StreamExtensions { #if !NET7_0_OR_GREATER - public static void ReadExactly(this Stream stream, Span buffer) - { - byte[] array = ArrayPool.Shared.Rent(buffer.Length); - try - { - stream.ReadExactly(array, 0, buffer.Length); - array.AsSpan(0, buffer.Length).CopyTo(buffer); - } - finally - { - ArrayPool.Shared.Return(array); - } - } - public static void ReadExactly(this Stream stream, byte[] buffer, int offset, int count) { if (count == 0) diff --git a/OpenMcdf/TransactedStream.cs b/OpenMcdf/TransactedStream.cs index 29e948d..c32b509 100644 --- a/OpenMcdf/TransactedStream.cs +++ b/OpenMcdf/TransactedStream.cs @@ -104,7 +104,7 @@ public override void Write(byte[] buffer, int offset, int count) { // Copy the existing sector data originalStream.Position = originalPosition - sectorOffset; - originalStream.ReadExactly(this.buffer); + originalStream.ReadExactly(this.buffer, 0, this.buffer.Length); OverlayStream.Position = overlayPosition; OverlayStream.Write(this.buffer, 0, this.buffer.Length); @@ -122,7 +122,7 @@ public void Commit() foreach (KeyValuePair entry in dirtySectorPositions) { OverlayStream.Position = entry.Value; - OverlayStream.ReadExactly(buffer); + OverlayStream.ReadExactly(buffer, 0, buffer.Length); originalStream.Position = entry.Key * Context.SectorSize; originalStream.Write(buffer, 0, buffer.Length);