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
}