|
@@ -1,106 +1,177 @@
|
|
|
-using System;
|
|
|
+using NPOI.SS.Formula.Functions;
|
|
|
+using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Collections.Immutable;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace InABox.DigitalMatter
|
|
|
{
|
|
|
- public class DMBuffer
|
|
|
+ public interface IDMBuffer
|
|
|
{
|
|
|
- public byte[] Buffer
|
|
|
+ /// <summary>
|
|
|
+ /// The remaining size of the buffer.
|
|
|
+ /// </summary>
|
|
|
+ public ushort BufferSize { get; }
|
|
|
+
|
|
|
+
|
|
|
+ #region Bit Manipulation
|
|
|
+
|
|
|
+ private static bool[] DecodeBits(byte[] value, int index, int length)
|
|
|
{
|
|
|
- get => _buffer.ToArray();
|
|
|
- set => _buffer = value.ToList();
|
|
|
+ var result = new List<bool>();
|
|
|
+ var array = new BitArray(value);
|
|
|
+ for (var i = index; i < index + length && i < array.Count; i++)
|
|
|
+ result.Add(array[i]);
|
|
|
+ return result.ToArray();
|
|
|
}
|
|
|
|
|
|
- public ushort BufferSize => (ushort)_buffer.Count;
|
|
|
+ private static byte[] EncodeBits(byte[] source, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ var array = new BitArray(source);
|
|
|
+ for (var i = 0; i < bits.Length; i++)
|
|
|
+ array.Set(index + i, bits[i]);
|
|
|
+ var result = new byte[source.Length];
|
|
|
+ array.CopyTo(result, 0);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
- public virtual void Reset()
|
|
|
+ public static bool[] GetBits(byte[] value, int index, int length)
|
|
|
{
|
|
|
- _buffer = new List<byte>();
|
|
|
+ return DecodeBits(value, index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddByte(byte value)
|
|
|
+ public static byte[] SetBits(byte[] value, int index, bool[] bits)
|
|
|
{
|
|
|
- return Add(new[] { value });
|
|
|
+ return EncodeBits(value, index, bits);
|
|
|
}
|
|
|
|
|
|
- public ushort AddBytes(byte[] value)
|
|
|
+ public static bool[] DecodeByte(byte value, int index, int length)
|
|
|
{
|
|
|
- return Add(value);
|
|
|
+ return DecodeBits(new[] { value }, index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddInt8(sbyte value)
|
|
|
+ public static bool[] DecodeBytes(byte[] value, int index, int length)
|
|
|
{
|
|
|
- return Add(BitConverter.GetBytes(value));
|
|
|
+ return DecodeBits(value, index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddUInt16(ushort value)
|
|
|
+ public static bool[] DecodeInt8(sbyte value, int index, int length)
|
|
|
{
|
|
|
- return Add(BitConverter.GetBytes(value));
|
|
|
+ return DecodeBits(new[] { Convert.ToByte(value) }, index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddInt16(short value)
|
|
|
+ public static bool[] DecodeInt16(short value, int index, int length)
|
|
|
{
|
|
|
- return Add(BitConverter.GetBytes(value));
|
|
|
+ return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddUInt32(uint value)
|
|
|
+ public static bool[] DecodeUInt16(ushort value, int index, int length)
|
|
|
{
|
|
|
- return Add(BitConverter.GetBytes(value));
|
|
|
+ return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddInt32(int value)
|
|
|
+ public static bool[] DecodeInt32(int value, int index, int length)
|
|
|
{
|
|
|
- return Add(BitConverter.GetBytes(value));
|
|
|
+ return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort AddString(string value, int length)
|
|
|
+ public static bool[] DecodeUInt32(uint value, int index, int length)
|
|
|
{
|
|
|
- return Add(FixedLengthString(value, length));
|
|
|
+ return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertByte(int index, byte value)
|
|
|
+ public static byte EncodeByte(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, new[] { value });
|
|
|
+ return EncodeBits(new byte[] { default }, 0, bits).First();
|
|
|
}
|
|
|
|
|
|
- public ushort InsertBytes(int index, byte[] value)
|
|
|
+ public static byte[] EncodeBytes(bool[] bits, int size)
|
|
|
{
|
|
|
- return Insert(index, value);
|
|
|
+ return EncodeBits(Array.CreateInstance(typeof(byte), size).OfType<byte>().ToArray(), 0, bits);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertInt8(int index, sbyte value)
|
|
|
+ public static sbyte EncodeInt8(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, BitConverter.GetBytes(value));
|
|
|
+ return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(default(sbyte)) }, 0, bits).First());
|
|
|
}
|
|
|
|
|
|
- public ushort InsertUInt16(int index, ushort value)
|
|
|
+ public static short EncodeInt16(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, BitConverter.GetBytes(value));
|
|
|
+ return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(default(short)), 0, bits), 0);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertInt16(int index, short value)
|
|
|
+ public static ushort EncodeUInt16(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, BitConverter.GetBytes(value));
|
|
|
+ return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(default(ushort)), 0, bits), 0);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertUInt32(int index, uint value)
|
|
|
+ public static int EncodeInt32(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, BitConverter.GetBytes(value));
|
|
|
+ return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(default(int)), 0, bits), 0);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertInt32(int index, int value)
|
|
|
+ public static uint EncodeUInt32(bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, BitConverter.GetBytes(value));
|
|
|
+ return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(default(uint)), 0, bits), 0);
|
|
|
}
|
|
|
|
|
|
- public ushort InsertString(int index, string value, int length)
|
|
|
+ public static byte UpdateByte(byte value, int index, bool[] bits)
|
|
|
{
|
|
|
- return Insert(index, FixedLengthString(value, length));
|
|
|
+ return EncodeBits(new[] { value }, index, bits).First();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] UpdateBytes(byte[] value, int index, bool[] bits, int size)
|
|
|
+ {
|
|
|
+ return EncodeBits(value, 0, bits);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static sbyte UpdateInt8(sbyte value, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(value) }, 0, bits).First());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static short UpdateInt16(short value, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
}
|
|
|
|
|
|
+ public static ushort UpdateUInt16(ushort value, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int UpdateInt32(int value, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static uint UpdateUInt32(uint value, int index, bool[] bits)
|
|
|
+ {
|
|
|
+ return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface IDMReadBuffer : IDMBuffer
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// Read <paramref name="length"/> bytes, starting at <paramref name="index"/>, but don't advance the read pointer.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="index">Index to start the read.</param>
|
|
|
+ /// <param name="length">Number of bytes to read.</param>
|
|
|
+ /// <returns>The bytes read.</returns>
|
|
|
+ protected byte[] Peek(int index, int length);
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Read <paramref name="length"/> bytes, advancing the read pointer by <paramref name="length"/>.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="length">Number of bytes to read.</param>
|
|
|
+ /// <returns>The bytes read.</returns>
|
|
|
+ protected byte[] Take(int length);
|
|
|
+
|
|
|
public byte PeekByte(int index)
|
|
|
{
|
|
|
return Peek(index, sizeof(byte)).First();
|
|
@@ -142,262 +213,337 @@ namespace InABox.DigitalMatter
|
|
|
return Encoding.Default.GetString(Peek(index, length)).Trim();
|
|
|
}
|
|
|
|
|
|
- public byte TakeByte(int index)
|
|
|
+ public byte TakeByte()
|
|
|
{
|
|
|
- return Take(index, sizeof(byte)).First();
|
|
|
+ return Take(sizeof(byte)).First();
|
|
|
}
|
|
|
|
|
|
- public byte[] TakeBytes(int index, int length)
|
|
|
+ public byte[] TakeBytes(int length)
|
|
|
{
|
|
|
- return Take(index, length);
|
|
|
+ return Take(length);
|
|
|
}
|
|
|
|
|
|
- public sbyte TakeInt8(int index)
|
|
|
+ public sbyte TakeInt8()
|
|
|
{
|
|
|
- return unchecked((sbyte)TakeByte(index));
|
|
|
+ return unchecked((sbyte)TakeByte());
|
|
|
//hmmm...
|
|
|
}
|
|
|
|
|
|
- public ushort TakeUInt16(int index)
|
|
|
+ public ushort TakeUInt16()
|
|
|
{
|
|
|
- return BitConverter.ToUInt16(Take(index, sizeof(ushort)), 0);
|
|
|
+ return BitConverter.ToUInt16(Take(sizeof(ushort)), 0);
|
|
|
}
|
|
|
|
|
|
- public short TakeInt16(int index)
|
|
|
+ public short TakeInt16()
|
|
|
{
|
|
|
- return BitConverter.ToInt16(Take(index, sizeof(short)), 0);
|
|
|
+ return BitConverter.ToInt16(Take(sizeof(short)), 0);
|
|
|
}
|
|
|
|
|
|
- public uint TakeUInt32(int index)
|
|
|
+ public uint TakeUInt32()
|
|
|
{
|
|
|
- return BitConverter.ToUInt32(Take(index, sizeof(uint)), 0);
|
|
|
+ return BitConverter.ToUInt32(Take(sizeof(uint)), 0);
|
|
|
}
|
|
|
|
|
|
- public int TakeInt32(int index)
|
|
|
+ public int TakeInt32()
|
|
|
{
|
|
|
- return BitConverter.ToInt32(Take(index, sizeof(int)), 0);
|
|
|
+ return BitConverter.ToInt32(Take(sizeof(int)), 0);
|
|
|
}
|
|
|
|
|
|
- public string TakeString(int index, int length)
|
|
|
+ public string TakeString(int length)
|
|
|
{
|
|
|
- return Encoding.Default.GetString(Take(index, length)).Trim();
|
|
|
+ return Encoding.Default.GetString(Take(length)).Trim();
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- public byte TakeByte()
|
|
|
+ public interface IDMWriteBuffer : IDMBuffer
|
|
|
+ {
|
|
|
+ protected ushort Add(byte[] bytes);
|
|
|
+
|
|
|
+ protected ushort Insert(int index, byte[] bytes);
|
|
|
+
|
|
|
+ void Reset();
|
|
|
+
|
|
|
+ public ushort AddByte(byte value)
|
|
|
{
|
|
|
- return TakeByte(0);
|
|
|
+ return Add(new[] { value });
|
|
|
}
|
|
|
|
|
|
- public byte[] TakeBytes(int length)
|
|
|
+ public ushort AddBytes(byte[] value)
|
|
|
{
|
|
|
- return TakeBytes(0, length);
|
|
|
+ return Add(value);
|
|
|
}
|
|
|
|
|
|
- public sbyte TakeInt8()
|
|
|
+ public ushort AddInt8(sbyte value)
|
|
|
{
|
|
|
- return TakeInt8(0);
|
|
|
- //hmmm...
|
|
|
+ return Add(BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- public ushort TakeUInt16()
|
|
|
+ public ushort AddUInt16(ushort value)
|
|
|
{
|
|
|
- return TakeUInt16(0);
|
|
|
+ return Add(BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- public short TakeInt16()
|
|
|
+ public ushort AddInt16(short value)
|
|
|
{
|
|
|
- return TakeInt16(0);
|
|
|
+ return Add(BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- public uint TakeUInt32()
|
|
|
+ public ushort AddUInt32(uint value)
|
|
|
{
|
|
|
- return TakeUInt32(0);
|
|
|
+ return Add(BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- public int TakeInt32()
|
|
|
+ public ushort AddInt32(int value)
|
|
|
{
|
|
|
- return TakeInt32(0);
|
|
|
+ return Add(BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- public string TakeString(int length)
|
|
|
+ public ushort AddString(string value, int length)
|
|
|
{
|
|
|
- return TakeString(0, length);
|
|
|
+ return Add(FixedLengthString(value, length));
|
|
|
}
|
|
|
|
|
|
- #region Private Members
|
|
|
+ public ushort InsertByte(int index, byte value)
|
|
|
+ {
|
|
|
+ return Insert(index, new[] { value });
|
|
|
+ }
|
|
|
|
|
|
- private List<byte> _buffer = new();
|
|
|
+ public ushort InsertBytes(int index, byte[] value)
|
|
|
+ {
|
|
|
+ return Insert(index, value);
|
|
|
+ }
|
|
|
|
|
|
- private ushort Add(byte[] bytes)
|
|
|
+ public ushort InsertInt8(int index, sbyte value)
|
|
|
{
|
|
|
- _buffer.AddRange(bytes);
|
|
|
- return BufferSize;
|
|
|
+ return Insert(index, BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- private ushort Insert(int index, byte[] bytes)
|
|
|
+ public ushort InsertUInt16(int index, ushort value)
|
|
|
{
|
|
|
- _buffer.InsertRange(index, bytes);
|
|
|
- return BufferSize;
|
|
|
+ return Insert(index, BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- private byte[] Peek(int index, int length)
|
|
|
+ public ushort InsertInt16(int index, short value)
|
|
|
{
|
|
|
- return _buffer.Skip(index).Take(length).ToArray();
|
|
|
+ return Insert(index, BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- private byte[] Take(int index, int length)
|
|
|
+ public ushort InsertUInt32(int index, uint value)
|
|
|
{
|
|
|
- var result = Peek(index, length);
|
|
|
- _buffer.RemoveRange(index, length);
|
|
|
- return result;
|
|
|
+ return Insert(index, BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- private byte[] FixedLengthString(string value, int length)
|
|
|
+ public ushort InsertInt32(int index, int value)
|
|
|
{
|
|
|
- var result = Encoding.Default.GetBytes(value).ToList();
|
|
|
- while (result.Count < length)
|
|
|
- result.Add(0x00);
|
|
|
- return result.ToArray();
|
|
|
+ return Insert(index, BitConverter.GetBytes(value));
|
|
|
}
|
|
|
|
|
|
- private bool[] DecodeBits(byte[] value, int index, int length)
|
|
|
+ public ushort InsertString(int index, string value, int length)
|
|
|
{
|
|
|
- var result = new List<bool>();
|
|
|
- var array = new BitArray(value);
|
|
|
- for (var i = index; i < index + length && i < array.Count; i++)
|
|
|
- result.Add(array[i]);
|
|
|
- return result.ToArray();
|
|
|
+ return Insert(index, FixedLengthString(value, length));
|
|
|
}
|
|
|
|
|
|
- private byte[] EncodeBits(byte[] source, int index, bool[] bits)
|
|
|
+ #region Private Members
|
|
|
+
|
|
|
+ private static byte[] FixedLengthString(string value, int length)
|
|
|
{
|
|
|
- var array = new BitArray(source);
|
|
|
- for (var i = 0; i < bits.Length; i++)
|
|
|
- array.Set(index + i, bits[i]);
|
|
|
- var result = new byte[source.Length];
|
|
|
- array.CopyTo(result, 0);
|
|
|
- return result;
|
|
|
+ var result = Encoding.Default.GetBytes(value).ToList();
|
|
|
+ while (result.Count < length)
|
|
|
+ result.Add(0x00);
|
|
|
+ return result.ToArray();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
+ }
|
|
|
|
|
|
- #region Bit Manipulation
|
|
|
+ public class DMArrayReadBuffer : IDMReadBuffer
|
|
|
+ {
|
|
|
+ private byte[] _buffer { get; set; }
|
|
|
+ private int _offset;
|
|
|
|
|
|
- public bool[] GetBits(byte[] value, int index, int length)
|
|
|
- {
|
|
|
- return DecodeBits(value, index, length);
|
|
|
- }
|
|
|
+ public ushort BufferSize => (ushort)(_buffer.Length - _offset);
|
|
|
|
|
|
- public byte[] SetBits(byte[] value, int index, bool[] bits)
|
|
|
+ public DMArrayReadBuffer(byte[] buffer)
|
|
|
{
|
|
|
- return EncodeBits(value, index, bits);
|
|
|
+ _buffer = buffer;
|
|
|
+ _offset = 0;
|
|
|
}
|
|
|
|
|
|
- public bool[] DecodeByte(byte value, int index, int length)
|
|
|
+ byte[] IDMReadBuffer.Peek(int index, int length)
|
|
|
{
|
|
|
- return DecodeBits(new[] { value }, index, length);
|
|
|
+ index += _offset;
|
|
|
+ length = Math.Min(length, _buffer.Length - index);
|
|
|
+ var newBytes = new byte[length];
|
|
|
+ Buffer.BlockCopy(_buffer, index, newBytes, 0, length);
|
|
|
+ return newBytes;
|
|
|
}
|
|
|
|
|
|
- public bool[] DecodeBytes(byte[] value, int index, int length)
|
|
|
+ byte[] IDMReadBuffer.Take(int length)
|
|
|
{
|
|
|
- return DecodeBits(value, index, length);
|
|
|
+ length = Math.Min(length, _buffer.Length - _offset);
|
|
|
+ var newBytes = new byte[length];
|
|
|
+ Buffer.BlockCopy(_buffer, _offset, newBytes, 0, length);
|
|
|
+ _offset += length;
|
|
|
+ return newBytes;
|
|
|
}
|
|
|
+ }
|
|
|
+ public class DMListReadBuffer : IDMReadBuffer
|
|
|
+ {
|
|
|
+ private List<byte> _buffer { get; set; }
|
|
|
+ private int _offset;
|
|
|
|
|
|
- public bool[] DecodeInt8(sbyte value, int index, int length)
|
|
|
- {
|
|
|
- return DecodeBits(new[] { Convert.ToByte(value) }, index, length);
|
|
|
- }
|
|
|
+ public ushort BufferSize => (ushort)(_buffer.Count - _offset);
|
|
|
|
|
|
- public bool[] DecodeInt16(short value, int index, int length)
|
|
|
+ public DMListReadBuffer(List<byte> buffer)
|
|
|
{
|
|
|
- return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
+ _buffer = buffer;
|
|
|
+ _offset = 0;
|
|
|
}
|
|
|
|
|
|
- public bool[] DecodeUInt16(ushort value, int index, int length)
|
|
|
+ byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
|
|
|
+
|
|
|
+ private byte[] Peek(int index, int length)
|
|
|
{
|
|
|
- return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
+ index += _offset;
|
|
|
+ length = Math.Min(length, _buffer.Count - index);
|
|
|
+ var newBytes = new byte[length];
|
|
|
+ _buffer.CopyTo(index, newBytes, 0, length);
|
|
|
+ return newBytes;
|
|
|
}
|
|
|
-
|
|
|
- public bool[] DecodeInt32(int value, int index, int length)
|
|
|
+
|
|
|
+ byte[] IDMReadBuffer.Take(int length)
|
|
|
{
|
|
|
- return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
+ var result = Peek(0, length);
|
|
|
+ _offset += length;
|
|
|
+ return result;
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ public class DMPartialReadBuffer : IDMReadBuffer
|
|
|
+ {
|
|
|
+ private ushort _offset;
|
|
|
+ private ushort _length;
|
|
|
+
|
|
|
+ public IDMReadBuffer Buffer { get; }
|
|
|
+ public ushort BufferSize => (ushort)(_length - (Offset - _offset));
|
|
|
+ public ushort Offset { get; private set; }
|
|
|
|
|
|
- public bool[] DecodeUInt32(uint value, int index, int length)
|
|
|
+ public DMPartialReadBuffer(IDMReadBuffer buffer, ushort offset, ushort length)
|
|
|
{
|
|
|
- return DecodeBits(BitConverter.GetBytes(value), index, length);
|
|
|
+ _length = length;
|
|
|
+ _offset = offset;
|
|
|
+ Offset = offset;
|
|
|
+ Buffer = buffer;
|
|
|
}
|
|
|
|
|
|
- public byte EncodeByte(bool[] bits)
|
|
|
+ byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
|
|
|
+
|
|
|
+ private byte[] Peek(int index, int length)
|
|
|
{
|
|
|
- return EncodeBits(new byte[] { default }, 0, bits).First();
|
|
|
+ index += Offset;
|
|
|
+ length = Math.Min(length, _offset + _length - index);
|
|
|
+ return Buffer.PeekBytes(index, length);
|
|
|
}
|
|
|
|
|
|
- public byte[] EncodeBytes(bool[] bits, int size)
|
|
|
+ byte[] IDMReadBuffer.Take(int length)
|
|
|
{
|
|
|
- return EncodeBits(Array.CreateInstance(typeof(byte), size).OfType<byte>().ToArray(), 0, bits);
|
|
|
+ var bytes = Peek(0, length);
|
|
|
+ Offset += (ushort)bytes.Length;
|
|
|
+ return bytes;
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ public class DMListWriteBuffer : IDMWriteBuffer
|
|
|
+ {
|
|
|
+ private List<byte> _buffer { get; set; }
|
|
|
+ private int _offset;
|
|
|
+
|
|
|
+ public List<byte> Buffer => _buffer;
|
|
|
+
|
|
|
+ public ushort BufferSize => (ushort)(_buffer.Count - _offset);
|
|
|
|
|
|
- public sbyte EncodeInt8(bool[] bits)
|
|
|
+ public DMListWriteBuffer(List<byte> buffer)
|
|
|
{
|
|
|
- return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(default(sbyte)) }, 0, bits).First());
|
|
|
+ _buffer = buffer;
|
|
|
+ _offset = 0;
|
|
|
}
|
|
|
|
|
|
- public short EncodeInt16(bool[] bits)
|
|
|
+ public void Reset()
|
|
|
{
|
|
|
- return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(default(short)), 0, bits), 0);
|
|
|
+ _buffer.Clear();
|
|
|
}
|
|
|
|
|
|
- public ushort EncodeUInt16(bool[] bits)
|
|
|
+ ushort IDMWriteBuffer.Add(byte[] bytes)
|
|
|
{
|
|
|
- return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(default(ushort)), 0, bits), 0);
|
|
|
+ _buffer.AddRange(bytes);
|
|
|
+ return BufferSize;
|
|
|
}
|
|
|
|
|
|
- public int EncodeInt32(bool[] bits)
|
|
|
+ ushort IDMWriteBuffer.Insert(int index, byte[] bytes)
|
|
|
{
|
|
|
- return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(default(int)), 0, bits), 0);
|
|
|
+ _buffer.InsertRange(index, bytes);
|
|
|
+ return BufferSize;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- public uint EncodeUInt32(bool[] bits)
|
|
|
+ public class DMBuffer : IDMReadBuffer, IDMWriteBuffer
|
|
|
+ {
|
|
|
+ public byte[] Buffer
|
|
|
{
|
|
|
- return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(default(uint)), 0, bits), 0);
|
|
|
+ get => _buffer.ToArray();
|
|
|
+ set => _buffer = value.ToList();
|
|
|
}
|
|
|
|
|
|
- public byte UpdateByte(byte value, int index, bool[] bits)
|
|
|
+ public DMBuffer() { }
|
|
|
+
|
|
|
+ public DMBuffer(byte[] buffer)
|
|
|
{
|
|
|
- return EncodeBits(new[] { value }, index, bits).First();
|
|
|
+ Buffer = buffer;
|
|
|
}
|
|
|
|
|
|
- public byte[] UpdateBytes(byte[] value, int index, bool[] bits, int size)
|
|
|
+ public DMBuffer(List<byte> buffer)
|
|
|
{
|
|
|
- return EncodeBits(value, 0, bits);
|
|
|
+ _buffer = buffer;
|
|
|
}
|
|
|
|
|
|
- public sbyte UpdateInt8(sbyte value, int index, bool[] bits)
|
|
|
+ public ushort BufferSize => (ushort)_buffer.Count;
|
|
|
+
|
|
|
+ public void Reset()
|
|
|
{
|
|
|
- return Convert.ToSByte(EncodeBits(new[] { Convert.ToByte(value) }, 0, bits).First());
|
|
|
+ _buffer = new List<byte>();
|
|
|
}
|
|
|
|
|
|
- public short UpdateInt16(short value, int index, bool[] bits)
|
|
|
+
|
|
|
+ #region Private Members
|
|
|
+
|
|
|
+ private List<byte> _buffer = new();
|
|
|
+
|
|
|
+ ushort IDMWriteBuffer.Add(byte[] bytes)
|
|
|
{
|
|
|
- return BitConverter.ToInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ _buffer.AddRange(bytes);
|
|
|
+ return BufferSize;
|
|
|
}
|
|
|
|
|
|
- public ushort UpdateUInt16(ushort value, int index, bool[] bits)
|
|
|
+ ushort IDMWriteBuffer.Insert(int index, byte[] bytes)
|
|
|
{
|
|
|
- return BitConverter.ToUInt16(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ _buffer.InsertRange(index, bytes);
|
|
|
+ return BufferSize;
|
|
|
}
|
|
|
-
|
|
|
- public int UpdateInt32(int value, int index, bool[] bits)
|
|
|
+ private byte[] Peek(int index, int length)
|
|
|
{
|
|
|
- return BitConverter.ToInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ return _buffer.Skip(index).Take(length).ToArray();
|
|
|
}
|
|
|
|
|
|
- public uint UpdateUInt32(uint value, int index, bool[] bits)
|
|
|
+ byte[] IDMReadBuffer.Peek(int index, int length) => Peek(index, length);
|
|
|
+
|
|
|
+ byte[] IDMReadBuffer.Take(int length)
|
|
|
{
|
|
|
- return BitConverter.ToUInt32(EncodeBits(BitConverter.GetBytes(value), 0, bits), 0);
|
|
|
+ var result = Peek(0, length);
|
|
|
+ _buffer.RemoveRange(0, length);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
+
|
|
|
}
|
|
|
}
|