我有一个最大大小为 1K 的字节数组缓冲区。我想写出数组的一个子集(子集的开头总是元素 0,但我们感兴趣的长度在一个变量中)。
这里的应用是压缩。我将缓冲区传递给压缩函数。为简单起见,假设压缩将导致数据等于或小于 1K 字节。
byte[] buffer = new byte[1024];
while (true)
{
uncompressedData = GetNextUncompressedBlock();
int compressedLength = compress(buffer, uncompressedData);
// Here, compressedBuffer[0..compressedLength - 1] is what we're interested in
// There's a method now with signature Write(byte[] compressedData) that
// I won't be able to change. Short of allocating a custom sized buffer,
// and copying data into the custom sized buffer... is there any other
// technique I could use to only expose the data I want?
}
我真的很想避免在这里复制——这似乎完全没有必要,因为所需的所有数据都buffer
已经在里面了。