[英]Can't read FileStream into byte[] correctly
我有一些C#代码作为TF(true,"C:\\input.txt","C:\\noexistsyet.file")
调用TF(true,"C:\\input.txt","C:\\noexistsyet.file")
,但是当我运行它时,它在FileStream.Read()
上打破以读取最后一个块将文件放入缓冲区,获取index-out-of-bounds ArgumentException
。
对我来说,代码似乎是逻辑的,没有溢出尝试写入缓冲区。 我以为我用rdlen
和_chunk
设置了所有这些,但也许我看错了。 有帮助吗?
我的错误: ArgumentException was unhandled: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
public static bool TF(bool tf, string filepath, string output)
{
long _chunk = 16 * 1024; //buffer count
long total_size = 0
long rdlen = 0;
long wrlen = 0;
long full_chunks = 0;
long end_remain_buf_len = 0;
FileInfo fi = new FileInfo(filepath);
total_size = fi.Length;
full_chunks = total_size / _chunk;
end_remain_buf_len = total_size % _chunk;
fi = null;
FileStream fs = new FileStream(filepath, FileMode.Open);
FileStream fw = new FileStream(output, FileMode.Create);
for (long chunk_pass = 0; chunk_pass < full_chunks; chunk_pass++)
{
int chunk = (int)_chunk * ((tf) ? (1 / 3) : 3); //buffer count for xbuffer
byte[] buffer = new byte[_chunk];
byte[] xbuffer = new byte[(buffer.Length * ((tf) ? (1 / 3) : 3))];
//Read chunk of file into buffer
fs.Read(buffer, (int)rdlen, (int)_chunk); //ERROR occurs here
//xbuffer = do stuff to make it *3 longer or *(1/3) shorter;
//Write xbuffer into chunk of completed file
fw.Write(xbuffer, (int)wrlen, chunk);
//Keep track of location in file, for index/offset
rdlen += _chunk;
wrlen += chunk;
}
if (end_remain_buf_len > 0)
{
byte[] buffer = new byte[end_remain_buf_len];
byte[] xbuffer = new byte[(buffer.Length * ((tf) ? (1 / 3) : 3))];
fs.Read(buffer, (int)rdlen, (int)end_remain_buf_len); //error here too
//xbuffer = do stuff to make it *3 longer or *(1/3) shorter;
fw.Write(xbuffer, (int)wrlen, (int)end_remain_buf_len * ((tf) ? (1 / 3) : 3));
rdlen += end_remain_buf_len;
wrlen += chunk;
}
//Close opened files
fs.Close();
fw.Close();
return false; //no functionality yet lol
}
Stream
( FileStream
的基类Read()
的Read()
方法返回一个int
指示读取的字节数;如果没有更多的字节可读取,则返回0,因此您甚至无需事先知道文件大小:
public static void CopyFileChunked(int chunkSize, string filepath, string output)
{
byte[] chunk = new byte[chunkSize];
using (FileStream reader = new FileStream(filepath, FileMode.Open))
using (FileStream writer = new FileStream(output, FileMode.Create))
{
int bytes;
while ((bytes = reader.Read(chunk , 0, chunkSize)) > 0)
{
writer.Write(chunk, 0, bytes);
}
}
}
或者甚至File.Copy()
可以做到这一点,如果你可以让框架决定块大小。
我认为它在这条线上失败了:
fw.Write(xbuffer, (int)wrlen, chunk);
你宣布xbuffer为
byte[] xbuffer = new byte[(buffer.Length * ((tf) ? (1 / 3) : 3))];
因为1 / 3
是整数除法,所以它返回0.并且您声明xbuffer
的大小为0,因此出现错误。您可以通过将操作数之一转换为浮点类型或使用文字来修复它,但是仍然需要将结果转换为整数。
byte[] xbuffer = new byte[(int)(buffer.Length * ((tf) ? (1m / 3) : 3))];
chunk
声明中也存在相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.