Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

byte collection based similar with ByteBuffer from java

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)

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

like image 224
pulancheck1988 Avatar asked Apr 09 '12 19:04

pulancheck1988


1 Answers

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;
    }
}
like image 69
Thomas Levesque Avatar answered Sep 29 '22 08:09

Thomas Levesque