| | 1 | | using System.Buffers; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Serialization.ValueObjects; |
| | 4 | |
|
| | 5 | | using Converters; |
| | 6 | | using Domain.Serialization.ValueObjects; |
| | 7 | | using Domain.ValueObjects; |
| | 8 | | using Domain.ValueObjects.Interfaces; |
| | 9 | |
|
| | 10 | | public class BigSizeTypeSerializer : IValueObjectTypeSerializer<BigSize> |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Serializes a BigSize value into a stream. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="valueObject">The BigSize value to serialize.</param> |
| | 16 | | /// <param name="stream">The stream where the serialized value will be written.</param> |
| | 17 | | /// <returns>A task that represents the asynchronous serialization operation.</returns> |
| | 18 | | /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception> |
| | 19 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception> |
| | 20 | | public async Task SerializeAsync(IValueObject valueObject, Stream stream) |
| | 21 | | { |
| 176 | 22 | | if (valueObject is not BigSize bigSize) |
| 0 | 23 | | throw new ArgumentException("Value object must be of type BigSize.", nameof(valueObject)); |
| | 24 | |
|
| 176 | 25 | | if (bigSize < 0xfd) |
| | 26 | | { |
| 152 | 27 | | await stream.WriteAsync(new[] { (byte)bigSize }); |
| | 28 | | } |
| 24 | 29 | | else if (bigSize < 0x10000) |
| | 30 | | { |
| 8 | 31 | | await stream.WriteAsync(new byte[] { 0xfd }); |
| 8 | 32 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)bigSize)); |
| | 33 | | } |
| 16 | 34 | | else if (bigSize < 0x100000000) |
| | 35 | | { |
| 8 | 36 | | await stream.WriteAsync(new byte[] { 0xfe }); |
| 8 | 37 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((uint)bigSize)); |
| | 38 | | } |
| | 39 | | else |
| | 40 | | { |
| 8 | 41 | | await stream.WriteAsync(new byte[] { 0xff }); |
| 8 | 42 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(bigSize.Value)); |
| | 43 | | } |
| 176 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Deserializes a BigSize value from a stream. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="stream">The stream from which the BigSize value will be deserialized.</param> |
| | 50 | | /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized BigSize |
| | 51 | | /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ |
| | 52 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception> |
| | 53 | | public async Task<BigSize> DeserializeAsync(Stream stream) |
| | 54 | | { |
| 284 | 55 | | if (stream.Position == stream.Length) |
| 8 | 56 | | throw new ArgumentException("BigSize cannot be read from an empty stream."); |
| | 57 | |
|
| 276 | 58 | | var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ulong)); |
| | 59 | |
|
| | 60 | | try |
| | 61 | | { |
| 276 | 62 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(byte)]); |
| | 63 | | ulong value; |
| | 64 | |
|
| 276 | 65 | | switch (buffer[0]) |
| | 66 | | { |
| | 67 | | case < 0xfd: |
| 228 | 68 | | value = buffer[0]; |
| 228 | 69 | | break; |
| | 70 | | // Check if there are enough bytes to read |
| 16 | 71 | | case 0xfd when stream.Position + 2 > stream.Length: |
| 8 | 72 | | throw new ArgumentException("BigSize cannot be read from a stream with insufficient data."); |
| | 73 | | case 0xfd: |
| | 74 | | { |
| 8 | 75 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]); |
| 8 | 76 | | value = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]); |
| 8 | 77 | | break; |
| | 78 | | } |
| 16 | 79 | | case 0xfe when stream.Position + 4 > stream.Length: |
| 8 | 80 | | throw new ArgumentException("BigSize cannot be read from a stream with insufficient data."); |
| | 81 | | case 0xfe: |
| | 82 | | { |
| 8 | 83 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(uint)]); |
| 8 | 84 | | value = EndianBitConverter.ToUInt32BigEndian(buffer[..sizeof(uint)]); |
| 8 | 85 | | break; |
| | 86 | | } |
| | 87 | | default: |
| | 88 | | { |
| 16 | 89 | | if (stream.Position + 8 > stream.Length) |
| | 90 | | { |
| 8 | 91 | | throw new ArgumentException("BigSize cannot be read from a stream with insufficient data."); |
| | 92 | | } |
| | 93 | |
|
| 8 | 94 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 8 | 95 | | value = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 96 | | break; |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| 252 | 100 | | return new BigSize(value); |
| | 101 | | } |
| | 102 | | finally |
| | 103 | | { |
| 276 | 104 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 105 | | } |
| 252 | 106 | | } |
| | 107 | | async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream) |
| | 108 | | { |
| 0 | 109 | | return await DeserializeAsync(stream); |
| 0 | 110 | | } |
| | 111 | | } |