Skip to content

Commit

Permalink
Decode number types
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Mar 4, 2024
1 parent d14f85b commit 94a4bdd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
39 changes: 38 additions & 1 deletion src/ArtemisNetCoreClient/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> GetBuffer()
{
Expand All @@ -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<byte> buffer = stackalloc byte[1] { value };
_memoryStream.Write(buffer);
}

public byte ReadByte()
{
Span<byte> buffer = stackalloc byte[1];
_ = _memoryStream.Read(buffer);
return buffer[0];
}

public void WriteInt(int value)
{
Span<byte> buffer = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(buffer, value);
_memoryStream.Write(buffer);
}

public int ReadInt()
{
Span<byte> buffer = stackalloc byte[sizeof(int)];
_ = _memoryStream.Read(buffer);
return BinaryPrimitives.ReadInt32BigEndian(buffer);
}

public void WriteLong(long value)
{
Expand All @@ -41,6 +71,13 @@ public void WriteLong(long value)
_memoryStream.Write(buffer);
}

public long ReadLong()
{
Span<byte> buffer = stackalloc byte[sizeof(long)];
_ = _memoryStream.Read(buffer);
return BinaryPrimitives.ReadInt64BigEndian(buffer);
}

public void WriteNullableString(string? value)
{
if (value is null)
Expand Down
63 changes: 63 additions & 0 deletions test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })]
Expand All @@ -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()
Expand All @@ -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()
{
Expand All @@ -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()
Expand Down

0 comments on commit 94a4bdd

Please sign in to comment.