Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is IBuffer for?

Tags:

c#

interface

Tinkering with some low-level code, I stumbled upon .Net's IBuffer interface. This interface declares just two properties - Length and Capacity.

Questions:

  • What is this interface for?
  • Since only Length and Capacity are exposed, how can a callee access the actual data?
like image 951
bavaza Avatar asked Mar 17 '23 23:03

bavaza


2 Answers

From MSDN:

When you implement the IBuffer interface, you must implement the IBufferByteAccess interface, which is a COM interface for accessing the buffer directly

So to answer your second question the accessing the data is taken care for by the implemntation of the IBufferByteAccess interface.

As to use cases MSDN says:

The IBuffer interface is used by the IInputStream and IOutputStream interfaces.

You can find more here

like image 58
PiotrWolkowski Avatar answered Mar 23 '23 22:03

PiotrWolkowski


1) The IBuffer interface is used to pass buffers around. IBuffer object represents a byte array.

The interface offers no way to access the buffer's bytes. The reason for this is that WinRT types cannot express pointers in their metadata because pointers do not map well to some languages(like JavaScript or safe C# code). The interface could offer a method to access individual bytes in the buffer, but calling a method to get each byte would hurt performance too much.

2) Internally the CLR can take an IBuffer object, query for its IBufferByteAccess interface and then query the Buffer property to get an unsafe pointer to bytes contained within the buffer. With the pointer, the bytes can be accessed directly.

internal interface IBufferByteAccess {
    unsafe Byte* Buffer { get; }
}
like image 38
Utsav Dawn Avatar answered Mar 23 '23 22:03

Utsav Dawn