WinRT的IBuffer
来源:互联网 发布:少儿编程市场研究报告 编辑:程序博客网 时间:2024/04/29 13:21
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.dllnamespace 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[] -> IBufferusing (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 MemoryStreambytesWritten =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 IBufferIBuffer 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 remainderproposedBuffer = data.AsBuffer(); // Wrap it in an object that implements IBufferreturnedBuffer = await raStream.ReadAsync(proposedBuffer,proposedBuffer.Capacity, InputStreamOptions.None);// We just use the returned IBuffer hereusing (var sr = new StreamReader(returnedBuffer.AsStream())) {String str = sr.ReadToEnd();}} // Close the stream}
0 0
- WinRT的IBuffer
- WinRT C++ byte* 转为 Ibuffer^(笔记)
- 探索 Emacs 中 Ibuffer.el 的使用方法
- WinRT的 HttpClient
- WinRT的HttpBaseProtocolFilter类
- WinRT的Socket
- WinRT的BackgroundTask
- WinRT
- emacs ibuffer
- Win8与WinRt的区别
- WinRT的存储对象模型
- WinRT的Stream对象模型
- 【Windows8开发】关于WinRT组件,WinRT dll,Win32 dll,WinRT exe组件的一些尝试
- Eclipse plugins 插件开发学习 - IScanner 和 IBuffer 封装的高级应用类
- WinRT:Win32的面向对象替代品
- WinRT:Win32的面向对象替代品
- WinRT:Win32的面向对象替代品
- 微软WinRT开发人员的一些解答
- 第三方支付或再遭重击:余额宝1年购买限额1万
- IOS 学习笔记7-NSSearchPathForDirectoriesInDomains
- linux系统字符集影响TOMCAT运行,造成页面HTML代码输出不全
- match_parent和fill_parent的区别
- java的一些学习网址。。。。。
- WinRT的IBuffer
- 播放系统声音&震动
- 大多数人不了解HTML5时代的网页设计
- Android 圆角图片
- 一致性哈希算法
- java 根据宽度等比压缩图片支持网络路径
- iOS开发之iOS7设置状态栏字体颜色
- 数据库理论基本概念之关系键
- c++ private public protected-->类的访问权限小结