将一个流的内容复制到另一个流的最佳方法是什么?有标准的实用方法吗?
当前回答
如果你想要一个过程复制一个流到其他的一个,尼克张贴是好的,但它是丢失的位置重置,它应该是
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
long TempPos = input.Position;
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
input.Position = TempPos;// or you make Position = 0 to set it at the start
}
但如果它在运行时不使用过程,则应该使用内存流
Stream output = new MemoryStream();
byte[] buffer = new byte[32768]; // or you specify the size you want of your buffer
long TempPos = input.Position;
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
input.Position = TempPos;// or you make Position = 0 to set it at the start
其他回答
不幸的是,没有真正简单的解决办法。你可以尝试这样做:
Stream s1, s2;
byte[] buffer = new byte[4096];
int bytesRead = 0;
while (bytesRead = s1.Read(buffer, 0, buffer.Length) > 0) s2.Write(buffer, 0, bytesRead);
s1.Close(); s2.Close();
但问题是,如果没有什么可读取的,Stream类的不同实现可能会有不同的行为。从本地硬盘读取文件的流可能会阻塞,直到读操作从磁盘读取了足够的数据来填充缓冲区,并且只有在到达文件末尾时才返回较少的数据。另一方面,从网络读取的流可能返回更少的数据,即使有更多的数据需要接收。
在使用通用解决方案之前,一定要检查您正在使用的特定流类的文档。
从。net 4.5开始,就有了流。CopyToAsync方法
input.CopyToAsync(output);
这将返回一个Task,完成后可以继续执行,如下所示:
await input.CopyToAsync(output)
// Code from here on will be run in a continuation.
注意,根据对CopyToAsync调用的位置,后面的代码可能继续也可能不继续在调用它的同一个线程上。
调用await时捕获的SynchronizationContext将决定在哪个线程上执行延续。
此外,这个调用(这是一个可能会改变的实现细节)仍然对读写进行排序(它只是没有在I/O完成时浪费线程阻塞)。
从。net 4.0开始,就有了流。CopyTo方法
input.CopyTo(output);
适用于。net 3.5及之前版本
框架中没有任何东西可以帮助实现这一点;你必须手动复制内容,就像这样:
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, read);
}
}
注1:这个方法将允许你报告进度(x字节读取到目前为止…) 注2:为什么使用固定的缓冲区大小而不是input.Length?因为这个长度可能是不可用的!从文档中可以看出:
如果从Stream派生的类不支持查找,则调用Length、SetLength、Position和Seek会抛出NotSupportedException异常。
下面的代码可以解决这个问题,使用CopyTo将流复制到MemoryStream
Stream stream = new MemoryStream();
//任何需要输入流的函数。在我的情况下,保存PDF文件为流 document.Save(流);
MemoryStream newMs = (MemoryStream)stream;
byte[] getByte = newMs.ToArray();
//注意-请在finally块中处理流,而不是在using块中处理,因为它会抛出一个错误'访问被拒绝,因为流被关闭'
实际上,有一种更简单的方法来做流复制。但是请注意,这意味着您可以将整个文件存储在内存中。如果您正在处理数百兆字节或更大的文件,请不要轻率地尝试使用此功能。
public static void CopySmallTextStream(Stream input, Stream output)
{
using (StreamReader reader = new StreamReader(input))
using (StreamWriter writer = new StreamWriter(output))
{
writer.Write(reader.ReadToEnd());
}
}
注意:关于二进制数据和字符编码也可能存在一些问题。
如果你想要一个过程复制一个流到其他的一个,尼克张贴是好的,但它是丢失的位置重置,它应该是
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
long TempPos = input.Position;
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
input.Position = TempPos;// or you make Position = 0 to set it at the start
}
但如果它在运行时不使用过程,则应该使用内存流
Stream output = new MemoryStream();
byte[] buffer = new byte[32768]; // or you specify the size you want of your buffer
long TempPos = input.Position;
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
input.Position = TempPos;// or you make Position = 0 to set it at the start
推荐文章
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题
- 是否可以动态编译和执行c#代码片段?
- 创建自定义MSBuild任务时,如何从c#代码获取当前项目目录?
- c#和Java的主要区别是什么?
- 在c#中创建一个特定时区的DateTime
- .NET中的属性是什么?
- csproj文件中的“Service Include”是干什么用的?
- 如何使用try catch进行异常处理是最佳实践
- 替换字符串中第一次出现的模式
- .NET中字节的字面后缀?
- 如何处理AccessViolationException
- c#忽略证书错误?