基于类似于 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
byte collection based similar with ByteBuffer from java
提问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
MemoryStream
can 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 Clear
and Remaining
:
如果需要,您可以为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;
}
}
回答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 的自定义类可能很容易。