- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
有什么办法可以用Stream.CopyTo只复制一定数量的字节到目标流?最佳解决方法是什么?
编辑:
我的解决方法(省略了一些代码):
internal sealed class Substream : Stream
{
private readonly Stream stream;
private readonly long origin;
private readonly long length;
private long position;
public Substream(Stream stream, long length)
{
this.stream = stream;
this.origin = stream.Position;
this.position = stream.Position;
this.length = length;
}
public override int Read(byte[] buffer, int offset, int count)
{
var n = Math.Max(Math.Min(count, origin + length - position), 0);
int bytesRead = stream.Read(buffer, offset, (int) n);
position += bytesRead;
return bytesRead;
}
}
然后复制n个字节:
var substream = new Substream(stream, n);
substream.CopyTo(stm);
最佳答案
执行copying streams并不过分复杂。如果你想让它适应只复制一定数量的字节,那么调整现有的方法应该不会太难,就像这样
public static void CopyStream(Stream input, Stream output, int bytes)
{
byte[] buffer = new byte[32768];
int read;
while (bytes > 0 &&
(read = input.Read(buffer, 0, Math.Min(buffer.Length, bytes))) > 0)
{
output.Write(buffer, 0, read);
bytes -= read;
}
}
检查 bytes > 0
可能不是绝对必要的,但不会造成任何伤害。
关于c# - 有什么方法可以使用 Stream.CopyTo 只复制一定数量的字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13021866/
我正在尝试将一个范围从一张纸复制到另一张纸(同时保留公式)。我使用 copyTo 编写了一个简单的脚本: function copyRangeAcrossSheets(source_sheet,sou
我想将文件 c:\a1\b2\c3\foo.txt 复制到 d:\a1\b2\c3\foo.txt。 D 驱动器上不存在子目录,如果我尝试直接执行 CopyTo(),则会出现 IO 异常。我一直没能找
我有以下方法,但出于某种原因,第一次调用 Copy to 似乎什么也没做?有人知道为什么吗?该方法的输入被压缩,如果需要,base64 可以提供该方法。 private byte[] GetFileC
我想知道在 C# 或 VB.NET 中如何使用 ArrayList 的 CopyTo 方法> 对象将其内容复制到其他 ArrayList(不是简单的数组)。 注意:我不是在寻找像 ArrayList
谁能解释为什么这段代码的输出只是“hello”以及这段代码的含义? ( 0, characterArray, 0, characterArray.Length ); 输出显示: The charact
我有一个 WCF 服务,它允许我以 block 的形式上传文件。我想知道的是,这段代码是否会导致上传的流在任何情况下都只能部分附加到目标流? 我的日志显示所有发送的流都是 512000 字节(这是我在
我如何使用 Stream CopyTo不覆盖现有输出文件的方法? public void MergeAndDeleteFiles(string outputFile, IEnumerable inpu
考虑以下代码: var results = searcher.FindAll(); SearchResult[] srList = new SearchResult[results.Count]; r
我快要失去理智了。在过去的一个小时里,我一直试图让 GzipStream 压缩一个字符串,但出于某种原因,它拒绝将整个字节数组写入内存流。起初我认为它与 using 语句有关,但即使在删除它们之后似乎
我正在使用 Java CIFS Client Library但面临的问题是 copyTo 函数不起作用。我有一个包含文件的文件夹。我想将这些文件读取到其他网络路径。 String path1 = "
我有下表: writing_agent_number|agent_name |individual_sub_name -------------------- --------------
我正在尝试将较小的图像复制到较大图像的中心。这是代码: src.copyTo(dest(Rect(50,50,src.cols,src.rows))); src 和 dest 都是 Mats,dest
我尝试过在这个论坛上搜索、阅读文档和谷歌搜索。我找不到问题的答案。 下面简要介绍一下我正在尝试做的事情: 从网络摄像头读取Mat对象frame(在父类OpenCVVideoWorker中声明为私有(p
我正在尝试通过 C++ 在 OpenCV 2.4 中使用蒙版,将较小的图像粘贴到较大的图像中。 在没有 mask 的情况下,我使用以下代码将小图像复制到大图像: smallImage.copyTo(l
我想使用 openCv 的 copyTo() 函数对图像应用蒙版,但蒙版不起作用。我仍然获得相同的图像。 当我在网上搜索时,我看到了一些建议,例如: -源图像和目标图像必须不同, -源图像必须具有 C
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePix
我正在比较 OpenCL UMat 和 OpenCL Mat,我运行了 cvtColor() 函数 500 次。 int OpenCL_UMat() { Mat img; UMat u
我想合并 2 个大文件但是 atm 我的代码只在复制 1 个文件后更新进度有没有更好的方法来报告进度这是我的复制代码 atm max = files.Count; MessageBox.Show(
我在将数据从 MemoryStream 复制到 ZipArchive 内的 Stream 时遇到问题。以下内容不起作用 - 它仅返回 114 个字节: GetDataAsByteArray(IData
我看到CopyToAsync有 CancellationToken论据。但我不需要CopyToAsync因为我的代码已经在后台线程中执行了。我只想拥有CopyTo重载需要 CancellationTo
我是一名优秀的程序员,十分优秀!