您的位置:首页 > 其它

WinRT IO相关整理

2016-01-17 16:48 176 查看
虽然一般UWP开发还是依赖.Net for UWP,但有时还是需要调用WinRT API。特别是在IO部分,WinRT有着和.Net似曾相识但又不尽相同的接口。在此对经常用到的一些地方进行一下整理。

WinRT IO

Stream

和.Net的Stream体系类似,WinRT中也存在一系统的IO流接口。和c++类似,在WinRT中流被分为input和output:

interface IInputStream
interface IOutputStream
interface IRandomAccessStream : IInputStream, IOutputStream


其中IInputStreamIOutputStream接口提供了基本的异步读写字节操作,不过由于太过抽象,可以加以DataReaderDataWriter包装使用。

IRandomAccessStream则提供了随机访问流,以及在特定偏移处打开input或output流的功能。

IInputStream IRandomAccessStream.GetInputStreamAt(ulong position);
IOutputStream IRandomAccessStream.GetOutputStreamAt(ulong position);
void IRandomAccessStream.Seek(ulong position);


对于以上接口的实现,WinRT提供了InMemoryRandomAccessStream类,用以进行高速内存流的读写。

IO操作类

WinRT提供了DataReaderDataWriter类以对输入、输出流进行操作。

静态的RandomAccessStream类(并非以上接口的实现),用于进行方便的从输入流到输出流的数据拷贝。

static IAsyncOperationWithProgress<UInt64, UInt64> RandomAccessStream.CopyAsync(
IInputStream source, IOutputStream destination, ulong bytesToCopy
);


另外对于常见的文件操作,WinRT提供了两种模式:

一种是基于流来使用StorageFile
该类实现了IInputStreamReference接口,用于从文件打开输入流;

IAsyncOperation<IInputStream> StorageFile.OpenSequentialReadAsync();


还实现了IStorageFile接口,支持打开随机读取流,以对文件进行写入。

IAsyncOperation<IRandomAccessStream> StorageFile.OpenAsync(FileAccessMode accessMode);


另一种是调用静态FileIO类。该类提供多种读写二进制或文本的方法,都接受一个IStorageFile作为参数。不过较流操作来说方便有余,灵活略欠。

WinRT的IO,和.Net的对比,有如下类似:

WinRT.Net
DataReader/DataWriterBinaryReader/BinaryWriter + 任意流
FileIOStreamReader/StreamWriter(亦是TextRWer) + 文件流
当然也并非这么绝对,两种方法都不是单一的操作字节或文本,也都涉及彼此的领域。

除了文件之外,WinRT也提供了网络IO,如TCP StreamSocket,UDP DatagramSocket。这些类也都提供了相应的获取输入、输出流的属性。

WinRT与其他运行时的转换

WinRT IO流与.Net流的转换(部分方法省略):

namespace System.IO
{
public static class WindowsRuntimeStreamExtensions
{
public static IInputStream AsInputStream(this Stream stream);
public static IOutputStream AsOutputStream(this Stream stream);
public static Stream AsStream(this IRandomAccessStream windowsRuntimeStream);
public static Stream AsStreamForRead(this IInputStream windowsRuntimeStream);
public static Stream AsStreamForWrite(this IOutputStream windowsRuntimeStream);
}
}


WinRT StorageFile到.Net流的转换(部分方法省略):

namespace System.IO
{
public static class WindowsRuntimeStorageExtensions
{
public static Task<Stream> OpenStreamForReadAsync(this IStorageFile windowsRuntimeFile);
public static Task<Stream> OpenStreamForWriteAsync(this IStorageFile windowsRuntimeFile);
}
}


另外在WinRT IO操作中经常会遇到的IBuffer,也有对应的方式和.Net类互转(部分方法省略):

namespace System.Runtime.InteropServices.WindowsRuntime
{
public sealed class WindowsRuntimeBuffer
{
public static IBuffer Create(byte[] data, int offset, int length, int capacity);
public static IBuffer Create(int capacity);
}

public static class WindowsRuntimeBufferExtensions
{
public static IBuffer AsBuffer(this byte[] source);
public static Stream AsStream(this IBuffer source);
public static void CopyTo(this byte[] source, int sourceIndex, IBuffer destination, uint destinationIndex, int count);
public static void CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count);
public static void CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count);
public static byte[] ToArray(this IBuffer source);
}
}


另外对于C++/CX,获取IBuffer数据可以采取另一种方法,通过IBufferByteAccess接口:

#include <robuffer.h>
using namespace Windows::Storage::Streams;

IBuffer ^buffer;
ComPtr<IInspectable> inspectable(reinterpret_cast<IInspectable *>(buffer));
ComPtr<IBufferByteAccess> bufferByteAccess;
inspectable.As(&bufferByteAccess);

byte *bytes;
bufferByteAccess->Buffer(&bytes);


可以避免数据的拷贝。

P.S.现在WinRT可能还存在文档比较少的问题,用起来会比较辛苦。相关的整理会慢慢补充。

另外关于文档出处,正如IBufferByteAccess的页面,MSDN上有一些关于C++的参考,其中存在部分和WinRT相同的地方,往往带有更丰富的备注,可供参考,如:

https://msdn.microsoft.com/en-us/library/br205850(v=vs.85).aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: