惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
V
Visual Studio Blog
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
IT之家
IT之家
博客园_首页
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - Franky
雷峰网
雷峰网
罗磊的独立博客
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
T
Tailwind CSS Blog
B
Blog RSS Feed
H
Help Net Security
T
The Blog of Author Tim Ferriss
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
C
CERT Recently Published Vulnerability Notes
博客园 - 三生石上(FineUI控件)
P
Palo Alto Networks Blog
I
Intezer
G
GRAHAM CLULEY
Engineering at Meta
Engineering at Meta
S
Securelist
J
Java Code Geeks
V
V2EX
Y
Y Combinator Blog
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Scott Helme
Scott Helme
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic

博客园 - my favorite

【转】QT样式表 (QStyleSheet) 【转】Qt事件循环与线程 二 [转]Qt 智能指针学习 【转】Android Web Server 【转】Android Http Server Java Annotation 注释语法 android中json的序列化与反序列化 JavaScript定义类的几种方式 js获取浏览器高度和宽度值(多浏览器) Setting up SSL made easy… Better, Faster, Easier SSL testing for ASP.NET MVC & WebForms ASP.NET MVC 3 Internationalization Windows 8 学习笔记(十四)--.map文件与.kml文件的解析 Windows 8学习笔记(十二)--集合控件 Windows 8 学习笔记(十三)--生命周期 Windows 8学习笔记(十一)---图片的显示与保存 Windows 8 Metro App学习笔记(九)—磁砖 Windows 8学习笔记(十)----Notification Windows 8学习笔记(七)--Input输入设备
Windows 8 学习笔记(八)--各种流之间的转换
my favorite · 2012-07-03 · via 博客园 - my favorite

今天在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;

        }

应该还有些其它的场景,目前就想到这些,绕晕了都,恩恩…

Trackback:

http://www.cnblogs.com/jing870812/archive/2012/04/12/2444870.html