WinRT IO-related sorting and winrtio sorting

Source: Internet
Author: User

WinRT IO-related sorting and winrtio sorting

Generally, UWP development depends on. Net for UWP, but sometimes you still need to call the WinRT API. Especially in the I/O section, WinRT has interfaces that are similar to. Net but not the same. Here we will sort out some frequently used areas.

 

WinRT IOStream

Similar to the. Net Stream system, WinRT also has a system IO Stream interface. Similar to c ++, the stream in WinRT is divided into input and output:

interface IInputStreaminterface IOutputStreaminterface IRandomAccessStream : IInputStream, IOutputStream

The IInputStream and IOutputStream interfaces provide basic asynchronous bytes read/write operations. However, because they are too abstract, they can be encapsulated by DataReader and DataWriter.

IRandomAccessStream provides Random Access to the stream and enables the input or output stream at a specific offset.

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

For the implementation of the preceding interfaces, WinRT provides the InMemoryRandomAccessStream class for reading and writing high-speed memory streams.

 

IO operation class

WinRT provides DataReader and DataWriter classes to operate input and output streams.

The static RandomAccessStream class (not the implementation of the above interface) is used to conveniently copy data from the input stream to the output stream.

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

 

WinRT provides two modes for common file operations:

  • One is stream-based StorageFile.

    This class implements the IInputStreamReference interface, which is used to open the input stream from the file;

    IAsyncOperation<IInputStream> StorageFile.OpenSequentialReadAsync();

    The IStorageFile interface is also implemented to enable random read streams to write files.

    IAsyncOperation<IRandomAccessStream> StorageFile.OpenAsync(FileAccessMode accessMode);
  • The other is to call the static FileIO class. This class provides multiple methods for reading and writing binary or text, and accepts an IStorageFile as the parameter. However, it is more convenient and flexible than stream operations.

 

The IO of WinRT is similar to that of. Net as follows:

WinRT . Net
DataReader/DataWriter BinaryReader/BinaryWriter + arbitrary stream
FileIO StreamReader/StreamWriter (also TextRWer) + file stream

 

 

 

Of course, this is not so absolute. Neither of the two methods is a single operation byte or text, and they also involve each other's fields.

 

In addition to files, WinRT also provides network I/O, such as TCP StreamSocket and UDP DatagramSocket. These classes also provide the corresponding attributes for retrieving input and output streams.

 

Conversion between WinRT and other Runtime

Conversion between WinRT IO stream and. Net stream (some methods are omitted ):

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);    }}

 

Conversion from WinRT StorageFile to. Net stream (some methods are omitted ):

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

 

In addition, IBuffer, which is often encountered in WinRT IO operations, also has the corresponding conversion method and. Net class (some methods are omitted ):

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);    }}

 

In addition, for C ++/CX, you can use another method to obtain IBuffer data through the IBufferByteAccess interface:

#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);

This prevents data copying.

 

P.S. Currently, WinRT may still have fewer documents, which may be difficult to use. The related sorting will be supplemented.

In addition, for the document source, as on the IBufferByteAccess page, there are some references for C ++ on MSDN. Some of the references are the same as those for WinRT, and more comments are often provided for reference, for example:

Https://msdn.microsoft.com/en-us/library/br205850 (v = vs.85). aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.