简介
这篇博客主要介绍了一个字节序列类CircularBuffer,它是用于频繁创建Stream数据的循环利用。CircularBuffer类继承自Stream类,并实现了其各种方法,包括读写、查找、设置长度等。
在CircularBuffer类中,主要定义了一些关键的属性,如字节数组、偏移值、开始坐标、结束坐标、长度、当前位置、容量、限制读取长度和标记等。其中,字节数组用于存储数据,偏移值用于定位数据,开始坐标和结束坐标用于定义数据的起始和结束位置,长度表示数据的长度,当前位置表示当前读写的位置,容量表示最大可以存储的数据量,限制读取长度用于限制读取的数据长度,标记用于标记当前对象。
在CircularBuffer类中,还定义了一些方法,如初始化、设置限制读取长度、读取字节、查找字节、设置长度、写入字节、收缩、扩展、开始接收、结束接收、开始发送、结束发送、重置等。这些方法主要用于操作和管理数据。
CircularBuffer类的主要优点是可以有效地管理和操作数据,特别是在需要频繁创建Stream数据的情况下,可以有效地提高性能和效率。同时,CircularBuffer类的设计也非常灵活和通用,可以适应各种不同的应用场景。
总的来说,CircularBuffer类是一个非常实用和高效的字节序列类,值得大家在实际的开发中使用和学习。
代码如下
public class CircularBuffer : Stream
{
/// <summary>
/// 字节数组
/// </summary>
private byte[] m_Buffer;
/// <summary>
/// 偏移值
/// </summary>
private int m_Offset; // []
/// <summary>
/// 开始坐标
/// </summary>
private int m_Begin; // [0, m_Capacity)
/// <summary>
/// 结束坐标
/// </summary>
private int m_End; // [0, m_Capacity)
/// <summary>
/// 长度
/// </summary>
private int m_Length;
/// <summary>
/// 当前位置
/// </summary>
private int m_Position;
/// <summary>
/// 容量
/// </summary>
private int m_Capacity;
/// <summary>
/// 限制读取长度
/// </summary>
private int m_LimitReadLength;
/// <summary>
/// 标记
/// </summary>
private string m_Tag;
/// <summary>
/// 缓存
/// </summary>
private byte[] m_Cached = new byte[4];
/// <summary>
/// 是否刻度
/// </summary>
public override bool CanRead => true;
/// <summary>
/// 是否可定位
/// </summary>
public override bool CanSeek => true;
/// <summary>
/// 是否可写入
/// </summary>
public override bool CanWrite => true;
/// <summary>
/// 长度
/// </summary>
public override long Length => m_Length;
/// <summary>
/// 当前位置坐标
/// </summary>
public override long Position
{
get => m_Position;
set
{
if (m_Position == value) return;
Seek(value, SeekOrigin.Begin);
}
}
/// <summary>
/// 容器长度
/// </summary>
public int Capacity => m_Capacity;
/// <summary>
/// 标记
/// </summary>
public string Tag => m_Tag;
/// <summary>
/// 索引读取
/// </summary>
/// <param name="index"></param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public byte this[int index]
{
get
{
if (index < 0 || index >= m_Length)
{
throw new ArgumentOutOfRangeException($"index {index} length {m_Length}");
}
var begin = m_Begin;
var end = m_End;
if (end > begin)
{
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
// |
// m_Position
return m_Buffer[m_Offset + begin + index];
}
else
{
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
// | |
// (1) m_Position (2)
var blockLen = m_Capacity - begin;
return blockLen > index ?
//(2)
m_Buffer[m_Offset + begin + index] :
//(1)
m_Buffer[m_Offset + index - blockLen];
}
}
set
{
if (index < 0 || index >= m_Length)
{
throw new ArgumentOutOfRangeException($"index {index} length {m_Length}");
}
var begin = m_Begin;
var end = m_End;
if (end > begin)
{
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
// |
// m_Position
m_Buffer[m_Offset + begin + index] = value;
}
else
{
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
// | |
// (1) m_Position (2)
var blockLen = m_Capacity - begin;
if (blockLen > index)
{
//(2)
m_Buffer[m_Offset + begin + index] = value;
}
else
{
//(1)
m_Buffer[m_Offset + index - blockLen] = value;
}
}
}
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="tag"></param>
/// <param name="capcity"></param>
public CircularBuffer(string tag, int capcity)
{
m_Capacity = capcity;
m_Buffer = new byte[m_Capacity];
m_Offset = 0;
m_Begin = 0;
m_End = 0;
m_Length = 0;
m_Position = 0;
m_Tag = tag;
}
/// <summary>
/// 设置限制读取长度
/// </summary>
/// <param name="length"></param>
public void SetLimitReadLength(int length)
{
Trace($"{length}");
m_LimitReadLength = length;
}
#region Override
/// <summary>
/// 继承的类必须实现此方法,以便在流中写入字节。
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public override void Flush()
{
throw new NotImplementedException();
}
/// <summary>
/// 打印日志
/// </summary>
/// <param name="msg"></param>
/// <param name="memberName"></param>
/// <param name="lineNum"></param>
[Conditional("DEBUG_STREAM_TRACE")]
private static void Trace(string msg, [CallerMemberName] string memberName = "", [CallerLineNumber] int lineNum = 0)
{
//Logging.Log($"{m_Tag} {memberName}:{lineNum}:{Thread.CurrentThread.ManagedThreadId} {m_Begin} {m_End} {m_Position} {m_LimitReadLength} {m_Length}/{m_Capacity} {msg}");
}
/// <summary>
/// 读取字节未见
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public override int Read(byte[] buffer, int offset, int count)
{
Trace($"{offset} {count}");
var position = m_Position;
var dataLen = (int)Math.Min(count, (0 == m_LimitReadLength ? m_Length : m_LimitReadLength) - position);
if (0 == dataLen) return 0;
try
{
var begin = m_Begin;
var end = m_End;
if (end > begin)
{
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
// |
// m_Position
Buffer.BlockCopy(m_Buffer, m_Offset + begin + position, buffer, offset, dataLen);
}
else
{
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
// | |
// (1) m_Position (2)
var blockLen = m_Capacity - begin;
if (blockLen > position)
{
//(2)
var copyLen = blockLen - position;
var remain = dataLen - copyLen;
if (remain > 0)
{
Buffer.BlockCopy(m_Buffer, m_Offset + begin + position, buffer, offset, copyLen);
Buffer.BlockCopy(m_Buffer, m_Offset, buffer, offset + copyLen, remain);
}
else
{
Buffer.BlockCopy(m_Buffer, m_Offset + begin + position, buffer, offset, dataLen);
}
}
else
{
//(1)
Buffer.BlockCopy(m_Buffer, m_Offset + position - blockLen, buffer, offset, dataLen);
}
}
}
catch (Exception e)
{
UnityEngine.Debug.LogError($"read {m_Position} {m_LimitReadLength} {m_Length} {m_Begin} {m_End} {m_Offset} {m_Capacity}");
throw e;
}
m_Position += dataLen;
return dataLen;
}
/// <summary>
/// 查找字节
/// </summary>
/// <param name="offset"></param>
/// <param name="origin"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public override long Seek(long offset, SeekOrigin origin)
{
Trace($"{offset} {origin}");
var length = m_Length;
long position = m_Position;
switch (origin)
{
case SeekOrigin.Begin:
position = offset;
break;
case SeekOrigin.End:
position = offset + length;
break;
case SeekOrigin.Current:
position += offset;
break;
default:
throw new ArgumentOutOfRangeException(nameof(origin), origin, null);
}
m_Position = (int)Math.Max(0, Math.Min(position, length));
return m_Position;
}
/// <summary>
/// 设置长度
/// </summary>
/// <param name="value"></param>
/// <exception cref="NotImplementedException"></exception>
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// 写入字节
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public StringBuilder GetHexData(int length = -1)
{
var sb = new StringBuilder();
var begin = m_Begin;
var end = m_End;
var count = 0;
var maxCount = -1 == length ? m_Length : length;
if (end > begin || 0 == m_Length)
{
for (var i = begin; i < end && count < maxCount; i++, count++)
{
sb.Append($"{m_Buffer[m_Offset + i]:x2} ");
}
}
else
{
for (var i = begin; i < m_Capacity && count < maxCount; i++, count++)
{
sb.Append($"{m_Buffer[m_Offset + i]:x2} ");
}
for (var i = 0; i < m_End && count < maxCount; i++, count++)
{
sb.Append($"{m_Buffer[m_Offset + i]:x2} ");
}
}
return sb;
}
/// <summary>
/// 获取字节
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <returns></returns>
private StringBuilder GetHexData(byte[] buffer, int offset, int count)
{
var sb = new StringBuilder();
for (var i = 0; i < count; i++)
{
sb.Append($"{buffer[offset + i]:x2} ");
}
return sb;
}
/// <summary>
/// 写入字节
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="count"></param>
/// <exception cref="OutOfMemoryException"></exception>
/// <exception cref="Exception"></exception>
public override void Write(byte[] buffer, int offset, int count)
{
if (0 == count) return;
Trace($"{GetHexData(buffer, offset, count)}");
var position = m_Position;
var length = m_Length;
if (m_Capacity - position < count)
{
throw new OutOfMemoryException();
}
try
{
var begin = m_Begin;
var end = m_End;
var replaceLen = length - position;
if (end > begin || 0 == m_Length)
{
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
// |
// m_Position
var copyLen = m_Capacity - (begin + position);
var remain = count - copyLen;
if (remain > 0)
{
Buffer.BlockCopy(buffer, offset, m_Buffer, m_Offset + begin + position, copyLen);
Buffer.BlockCopy(buffer, offset + copyLen, m_Buffer, m_Offset, remain);
}
else
{
Buffer.BlockCopy(buffer, offset, m_Buffer, m_Offset + begin + position, count);
}
}
else
{
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
// | |
// (1) m_Position (2)
var blockLen = m_Capacity - begin;
if (blockLen > position)
{
//(2)
var copyLen = blockLen - position;
var remain = count - copyLen;
if (remain > 0)
{
Buffer.BlockCopy(buffer, offset, m_Buffer, m_Offset + begin + position, copyLen);
Buffer.BlockCopy(buffer, offset + copyLen, m_Buffer, m_Offset, remain);
}
else
{
Buffer.BlockCopy(buffer, offset, m_Buffer, m_Offset + begin + position, count);
}
}
else
{
//(1)
Buffer.BlockCopy(buffer, offset, m_Buffer, m_Offset + position - blockLen, count);
}
}
if (count > replaceLen)
{
Expand(count - replaceLen);
}
else
{
m_Position += count;
}
}
catch (Exception e)
{
UnityEngine.Debug.LogError($"write {m_Position} {m_LimitReadLength} {m_Length} {m_Begin} {m_End} {m_Offset} {m_Capacity} {count}");
throw e;
}
Trace($"{GetHexData()}");
}
/// <summary>
/// 释放
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (m_Capacity > 0)
{
m_Buffer = null;
m_Capacity = 0;
}
}
#endregion
/// <summary>
/// 收缩
/// </summary>
/// <param name="length"></param>
public void Shrink(int length)
{
Trace($"start {length}");
m_Begin += length;
if (m_Begin >= m_Capacity)
{
m_Begin -= m_Capacity;
}
if (m_Position <= length)
{
m_Position = 0;
}
else
{
m_Position -= length;
}
m_Length -= length;
Trace($"end {length}");
}
/// <summary>
/// 扩展
/// </summary>
/// <param name="length"></param>
public void Expand(int length)
{
Trace($"start {length}");
m_End += length;
if (m_End >= m_Capacity)
{
m_End -= m_Capacity;
}
m_Length += length;
m_Position = m_Length;
Trace($"end {length}");
}
/// <summary>
/// 开始接收
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public ArraySegment<byte>[] BeginRcv()
{
Trace("");
try
{
var length = m_Length;
if (m_Capacity == length)
{
return Array.Empty<ArraySegment<byte>>();
}
var begin = m_Begin;
var end = m_End;
if (end > begin || 0 == m_Length)
{
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
if (0 == begin)
{
return new ArraySegment<byte>[]
{
new ArraySegment<byte>(m_Buffer, m_Offset + end, m_Capacity - end)
};
}
return new ArraySegment<byte>[]
{
new ArraySegment<byte>(m_Buffer, m_Offset + end, m_Capacity - end),
new ArraySegment<byte>(m_Buffer, m_Offset, begin)
};
}
else
{
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
return new ArraySegment<byte>[]
{
new ArraySegment<byte>(m_Buffer, m_Offset + end, begin - end)
};
}
}
catch (Exception ex)
{
UnityEngine.Debug.LogError($"{m_Length} {m_Capacity} {m_Begin} {m_End} {m_Offset}");
throw ex;
}
}
/// <summary>
/// 结束接收
/// </summary>
/// <param name="length"></param>
/// <exception cref="OutOfMemoryException"></exception>
public void EndRcv(int length)
{
Trace($"{length}");
if (m_Capacity >= m_Length + length)
{
Expand(length);
}
else
{
throw new OutOfMemoryException();
}
}
/// <summary>
/// 开始发送
/// </summary>
/// <returns></returns>
public ArraySegment<byte>[] BeginSend()
{
if (0 == m_Length) return Array.Empty<ArraySegment<byte>>();
var begin = m_Begin;
var end = m_End;
if (end > begin)
{
Trace($"offset {m_Offset + begin} len {end - begin}");
// 0 m_Begin m_End m_Capacity
// |_______|_______|_______|
return new ArraySegment<byte>[]
{
new ArraySegment<byte>(m_Buffer, m_Offset + begin, end - begin)
};
}
else
{
Trace($"offset {m_Offset + begin} len {m_Capacity - begin} offset {m_Offset} len {end}");
// 0 m_End m_Begin m_Capacity
// |_______|_______|_______|
return new ArraySegment<byte>[]
{
new ArraySegment<byte>(m_Buffer, m_Offset + begin, m_Capacity - begin),
new ArraySegment<byte>(m_Buffer, m_Offset, end)
};
}
}
/// <summary>
/// 结束发送
/// </summary>
/// <param name="length"></param>
/// <exception cref="IndexOutOfRangeException"></exception>
public void EndSend(int length)
{
Trace($"{GetHexData(length)}");
if (m_Length >= length)
{
Shrink(length);
}
else
{
throw new IndexOutOfRangeException();
}
}
/// <summary>
/// 重置
/// </summary>
public void Reset()
{
Trace("");
m_Length = 0;
m_Begin = 0;
m_End = 0;
m_Position = 0;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="v"></param>
public void Write(int v)
{
m_Cached[0] = (byte)(v >> 0);
m_Cached[1] = (byte)(v >> 8);
m_Cached[2] = (byte)(v >> 16);
m_Cached[3] = (byte)(v >> 24);
Write(m_Cached, 0, 4);
}
/// <summary>
/// 读取
/// </summary>
/// <returns></returns>
public int ReadInt32()
{
Read(m_Cached, 0, 4);
var result = 0;
result |= (int)m_Cached[0];
result |= (int)(m_Cached[1] << 8);
result |= (int)(m_Cached[2] << 16);
result |= (int)(m_Cached[3] << 24);
return result;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="v"></param>
public void Write(uint v)
{
m_Cached[0] = (byte)(v >> 0);
m_Cached[1] = (byte)(v >> 8);
m_Cached[2] = (byte)(v >> 16);
m_Cached[3] = (byte)(v >> 24);
Write(m_Cached, 0, 4);
}
/// <summary>
/// 读取
/// </summary>
/// <returns></returns>
public uint ReadUInt32()
{
Read(m_Cached, 0, 4);
uint result = 0;
result |= (uint)m_Cached[0];
result |= (uint)(m_Cached[1] << 8);
result |= (uint)(m_Cached[2] << 16);
result |= (uint)(m_Cached[3] << 24);
return result;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="v"></param>
public void Write(ushort v)
{
m_Cached[0] = (byte)(v >> 0);
m_Cached[1] = (byte)(v >> 8);
Write(m_Cached, 0, 2);
}
/// <summary>
/// 读取
/// </summary>
/// <returns></returns>
public uint ReadUInt16()
{
Read(m_Cached, 0, 2);
ushort result = 0;
result |= (ushort)m_Cached[0];
result |= (ushort)(m_Cached[1] << 8);
return result;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="v"></param>
public void Write(short v)
{
m_Cached[0] = (byte)(v >> 0);
m_Cached[1] = (byte)(v >> 8);
Write(m_Cached, 0, 2);
}
/// <summary>
/// 读取
/// </summary>
/// <returns></returns>
public int ReadInt16()
{
Read(m_Cached, 0, 2);
short result = 0;
result |= (short)m_Cached[0];
result |= (short)(m_Cached[1] << 8);
return result;
}
/// <summary>
/// 写入
/// </summary>
/// <param name="v"></param>
public void Write(byte v)
{
m_Cached[0] = v;
Write(m_Cached, 0, 1);
}
/// <summary>
/// 读取
/// </summary>
/// <returns></returns>
public byte ReadUInt8()
{
Read(m_Cached, 0, 1);
return m_Cached[0];
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 841774407@qq.com