Windows 8 learning notes (8)-conversion between various streams

Source: Internet
Author: User

Today, the conversion between stream and irandomaccessstream is very tangled, So I simply listed some of the scenarios I thought of first. I hope that I will not have to use it again next time...

StreamTransferIrandomaccessstream

Method 1: byte [] bytes = streamtobytes (Stream); inmemoryrandomaccessstream memorystream = new inmemoryrandomaccessstream (); datawriter = new datawriter (memorystream. getoutputstreamat (0); datawriter. writebytes (bytes); await datawriter. storeasync ();
Method 2: var randomaccessstream = new inmemoryrandomaccessstream (); var outputstream = randomaccessstream. getoutputstreamat (0); await randomaccessstream. copyasync (stream. asinputstream (), outputstream );
 

Irandomaccessstream TransferStream

Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));
 

IbufferTransferStream

Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);
 

StreamTransferIbuffer

MemoryStream memoryStream = new MemoryStream();             if (stream != null) {      byte[] bytes = ReadFully(stream);      if (bytes != null)      {           var binaryWriter = new BinaryWriter(memoryStream);           binaryWriter.Write(bytes);       }  }  IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);
 

IbufferTransferByte []

byte[] bytes=WindowsRuntimeBufferExtensions.ToArray(buffer,0,(int)buffer.Length);

 

Byte []TransferIbuffer

WindowsRuntimeBufferExtensions.AsBuffer(bytes,0,bytes.Length);

 

IbufferTransferIrandomaccessstream

InMemoryRandomAccessStream inStream = new InMemoryRandomAccessStream();DataWriter datawriter = new DataWriter(inStream.GetOutputStreamAt(0));datawriter.WriteBuffer(buffer,0,buffer.Length);await datawriter.StoreAsync();

 

IrandomaccessstreamTransferIbuffer

Stream stream=WindowsRuntimeStreamExtensions.AsStreamForRead(randomStream.GetInputStreamAt(0));MemoryStream memoryStream = new MemoryStream();            if (stream != null){    byte[] bytes = ReadFully(stream);    if (bytes != null)    {        var binaryWriter = new BinaryWriter(memoryStream);        binaryWriter.Write(bytes);    }} IBuffer buffer=WindowsRuntimeBufferExtensions.GetWindowsRuntimeBuffer(memoryStream,0,(int)memoryStream.Length);

 

IrandomaccessstreamTransferFileinputstream

FileInputStream inputStream=randomStream.GetInputStreamAt(0) as FileInputStream;

 

IrandomaccessstreamTransferFileoutputstream

FileOutputStream outStream= randomStream.GetOutputStreamAt(0) as FileOutputStream;

 

StreamTransferByte []

Public static byte [] convertstreamtobyte (Stream Input)

{

Byte [] buffer = new byte [16*1024];

Using (memorystream MS = new memorystream ())

{

Int read;

While (read = input. Read (buffer, 0, buffer. Length)> 0)

{

Ms. Write (buffer, 0, read );

}

Return Ms. toarray ();

}

}

 

ByteTransferStream

public Stream BytesToStream(byte[] bytes)        {            Stream stream = new MemoryStream(bytes);            return stream;        }

 

StreamTransferMemorystream

public static MemoryStream ConvertStreamToMemoryStream(Stream stream)        {             MemoryStream memoryStream = new MemoryStream();            if (stream != null)            {                byte[] buffer = ReadFully(stream);                if (buffer != null)                {                    var binaryWriter = new BinaryWriter(memoryStream);                    binaryWriter.Write(buffer);                }             }             return memoryStream;        }
There should be some other scenarios. Now I think of this, and I am confused...

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.