diff --git a/src/ArtemisNetCoreClient/ByteBuffer.cs b/src/ArtemisNetCoreClient/ByteBuffer.cs index 1568297..ba37d06 100644 --- a/src/ArtemisNetCoreClient/ByteBuffer.cs +++ b/src/ArtemisNetCoreClient/ByteBuffer.cs @@ -6,7 +6,17 @@ namespace ActiveMQ.Artemis.Core.Client; internal class ByteBuffer { - private readonly MemoryStream _memoryStream = new(); + private readonly MemoryStream _memoryStream; + + public ByteBuffer() + { + _memoryStream = new MemoryStream(); + } + + public ByteBuffer(byte[] payload) + { + _memoryStream = new MemoryStream(payload, writable: false); + } public ReadOnlyMemory GetBuffer() { @@ -21,18 +31,38 @@ public void WriteBool(bool value) WriteByte(value ? minusOne : zero); } + public bool ReadBool() + { + var value = ReadByte(); + return value != 0; + } + public void WriteByte(byte value) { Span buffer = stackalloc byte[1] { value }; _memoryStream.Write(buffer); } + public byte ReadByte() + { + Span buffer = stackalloc byte[1]; + _ = _memoryStream.Read(buffer); + return buffer[0]; + } + public void WriteInt(int value) { Span buffer = stackalloc byte[sizeof(int)]; BinaryPrimitives.WriteInt32BigEndian(buffer, value); _memoryStream.Write(buffer); } + + public int ReadInt() + { + Span buffer = stackalloc byte[sizeof(int)]; + _ = _memoryStream.Read(buffer); + return BinaryPrimitives.ReadInt32BigEndian(buffer); + } public void WriteLong(long value) { @@ -41,6 +71,13 @@ public void WriteLong(long value) _memoryStream.Write(buffer); } + public long ReadLong() + { + Span buffer = stackalloc byte[sizeof(long)]; + _ = _memoryStream.Read(buffer); + return BinaryPrimitives.ReadInt64BigEndian(buffer); + } + public void WriteNullableString(string? value) { if (value is null) diff --git a/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs b/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs index 8958c12..e9cbafb 100644 --- a/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs +++ b/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs @@ -14,6 +14,20 @@ public void should_encode_byte() // Assert CollectionAssert.AreEqual(new byte[] { 125 }, byteBuffer.GetBuffer().ToArray()); } + + [Test] + public void should_decode_byte() + { + // Arrange + var buffer = new ByteBuffer([125]); + + // Act + byte value = buffer.ReadByte(); + + + // Assert + Assert.That(value, Is.EqualTo(125)); + } [TestCase(true, new[] { unchecked((byte) -1) })] [TestCase(false, new byte[] { 0 })] @@ -28,6 +42,20 @@ public void should_encode_bool(bool value, byte[] encoded) // Assert CollectionAssert.AreEqual(encoded, byteBuffer.GetBuffer().ToArray()); } + + [TestCase(new[] { unchecked((byte) -1) }, true)] + [TestCase(new byte[] { 0 }, false)] + public void should_decode_bool(byte[] encoded, bool expected) + { + // Arrange + var byteBuffer = new ByteBuffer(encoded); + + // Act + var value = byteBuffer.ReadBool(); + + // Assert + Assert.That(expected, Is.EqualTo(value)); + } [Test] public void should_encode_int() @@ -42,6 +70,19 @@ public void should_encode_int() CollectionAssert.AreEqual(new byte[] { 0, 0, 0, unchecked((byte) -86) }, byteBuffer.GetBuffer().ToArray()); } + [Test] + public void should_decode_int() + { + // Arrange + var byteBuffer = new ByteBuffer([0, 0, 0, unchecked((byte) -86)]); + + // Act + var value = byteBuffer.ReadInt(); + + // Assert + Assert.That(value, Is.EqualTo(170)); + } + [Test] public void should_encode_long() { @@ -65,6 +106,28 @@ public void should_encode_long() unchecked((byte) -1) }, byteBuffer.GetBuffer().ToArray()); } + + [Test] + public void should_decode_long() + { + // Arrange + var byteBuffer = new ByteBuffer([ + 127, + unchecked((byte) -1), + unchecked((byte) -1), + unchecked((byte) -1), + unchecked((byte) -1), + unchecked((byte) -1), + unchecked((byte) -1), + unchecked((byte) -1) + ]); + + // Act + var value = byteBuffer.ReadLong(); + + // Assert + Assert.That(value, Is.EqualTo(long.MaxValue)); + } [Test] public void should_write_short_string()