基于类似于 ByteBuffer 的字节集合来自 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10078348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:31:22  来源:igfitidea点击:

byte collection based similar with ByteBuffer from java

c#javabytebuffer

提问by pulancheck1988

I need a C# implementation of something similar with ByteBuffer from Java. Methods of interest - .remaining() - returns the number of elements between the current position and the limit. - .array() - .clear() - .put(byte[], int, int)

我需要一个类似于 Java 的 ByteBuffer 的 C# 实现。感兴趣的方法 - .remaining() - 返回当前位置和限制之间的元素数。- .array() - .clear() - .put(byte[], int, int)

I started something with MemoryStream.. but no clear(), and a lot of improvisation Also, i found a c# implementation on Koders: http://www.koders.com/csharp/fid2F8CB1B540E646746D3ADCB2B0AC867A0A8DCB06.aspx?s=socket#L2.. which I will use.. but maybe you guys know something better

我开始用MemoryStream.. 但没有clear(),还有很多即兴创作 另外,我在 Koders 上找到了 ac# 实现:http: //www.koders.com/csharp/fid2F8CB1B540E646746D3ADCB2B0AC867A0A8DCB06.aspx?s=socket#L2 ..使用..但也许你们知道更好的东西

回答by Thomas Levesque

MemoryStreamcan do everything you want:

MemoryStream可以做你想做的一切:

  • .array()=> .ToArray()
  • .clear()=> .SetLength(0)
  • .put(byte[], int, int)=> .Write(byte[], int, int)
  • .remaining()=> .Length - .Position
  • .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 Clearand Remaining:

如果需要,您可以为Clearand创建扩展方法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;
    }
}

回答by Will

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

MemoryStream 应该拥有您正在寻找的一切。结合 BinaryWriter 来写不同的数据类型。

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)

回答by Kendall Frey

Are you looking for a Queue<T>?

你在找Queue<T>吗?

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

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.

对于 Queue 不支持的一些方法,编写一个包装 Queue 的自定义类可能很容易。