byte collection based similar with ByteBuffer from java

一曲冷凌霜 提交于 2019-12-03 00:23:58

MemoryStream can do everything you want:

  • .array() => .ToArray()
  • .clear() => .SetLength(0)
  • .put(byte[], int, int) => .Write(byte[], int, int)
  • .remaining() => .Length - .Position

If you want, you can create extension methods for Clear and Remaining:

public static class MemoryStreamExtensions
{
    public static void Clear(this MemoryStream stream)
    {
        stream.SetLength(0);
    }

    public static int Remaining(this MemoryStream stream)
    {
        return stream.Length - stream.Position;
    }
}

MemoryStream should have everything you are looking for. Combined with BinaryWriter to write different data types.

var ms = new MemoryStream();
ms.SetLength(100);

long remaining = ms.Length - ms.Position; //remaining()

byte[] array = ms.ToArray(); //array()

ms.SetLength(0); //clear()

ms.Write(buffer, index, count); //put(byte[], int, int)

Are you looking for a Queue<T>?

http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

For some of the methods that Queue doesn't support, it might be easy enough to write a custom class that wraps a Queue.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!