Skip to content

Spanify SequentialReader #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions MetadataExtractor.Tests/IO/SequentialReaderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ public void GetBytes()
}

[Fact]
public void OverflowBoundsCalculation()
public void GetBytes_OverflowBoundsCalculation()
{
var reader = CreateReader(new byte[10]);
var ex = Assert.Throws<IOException>(() => reader.GetBytes(15));
Assert.Equal("End of data reached.", ex.Message);
}

[Fact]
public void GetBytesEof()
public void GetBytes_Eof()
{
CreateReader(new byte[50]).GetBytes(50);

Expand All @@ -255,8 +255,45 @@ public void GetBytesEof()
Assert.Throws<IOException>(() => CreateReader(new byte[50]).GetBytes(51));
}

#if NET
[Fact]
public void GetByteEof()
public void GetBytes_Span()
{
var bytes = new byte[] { 0, 1, 2, 3, 4, 5 };
for (var i = 0; i < bytes.Length; i++)
{
var reader = CreateReader(bytes);
#pragma warning disable CA2014 // Do not use stackalloc in loops
Span<byte> span = stackalloc byte[i];
#pragma warning restore CA2014 // Do not use stackalloc in loops
reader.GetBytes(span);
Assert.Equal(bytes.Take(i).ToArray(), span.ToArray());
}
}

[Fact]
public void GetBytes_OverflowBoundsCalculation_Span()
{
var reader = CreateReader(new byte[10]);
var ex = Assert.Throws<IOException>(() => reader.GetBytes(stackalloc byte[15]));
Assert.Equal("End of data reached.", ex.Message);
}

[Fact]
public void GetBytes_Eof_Span()
{
CreateReader(new byte[50]).GetBytes(stackalloc byte[50]);

var reader = CreateReader(new byte[50]);
reader.GetBytes(stackalloc byte[25]);
reader.GetBytes(stackalloc byte[25]);

Assert.Throws<IOException>(() => CreateReader(new byte[50]).GetBytes(stackalloc byte[51]));
}
#endif

[Fact]
public void GetByte_Eof()
{
CreateReader(new byte[1]).GetByte();

Expand Down
9 changes: 9 additions & 0 deletions MetadataExtractor/IO/SequentialByteArrayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public override byte[] GetBytes(int count)
return bytes;
}

public override void GetBytes(Span<byte> bytes)
{
if (_index + bytes.Length > _bytes.Length)
throw new IOException("End of data reached.");

_bytes.AsSpan(_index, bytes.Length).CopyTo(bytes);
_index += bytes.Length;
}

public override void GetBytes(byte[] buffer, int offset, int count)
{
if (_index + count > _bytes.Length)
Expand Down
157 changes: 52 additions & 105 deletions MetadataExtractor/IO/SequentialReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.Buffers.Binary;

namespace MetadataExtractor.IO
{
/// <summary>Base class for reading sequentially through a sequence of data encoded in a byte stream.</summary>
Expand All @@ -13,13 +15,8 @@ namespace MetadataExtractor.IO
/// <see cref="IsMotorolaByteOrder"/>.
/// </remarks>
/// <author>Drew Noakes https://drewnoakes.com</author>
public abstract class SequentialReader
public abstract class SequentialReader(bool isMotorolaByteOrder)
{
protected SequentialReader(bool isMotorolaByteOrder)
{
IsMotorolaByteOrder = isMotorolaByteOrder;
}

/// <summary>Get and set the byte order of this reader. <c>true</c> by default.</summary>
/// <remarks>
/// <list type="bullet">
Expand All @@ -28,7 +25,7 @@ protected SequentialReader(bool isMotorolaByteOrder)
/// </list>
/// </remarks>
/// <value><c>true</c> for Motorola/big endian, <c>false</c> for Intel/little endian</value>
public bool IsMotorolaByteOrder { get; }
public bool IsMotorolaByteOrder { get; } = isMotorolaByteOrder;

public abstract long Position { get; }

Expand All @@ -40,6 +37,8 @@ protected SequentialReader(bool isMotorolaByteOrder)
/// <exception cref="IOException"/>
public abstract byte[] GetBytes(int count);

public abstract void GetBytes(Span<byte> bytes);

/// <summary>Retrieves bytes, writing them into a caller-provided buffer.</summary>
/// <param name="buffer">The array to write bytes to.</param>
/// <param name="offset">The starting position within <paramref name="buffer"/> to write to.</param>
Expand Down Expand Up @@ -100,139 +99,81 @@ protected SequentialReader(bool isMotorolaByteOrder)
/// <exception cref="IOException"/>
public ushort GetUInt16()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first
return (ushort)
(GetByte() << 8 |
GetByte());
}
// Intel ordering - LSB first
return (ushort)
(GetByte() |
GetByte() << 8);
Span<byte> bytes = stackalloc byte[2];

GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadUInt16BigEndian(bytes)
: BinaryPrimitives.ReadUInt16LittleEndian(bytes);
}

/// <summary>Returns a signed 16-bit int calculated from two bytes of data (MSB, LSB).</summary>
/// <returns>the 16 bit int value, between 0x0000 and 0xFFFF</returns>
/// <exception cref="IOException">the buffer does not contain enough bytes to service the request</exception>
public short GetInt16()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first
return (short)
(GetByte() << 8 |
GetByte());
}
// Intel ordering - LSB first
return (short)
(GetByte() |
GetByte() << 8);
Span<byte> bytes = stackalloc byte[2];

GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadInt16BigEndian(bytes)
: BinaryPrimitives.ReadInt16LittleEndian(bytes);
}

/// <summary>Get a 32-bit unsigned integer from the buffer, returning it as a long.</summary>
/// <returns>the unsigned 32-bit int value as a long, between 0x00000000 and 0xFFFFFFFF</returns>
/// <exception cref="IOException">the buffer does not contain enough bytes to service the request</exception>
public uint GetUInt32()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first (big endian)
return (uint)
(GetByte() << 24 |
GetByte() << 16 |
GetByte() << 8 |
GetByte());
}
// Intel ordering - LSB first (little endian)
return (uint)
(GetByte() |
GetByte() << 8 |
GetByte() << 16 |
GetByte() << 24);
Span<byte> bytes = stackalloc byte[4];

GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadUInt32BigEndian(bytes)
: BinaryPrimitives.ReadUInt32LittleEndian(bytes);
}

/// <summary>Returns a signed 32-bit integer from four bytes of data.</summary>
/// <returns>the signed 32 bit int value, between 0x00000000 and 0xFFFFFFFF</returns>
/// <exception cref="IOException">the buffer does not contain enough bytes to service the request</exception>
public int GetInt32()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first (big endian)
return
GetByte() << 24 |
GetByte() << 16 |
GetByte() << 8 |
GetByte();
}
// Intel ordering - LSB first (little endian)
return
GetByte() |
GetByte() << 8 |
GetByte() << 16 |
GetByte() << 24;
Span<byte> bytes = stackalloc byte[4];

GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadInt32BigEndian(bytes)
: BinaryPrimitives.ReadInt32LittleEndian(bytes);
}

/// <summary>Get a signed 64-bit integer from the buffer.</summary>
/// <returns>the 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF</returns>
/// <exception cref="IOException">the buffer does not contain enough bytes to service the request</exception>
public long GetInt64()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first
return
(long)GetByte() << 56 |
(long)GetByte() << 48 |
(long)GetByte() << 40 |
(long)GetByte() << 32 |
(long)GetByte() << 24 |
(long)GetByte() << 16 |
(long)GetByte() << 8 |
GetByte();
}
// Intel ordering - LSB first
return
GetByte() |
(long)GetByte() << 8 |
(long)GetByte() << 16 |
(long)GetByte() << 24 |
(long)GetByte() << 32 |
(long)GetByte() << 40 |
(long)GetByte() << 48 |
(long)GetByte() << 56;
Span<byte> bytes = stackalloc byte[8];
GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadInt64BigEndian(bytes)
: BinaryPrimitives.ReadInt64LittleEndian(bytes);
}

/// <summary>Get an unsigned 64-bit integer from the buffer.</summary>
/// <returns>the unsigned 64 bit int value, between 0x0000000000000000 and 0xFFFFFFFFFFFFFFFF</returns>
/// <exception cref="IOException">the buffer does not contain enough bytes to service the request</exception>
public ulong GetUInt64()
{
if (IsMotorolaByteOrder)
{
// Motorola - MSB first
return
(ulong)GetByte() << 56 |
(ulong)GetByte() << 48 |
(ulong)GetByte() << 40 |
(ulong)GetByte() << 32 |
(ulong)GetByte() << 24 |
(ulong)GetByte() << 16 |
(ulong)GetByte() << 8 |
GetByte();
}
// Intel ordering - LSB first
return
GetByte() |
(ulong)GetByte() << 8 |
(ulong)GetByte() << 16 |
(ulong)GetByte() << 24 |
(ulong)GetByte() << 32 |
(ulong)GetByte() << 40 |
(ulong)GetByte() << 48 |
(ulong)GetByte() << 56;
Span<byte> bytes = stackalloc byte[8];
GetBytes(bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadUInt64BigEndian(bytes)
: BinaryPrimitives.ReadUInt64LittleEndian(bytes);
}

#pragma warning restore format
Expand Down Expand Up @@ -271,8 +212,14 @@ public float GetS15Fixed16()
/// <exception cref="IOException"/>
public string GetString(int bytesRequested, Encoding encoding)
{
#if NETSTANDARD2_1
Span<byte> bytes = bytesRequested > 2048 ? new byte[bytesRequested] : stackalloc byte[bytesRequested];
GetBytes(bytes);
return encoding.GetString(bytes);
#else
var bytes = GetBytes(bytesRequested);
return encoding.GetString(bytes, 0, bytes.Length);
#endif
}

public StringValue GetStringValue(int bytesRequested, Encoding? encoding = null)
Expand Down
Loading