WinRT的IBuffer

本文详细介绍了如何使用IBuffer接口及其扩展方法在C#中进行字节操作,包括读写流文件的具体实现。通过实例演示了如何高效地将字节数组转换为IBuffer对象,并在不同场景下灵活运用。
ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

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
}


您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值