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中比较重要的一块,整个体系设计得非常好,同时对于代码学习来说也是比较好的范例.同时很多面试官也比较喜欢用容器来考察面试者的基础知识,所以掌握好容器还是比较重要的.本文主要总结一 ...
随机推荐
- CA接口测试类
package com.creditharmony.adapter.testCase.ca; import org.junit.Test; import com.alibaba.druid.util. ...
- Presto集群安装配置
Presto是一个运行在多台服务器上的分布式系统. 完整安装包括一个coordinator(调度节点)和多个worker. 由客户端提交查询,从Presto命令行CLI提交到coordinator. ...
- 【svn】 SVN错误:Attempted to lock an already-locked dir
出现这个问题后使用“清理”功能,如果还不行,就直接到上一级目录,再执行“清理”,然后再“更新”. 有时候如果看到某个包里面的文件夹没有SVN的标志,直接用“Ctrl+Delete”手工删除,然后“清理 ...
- jQuery validate在没有校验通过的情况下拒绝提交
下面通过一个简单的例子说明,这个问题,可能是很多人遇到的,验证不通过的时候,依然提交了表单. HTML <form class="survey" id="surve ...
- Struts2 Interceptors
Alias Interceptor : 别名拦截器 <action name="someAction" class="com.examples.SomeAction ...
- C# .net 最大HTTP连接数
说明:本WCF程序收到请求时会对外HTTP请求. 桌面系统默认是2个,如果是SERVER,默认是10个,超过10个就会排队,当有大量向外请求时,很多对外请求就会超时.排队的HTTP请求超时仍在计时. ...
- 【Properties文件】Java使用Properties来读取配置文件
配置文件位置及内容 执行结果 程序代码 package Utils.ConfigFile; import java.io.BufferedInputStream; import java.io.B ...
- 读书笔记:应用随机过程:概率模型导论:Aloha协议问题
例4.16,Aloha协议:就本书例题所涉及的部分来说,几乎等同于CSMA.这个例题重写如下: 考察一个包含多个设备的通信系统,其中在每个时间段发送信息的设备个数是独立同分布的.......每个设备将 ...
- php时间日期处理
php日期函数: 首先想到的就是date(),time(),strtotime(),mktime() strtotime() strtotime()函数用于将英文文本字符串表示的日期转换为时间戳,为 ...
- php base64编码和urlencode
base64编码 加密 base64_encode($str); 解密 base64_decode(base64_encode($str)); urlencode和base64混合使用 functio ...