MemoryStream 的GetBuffer() 和 ToArray()的区别
GetBuffer():
Note that the buffer contains allocated bytes which might be unused. For example, if the string "test" is written into the MemoryStream object, the length of the buffer returned from GetBuffer is 256, not 4, with 252 bytes unused. To obtain only the data in the buffer, use the ToArray method; however, ToArray creates a copy of the data in memory.
The buffer can also be null.
This method works when the memory stream is closed.
微软官方的解释:请注意,该缓冲区包含这可能是未分配的字节。例如,如果将字符串“测试”被写入MemoryStream对象,该缓冲区的长度从的GetBuffer返回的是256,而不是4,具有252个字节未使用。为了获得只在缓冲区中的数据,使用ToArray方法;然而,ToArray在内存中创建了数据的副本。
ToArray() :
This method omits unused bytes in MemoryStream from the array. To get the entire buffer, use the GetBuffer method.
This method returns a copy of the contents of the MemoryStream as a byte array.
微软官方的解释:此方法返回内存流内容副本作为一个字节数组。(真实数据)
MemoryStream 的GetBuffer() 和 ToArray()的区别的更多相关文章
- GetBuffer 与ToArray区别,解决问题场景
GetBuffer 是把 stream 中的 buffer 的引用传递出来, buffer 的大小是由 stream的 Capacity来决定的. 因为只是地址的引用传递,所以 GetBuffer() ...
- MemoryStream类
转自:http://www.cnblogs.com/kissdodog/archive/2013/01/20/2868864.html MemoryStream 是一个特例,MemoryStream中 ...
- c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换
字节数组byte[]与图片image之间的转化 字节数组转换成图片 public static Image byte2img(byte[] buffer) { MemoryStream ms = ne ...
- C#中MemoryStream类的介绍
MemoryStream位于System.IO命名空间,为系统内存提供流式的读写操作.常作为其他流数据交换时的中间对象操作. 1.MemoryStream类封装一个字节数组,在构造实例时可以使用一个字 ...
- Loading Xps from MemoryStream
A common way of loading XpsDocument is to load it from file: XpsDocument document = new XpsDocument( ...
- MemoryStream转string
MemoryStream rtfTx = new MemoryStream(); var bs = rtfTx.ToArray(); string s = Encoding.UTF8.GetStrin ...
- C# Bitmap类型与Byte[]类型相互转化
Bitmap => byte[] Bitmap b = new Bitmap( "test.bmp "); MemoryStream ms = new Memory ...
- 2018.8.14-C#复习笔记总
using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...
- 容器--Collection和AbstractCollection
一.前言 容器是JAVA中比较重要的一块,整个体系设计得非常好,同时对于代码学习来说也是比较好的范例.同时很多面试官也比较喜欢用容器来考察面试者的基础知识,所以掌握好容器还是比较重要的.本文主要总结一 ...
随机推荐
- asp.net MVC ViewData详解
转自:http://www.cnblogs.com/gaopin/archive/2012/11/13/2767515.html 控制器向视图中传值ViewData详解 1.将一个字符串传值到视图中 ...
- 从MySQL到Redis 提升数据迁移的效率
场景是从MySQL中将数据导入到Redis的Hash结构中.当然,最直接的做法就是遍历MySQL数据,一条一条写入到Redis中.这样可能没什么错,但是速度会非常慢.而如果能够使MySQL的查询输出数 ...
- java.io.IOException: ORA-22920: 未锁定含有 LOB 值的行
究其原因是因为没有锁定要更新的行记录.将 mysql="select filebody from filelist where filename=?"中的SQL语句加上 ...
- IOS开发—数据库的使用
1.首先封装方法(建一个DataBase类封装) 2.实现DataBase类的方法 3.第一步先建立一个表 4.进行插入.删除.更新 插入的sql语句:@"insert into user ...
- 【linux】/etc/passwd文件
/etc/passwd文件内容格式 /etc/passwd是保存用户信息的文件. 格式:用户名: 密码 : uid : gid :用户描述:主目录:登陆shell 举个例子: root:x:0:0: ...
- 51nod 1392 装盒子
有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且一个盒子里最多只能直接装入另外一个盒子 (但是可以不断嵌套),例如1 * 1 可 ...
- C# 如何将字符串形式的” \\u1234 “ 为 “ \u1234” 的unicode编码解码为中文
using System.Text.RegularExpressions; decodedStr = Regex.Unescape(escapeUnicodeStr);
- 深入分析Volatile的实现原理(转)
引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共享变量的“可见性”.可见性的意思是当 ...
- @property and retain, assign, copy, nonatomic解析
nonatomic vs. atomic - "atomic" is the default. Always use "nonatomic". I don't ...
- Well, let's start everything from the very beginning.
帝都的霾仿佛亘古不变,不知觉2015年竟已快到尾声,而我在IBM也已呆了4个月.回顾过往多遇贵人,所获颇丰.最幸运的还是自己不忘初心,仍在不断成长.继续学习. 过去的几个月一直用WordPress,搭 ...