WinRT的IBuffer

ReadAsync要用到的IBuffer表示一个字节数组,接口如下:

public interface IBuffer {
UInt32 Capacity { get; } // Maximum size of the buffer (in bytes)
UInt32 Length { get; set; } // Number of bytes currently in use by the buffer
}

看起来很奇怪,因为它不能访问字节的内容,其实所有的IBuffer对象都实现了一具IBufferByteAccess接口,此接口是COM接口,但不是WinRT接口。

namespace System.Runtime.InteropServices.WindowsRuntime {
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("905a0fef-bc53-11df-8c49-001e4fc686da")]
internal interface IBufferByteAccess {
unsafe Byte* Buffer { get; }
}
}

CLR用了一些扩展方法将字节转换成IBuffer:

// Defined in System.Runtime.WindowsRuntime.dll
namespace System.Runtime.InteropServices.WindowsRuntime {
public static class WindowsRuntimeBufferExtensions {
public static IBuffer AsBuffer(this Byte[] source);
public static IBuffer GetWindowsRuntimeBuffer(this MemoryStream stream);
public static Byte[] ToArray(this IBuffer source);
public static Stream AsStream(this IBuffer source);
// Not shown: other overloads, CopyTo, GetByte, & IsSameData
}
}

例子如下:

private async void SimpleWriteAndRead(StorageFile file) {
using (IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite)) {
Byte[] bytes = new Byte[] { 1, 2, 3, 4, 5 };
UInt32 bytesWritten = await raStream.WriteAsync(bytes.AsBuffer()); // Byte[] -> IBuffer
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms)) {
sw.Write("A string in a stream");
sw.Flush(); // Required: Flushes StreamWriter's contents to underlying MemoryStream
bytesWritten =
await raStream.WriteAsync(ms.GetWindowsRuntimeBuffer()); // Stream -> IBuffer
}
} // Close the stream

using (IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.Read)) {
// NOTE: This is the most efficient way to allocate, populate, & access data:
Byte[] data = new Byte[5]; // Allocate the Byte[]
IBuffer proposedBuffer = data.AsBuffer(); // Wrap it in an object that implements IBuffer
IBuffer returnedBuffer = await raStream.ReadAsync(proposedBuffer,
proposedBuffer.Capacity, InputStreamOptions.None);
if (returnedBuffer != proposedBuffer) {
// The proposed & returned IBuffers are not the same.
// Copy the returned bytes into the original Byte[]
returnedBuffer.CopyTo(data);
} else {
// The proposed & returned IBuffers are the same.
// The returned bytes are already in the original Byte[]
}
// TODO: Put code here to access the read bytes from the data array...
data = new Byte[raStream.Size - 5]; // Allocate Byte[] for remainder
proposedBuffer = data.AsBuffer(); // Wrap it in an object that implements IBuffer
returnedBuffer = await raStream.ReadAsync(proposedBuffer,
proposedBuffer.Capacity, InputStreamOptions.None);
// We just use the returned IBuffer here
using (var sr = new StreamReader(returnedBuffer.AsStream())) {
String str = sr.ReadToEnd();
}
} // Close the stream
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值