7 Commits
1.3.4 ... 1.3.5

Author SHA1 Message Date
Matt Nadareski
c48d62fd5e Bump version 2024-04-16 21:49:55 -04:00
Matt Nadareski
0f10dc2ae4 Move more classes, fix build 2024-04-16 21:47:41 -04:00
Matt Nadareski
c648ad9f5e Move extensions to new namespace 2024-04-16 21:45:26 -04:00
Matt Nadareski
cd01e170fe Port ReadOnlyBitStream from Compression, add tests 2024-04-16 20:13:27 -04:00
Matt Nadareski
ecaec1d44a Add single-stream constructor to ROCS 2024-04-16 20:02:54 -04:00
Matt Nadareski
ce521d92ca Fix test build issue 2024-04-16 13:38:08 -04:00
Matt Nadareski
a69b6dfa3a Move composite stream to new namespace 2024-04-16 13:27:23 -04:00
11 changed files with 329 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
using SabreTools.IO.Extensions;
using Xunit;
namespace SabreTools.IO.Test
namespace SabreTools.IO.Test.Extensions
{
public class IOExtensionsTests
{

View File

@@ -0,0 +1,54 @@
using System.IO;
using SabreTools.IO.Streams;
using Xunit;
namespace SabreTools.IO.Test.Streams
{
public class ReadOnlyBitStreamTests
{
[Fact]
public void DefaultConstructorTest()
{
var stream = new ReadOnlyBitStream(new MemoryStream());
Assert.Equal(0, stream.Length);
Assert.Equal(0, stream.Position);
stream = new ReadOnlyBitStream(new MemoryStream(new byte[16]));
Assert.Equal(16, stream.Length);
Assert.Equal(0, stream.Position);
}
[Fact]
public void ReadSingleBitTest()
{
byte[] data = [0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
byte? bit = stream.ReadBit();
Assert.NotNull(bit);
Assert.Equal((byte)0b00000001, bit);
Assert.Equal(1, stream.Position);
}
[Fact]
public void ReadBitsLSBTest()
{
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
uint? bits = stream.ReadBitsLSB(4);
Assert.NotNull(bits);
Assert.Equal((byte)0b00000101, bits);
Assert.Equal(1, stream.Position);
}
[Fact]
public void ReadBitsMSBTest()
{
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
uint? bits = stream.ReadBitsMSB(4);
Assert.NotNull(bits);
Assert.Equal((byte)0b00001010, bits);
Assert.Equal(1, stream.Position);
}
}
}

View File

@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using SabreTools.IO.Streams;
using Xunit;
namespace SabreTools.IO.Test
namespace SabreTools.IO.Test.Streams
{
public class ReadOnlyCompositeStreamTests
{
@@ -34,6 +35,14 @@ namespace SabreTools.IO.Test
Assert.Equal(0, stream.Position);
}
[Fact]
public void SingleStreamConstructorTest()
{
var stream = new ReadOnlyCompositeStream(new MemoryStream(new byte[1024]));
Assert.Equal(1024, stream.Length);
Assert.Equal(0, stream.Position);
}
[Fact]
public void FilledArrayConstructorTest()
{

View File

@@ -1,7 +1,7 @@
using System;
using System.IO;
namespace SabreTools.IO
namespace SabreTools.IO.Extensions
{
/// <summary>
/// Big endian reading overloads for BinaryReader

View File

@@ -2,7 +2,7 @@ using System;
using System.Collections.Generic;
using System.Text;
namespace SabreTools.IO
namespace SabreTools.IO.Extensions
{
/// <summary>
/// Extensions for byte arrays

View File

@@ -3,7 +3,7 @@ using System.IO;
using System.Linq;
using System.Text;
namespace SabreTools.IO
namespace SabreTools.IO.Extensions
{
/// <summary>
/// Methods around path operations

View File

@@ -4,7 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
namespace SabreTools.IO
namespace SabreTools.IO.Extensions
{
/// <summary>
/// Extensions for Streams

View File

@@ -1,7 +1,7 @@
using System;
using System.Xml;
namespace SabreTools.IO
namespace SabreTools.IO.Extensions
{
/// <summary>
/// Additional methods for XmlTextWriter

View File

@@ -7,7 +7,7 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.3.4</Version>
<Version>1.3.5</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>

View File

@@ -0,0 +1,239 @@
using System;
using System.IO;
using SabreTools.IO.Extensions;
namespace SabreTools.IO.Streams
{
/// <summary>
/// Wrapper to allow reading bits from a source stream
/// </summary>
public class ReadOnlyBitStream
{
/// <inheritdoc cref="Stream.Position"/>
public long Position => _source.Position;
/// <inheritdoc cref="Stream.Length"/>
public long Length => _source.Length;
/// <summary>
/// Original stream source
/// </summary>
private readonly Stream _source;
/// <summary>
/// Last read byte value from the stream
/// </summary>
private byte? _bitBuffer;
/// <summary>
/// Index in the byte of the current bit
/// </summary>
private int _bitIndex;
/// <summary>
/// Create a new BitStream from a source Stream
/// </summary>
public ReadOnlyBitStream(Stream source)
{
_source = source;
_bitBuffer = null;
_bitIndex = 0;
// Verify the stream
if (!source.CanRead || !source.CanSeek)
throw new ArgumentException($"{nameof(source)} needs to be readable and seekable");
}
/// <summary>
/// Discard the current cached byte
/// </summary>
public void Discard()
{
_bitBuffer = null;
_bitIndex = 0;
}
/// <summary>
/// Read a single bit, if possible
/// </summary>
/// <returns>The next bit encoded in a byte, null on error or end of stream</returns>
public byte? ReadBit()
{
// If we reached the end of the stream
if (_source.Position >= _source.Length)
return null;
// If we don't have a value cached
if (_bitBuffer == null)
{
// Read the next byte, if possible
_bitBuffer = ReadSourceByte();
if (_bitBuffer == null)
return null;
// Reset the bit index
_bitIndex = 0;
}
// Get the value by bit-shifting
int value = _bitBuffer.Value & 0x01;
_bitBuffer = (byte?)(_bitBuffer >> 1);
_bitIndex++;
// Reset the byte if we're at the end
if (_bitIndex >= 8)
Discard();
return (byte)value;
}
/// <summary>
/// Read a multiple bits in LSB, if possible
/// </summary>
/// <returns>The next bits encoded in a UInt32, null on error or end of stream</returns>
public uint? ReadBitsLSB(int bits)
{
uint value = 0;
for (int i = 0; i < bits; i++)
{
// Read the next bit
byte? bitValue = ReadBit();
if (bitValue == null)
return null;
// Add the bit shifted by the current index
value += (uint)(bitValue.Value << i);
}
return value;
}
/// <summary>
/// Read a multiple bits in MSB, if possible
/// </summary>
/// <returns>The next bits encoded in a UInt32, null on error or end of stream</returns>
public uint? ReadBitsMSB(int bits)
{
uint value = 0;
for (int i = 0; i < bits; i++)
{
// Read the next bit
byte? bitValue = ReadBit();
if (bitValue == null)
return null;
// Add the bit shifted by the current index
value = (value << 1) + bitValue.Value;
}
return value;
}
/// <summary>
/// Read a byte, if possible
/// </summary>
/// <returns>The next byte, null on error or end of stream</returns>
/// <remarks>Assumes the stream is byte-aligned</remarks>
public byte? ReadByte()
{
try
{
Discard();
return _source.ReadByteValue();
}
catch
{
return null;
}
}
/// <summary>
/// Read a UInt16, if possible
/// </summary>
/// <returns>The next UInt16, null on error or end of stream</returns>
/// <remarks>Assumes the stream is byte-aligned</remarks>
public ushort? ReadUInt16()
{
try
{
Discard();
return _source.ReadUInt16();
}
catch
{
return null;
}
}
/// <summary>
/// Read a UInt32, if possible
/// </summary>
/// <returns>The next UInt32, null on error or end of stream</returns>
/// <remarks>Assumes the stream is byte-aligned</remarks>
public uint? ReadUInt32()
{
try
{
Discard();
return _source.ReadUInt32();
}
catch
{
return null;
}
}
/// <summary>
/// Read a UInt64, if possible
/// </summary>
/// <returns>The next UInt64, null on error or end of stream</returns>
/// <remarks>Assumes the stream is byte-aligned</remarks>
public ulong? ReadUInt64()
{
try
{
Discard();
return _source.ReadUInt64();
}
catch
{
return null;
}
}
/// <summary>
/// Read <paramref name="bytes"/> bytes, if possible
/// </summary>
/// <param name="bytes">Number of bytes to read</param>
/// <returns>The next <paramref name="bytes"/> bytes, null on error or end of stream</returns>
/// <remarks>Assumes the stream is byte-aligned</remarks>
public byte[]? ReadBytes(int bytes)
{
try
{
Discard();
return _source.ReadBytes(bytes);
}
catch
{
return null;
}
}
/// <summary>
/// Read a single byte from the underlying stream, if possible
/// </summary>
/// <returns>The next full byte from the stream, null on error or end of stream</returns>
private byte? ReadSourceByte()
{
try
{
return _source.ReadByteValue();
}
catch
{
return null;
}
}
}
}

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SabreTools.IO
namespace SabreTools.IO.Streams
{
/// <summary>
/// Read-only stream wrapper around multiple, consecutive streams
@@ -69,6 +69,23 @@ namespace SabreTools.IO
_position = 0;
}
/// <summary>
/// Create a new ReadOnlyCompositeStream from a single Stream
/// </summary>
/// <param name="stream"></param>
public ReadOnlyCompositeStream(Stream stream)
{
_streams = [stream];
_length = 0;
_position = 0;
// Verify the stream and add to the length
if (!stream.CanRead || !stream.CanSeek)
throw new ArgumentException($"{nameof(stream)} needs to be readable and seekable");
_length += stream.Length;
}
/// <summary>
/// Create a new ReadOnlyCompositeStream from an existing collection of Streams
/// </summary>