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让FileUpload控件支持浏览自动上传功能的解决方法
ASP.NET的FileUpload控件默认是不支持服务端的onchange事件的,此时可以用一种变通的方法来实现这一功能. 这就需要借用客户端的onchange事件,调用__doPostBack方法 ...
- 07socket编程
TCP客户/服务器模型: 从图中就可以看出基本的过程来. 回射客户/服务器: 这个是回射的图示,客户端发给服务端,服务端在发回给客户端. socket函数: 包含头文件<sys/sock ...
- 剑指offer系列40----机器人的运动范围
package com.exe8.offer; /** *[题目]地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动, * 每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标 ...
- Perl 模块 Getopt::Std 和 Getopt::Long
示例程序: getopt.pl; 1 2 3 4 5 6 7 8 #!/usr/bin/perl -w #use strict; use Getopt::Std; use vars qw($opt_a ...
- 黄聪:主机宝安装wordpress注意事项
1.web环境安装PHP使用5.4.21-nts-03版本 2.web环境安装Mysql使用5.5.45版本 3.创建好站点后,给站点的public_html目录添加IIS_xxx用户最高权限,添加N ...
- python3读取文件
#coding:utf-8 rfile = open('test.txt','r') str=[] for x in rfile: str = x.split(',') for x in str: p ...
- Standing on Shouder of Giants
Zachary_Fan 如何一步一步用DDD设计一个电商网站(二)-- 项目架构 http://www.cnblogs.com/Zachary-Fan/p/6012454.html HTTP 权威指南 ...
- py继续
这个正则里面有引号,我外面在一个引号就出问题了,应该怎么处理 用双引号
- 转-TabHost组件(一)(实现底部菜单导航)
http://www.cnblogs.com/lichenwei/p/3974009.html 什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用wind ...
- UCOS-消息邮箱(学习笔记)
任务间数据传递通过缓冲区进行,如果将缓冲区赋值给时间控制块成员:指针OSEventPtr,且任务控制块类型为OS_EVENT_TYPE_MBOS则即为消息邮箱: 一创建消息邮箱:OS_EVENT *O ...