簡體   English   中英

無法正確讀取FileStream到byte []

[英]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
    }

StreamFileStream的基類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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM