我正在尝试将字节流分割成大小不断增加的块。
源流包含未知的字节数,读取开销很大。枚举器的输出应该是大小递增的字节数组,从8KB到1MB。
这非常简单,只需读取整个流,将其存储在一个数组中,并取出相关的部分。但是,由于流可能非常大,因此一次读取它是不可行的。此外,虽然性能不是主要的关注点,但保持系统负载非常低是很重要的。
在实现这一点时,我注意到保持代码简短和可维护相对困难。还有一些与流相关的问题需要牢记(例如,即使Stream.Read成功了,它也可能不会填充缓冲区)。
我没有找到任何对我的情况有帮助的现有类,也没有在网上找到类似的东西。您将如何实现这样的类?
发布于 2012-02-07 00:00:09
public IEnumerable<BufferWrapper> getBytes(Stream stream)
{
List<int> bufferSizes = new List<int>() { 8192, 65536, 220160, 1048576 };
int count = 0;
int bufferSizePostion = 0;
byte[] buffer = new byte[bufferSizes[0]];
bool done = false;
while (!done)
{
BufferWrapper nextResult = new BufferWrapper();
nextResult.bytesRead = stream.Read(buffer, 0, buffer.Length);
nextResult.buffer = buffer;
done = nextResult.bytesRead == 0;
if (!done)
{
yield return nextResult;
count++;
if (count > 10 && bufferSizePostion < bufferSizes.Count)
{
count = 0;
bufferSizePostion++;
buffer = new byte[bufferSizes[bufferSizePostion]];
}
}
}
}
public class BufferWrapper
{
public byte[] buffer { get; set; }
public int bytesRead { get; set; }
}显然,何时增加缓冲区大小以及如何选择缓冲区大小的逻辑可能会改变。
有人可能还会找到一种更好的方法来处理要发送的最后一个缓冲区,因为这不是最有效的方法。
发布于 2012-02-07 00:30:00
作为参考,我目前使用的实现已经根据@Servy的回答进行了改进
private const int InitialBlockSize = 8 * 1024;
private const int MaximumBlockSize = 1024 * 1024;
private Stream _Stream;
private int _Size = InitialBlockSize;
public byte[] Current
{
get;
private set;
}
public bool MoveNext ()
{
if (_Size < 0) {
return false;
}
var buf = new byte[_Size];
int count = 0;
while (count < _Size) {
int read = _Stream.Read (buf, count, _Size - count);
if (read == 0) {
break;
}
count += read;
}
if (count == _Size) {
Current = buf;
if (_Size <= MaximumBlockSize / 2) {
_Size *= 2;
}
}
else {
Current = new byte[count];
Array.Copy (buf, Current, count);
_Size = -1;
}
return true;
}https://stackoverflow.com/questions/9162760
复制相似问题