5
byte[] input = new byte[] {2, 4, 5, 2, 1};
ByteBuffer bytebuf = ByteBuffer.wrap(input);

ByteBuffer.wrap(byte[] array) method makes buffer and array are inter-connected, modifications to the buffer will cause the array to be modified and vice versa.

The equivalent of ByteBuffer in C# is memorystream. But I don't know how to connect memorystream with array likes ByteBuffer.wrap() method did.

Can someone tell what is the equivalent of ByteBuffer.wrap() in C#? I have searched everywhere but could not find the answer at all.

Thanks in advance.

Steve P.
  • 14,489
  • 8
  • 42
  • 72
NoeL
  • 67
  • 1
  • 1
  • 7

2 Answers2

3

Use a binary writer and a memory stream.

I have not flagged this question as a duplicate only because you didn't ask precisely what that other poster did. I am not sure what to do in that case, but still wish to help you. Good luck!

Here's the code from that link for posterity's sake:

MemoryStream stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
    writer.Write(myByte);
    writer.Write(myInt32);
    writer.Write("Hello");
}
byte[] bytes = stream.ToArray();
Community
  • 1
  • 1
William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • hi @Morisson, the link you provided is linking back to this question. Could you fix it please? – NoeL Aug 04 '13 at 05:45
  • I don't think this is a right solution. The code you gave is just for inputting array to memorystream using BinaryWriter, it is not a method to connect stream with array. Even the last code byte[] bytes = stream.ToArray(); will result an error because the stream cannot be accessed because the stream has been closed by the BinaryWriter. – NoeL Aug 04 '13 at 05:56
  • @NoeL - care to give link (MSDN?) to prove your last statement about `ToArray` failing on closed/disposed `MemeoryStream`? – Alexei Levenkov Aug 04 '13 at 06:10
  • @AlexeiLevenkov, ToArray() doesn't close the stream, BinaryWriter did. I don't find any MSDN link, but there is a question here http://stackoverflow.com/questions/1084813/why-a-binarywriter-closes-the-outer-stream-on-disposal-and-how-to-prevent-that I have tested it and BinaryWriter always close the stream – NoeL Aug 04 '13 at 06:23
  • @NoeL I'm not sure why you dislike MSDN, but this may be useful link [MemoryStream.ToArray](http://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray.aspx) - "Note:This method works when the MemoryStream is closed." – Alexei Levenkov Aug 04 '13 at 06:31
  • Don't know how I messed that up, fixed. – William Morrison Aug 04 '13 at 17:09
0

The equivalent of wrap would be to use the constructor of MemoryStream that takes a byte[]: MemoryStream(byte[]). This 'wraps' the original array, and as such is a fixed-size buffer.

byte[] input = new byte[] {2, 4, 5, 2, 1};
using var ms = new MemoryStream(input);

The contrast with the other answer being that this preserves the contents of the array (important if you want to read from it) and doesn't create any copies, which I understand was the point of your question.

Otherwise, to read/write logical values to the buffer/stream, the answer is much the same as the existing answer: use a StreamWriter and/or StreamReader.

using (var bw = new BinaryWriter(ms, Encoding.UTF8, leaveOpen: true))
{
    bw.Write(12356789);
    
    try
    {
        bw.Write(12356789); // additional write will throw due to lack of space
    }
    catch {}
}

using (var br = new BinaryReader(ms, Encoding.UTF8, leaveOpen: true))
{
    // reset position before reading
    ms.Position = 0;
    Console.WriteLine(br.ReadInt32());
}

// check we modified the original buffer (ToArray would return a copy)
Console.WriteLine(String.Join(", ", input));
VisualMelon
  • 662
  • 12
  • 23