Windows 8 学习笔记(八)--各种流之间的转换

今天在Stream与IrandomAccessStream之间转换时,感觉很纠结,所以干脆先把想到的一些场景都罗列了一下,希望下次再用的时候就不用这么毛手毛脚的了。。。

Stream IRandomAccessStream

方法一:
byte[] bytes = StreamToBytes(stream);
InMemoryRandomAccessStream memoryStream = new InMemoryRandomAccessStream();
DataWriter datawriter = new DataWriter(memoryStream.GetOutputStreamAt(0));
datawriter.WriteBytes(bytes);
await datawriter.StoreAsync();
方法二:
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
await RandomAccessStream.CopyAsync(stream.AsInputStream(), outputStream);
 

IRandomAccessStream Stream

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

IbufferStream

Stream stream = WindowsRuntimeBufferExtensions.AsStream(buffer);
 

StreamIbuffer

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

Ibufferbyte[]

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

 

Byte[]Ibuffer

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

 

IbufferIrandomAccessStream

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

 

IrandomAccessStreamIbuffer

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

 

IRandomAccessStreamFileInputStream

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

 

IRandomAccessStreamFileOutputStream

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

 

Streambyte[]

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

}

}

 

ByteStream

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

 

StreamMemoryStream

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;
        }
应该还有些其它的场景,目前就想到这些,绕晕了都,恩恩…

posted on 2012-04-12 22:18  ShinyTang  阅读(6545)  评论(10编辑  收藏  举报

导航

作者:LucyTangLucyTang's Blog on 博客园
出处:http://www.cnblogs.com/jing870812/

本作品由LucyTang创作,采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。 任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问或者授权方面的协商,请给我留言