diff --git a/src/ArtemisNetCoreClient/ByteBuffer.cs b/src/ArtemisNetCoreClient/ByteBuffer.cs index 741d48d..8dae44f 100644 --- a/src/ArtemisNetCoreClient/ByteBuffer.cs +++ b/src/ArtemisNetCoreClient/ByteBuffer.cs @@ -118,6 +118,28 @@ public long ReadLong() _ = _memoryStream.Read(buffer); return BinaryPrimitives.ReadInt64BigEndian(buffer); } + + public void WriteNullableLong(long? value) + { + WriteBool(value.HasValue); + if (value.HasValue) + { + WriteLong(value.Value); + } + } + + public long? ReadNullableLong() + { + var isNotNull = ReadBool(); + if (isNotNull) + { + return ReadLong(); + } + else + { + return null; + } + } public void WriteNullableString(string? value) { diff --git a/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs b/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs index 50e54d9..b93f6d2 100644 --- a/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs +++ b/test/ArtemisNetCoreClient.Tests/ByteBufferTests.cs @@ -187,6 +187,34 @@ public void should_decode_long() Assert.That(value, Is.EqualTo(long.MaxValue)); } + [TestCase(280, new byte[] { unchecked((byte)-1), 0, 0, 0, 0, 0, 0, 1, 24 })] + [TestCase(null, new byte[] { 0 })] + public void should_encode_nullable_long(long? value, byte[] encoded) + { + // Arrange + var byteBuffer = new ByteBuffer(); + + // Act + byteBuffer.WriteNullableLong(value); + + // Assert + CollectionAssert.AreEqual(encoded, byteBuffer.GetBuffer().ToArray()); + } + + [TestCase(new byte[] { unchecked((byte)-1), 0, 0, 0, 0, 0, 0, 1, 24 }, 280)] + [TestCase(new byte[] { 0 }, null)] + public void should_decode_nullable_long(byte[] encoded, long? expected) + { + // Arrange + var byteBuffer = new ByteBuffer(encoded); + + // Act + var value = byteBuffer.ReadNullableLong(); + + // Assert + Assert.That(expected, Is.EqualTo(value)); + } + [Test] public void should_encode_short_string() {