开发者

Cast filestream length to int

I have a question about the safety of a cast from long to int. I fear that the method I wrote might fail at this cast. Can you please take a look at the code below and tell me if it is possible to write something that would avoid a possible fail?

Thank you in advance.

    public static string ReadDecrypted(string fileFullPath)
    {
        string result = string.Empty;

        using (FileStream fs = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
        {
            int fsLength = (int)fs.Length;
            byte[] decrypted;
            byte[] read = new byte[fsLength];
            if (fs.CanRead)
            {
                fs.Read(read, 0, fsLength);
                decrypted = ProtectedData.Unprotect(read, CreateEntropy(), DataProtecti开发者_如何学PythononScope.CurrentUser);
                result = Utils.AppDefaultEncoding.GetString(decrypted, 0, decrypted.Length);
            }
        }
        return result;
    }


the short answer is: yes, this way you will have problems with any file with a length >= 2 GB!

if you don't expect any files that big then you can insert directly at the start of the using block:

if (((int)fs.Length) != fs.Length) throw new Exception ("too big");

otherwise you should NOT cast to int, but change byte[] read = new byte[fsLength]; to byte[] read = new byte[fs.Length]; and use a loop to read the file content in "chunks" of max. 2 GB per chunk.

Another alternative (available in .NET4) is to use MemoryMappedFile (see http://msdn.microsoft.com/en-us/library/dd997372.aspx) - this way you don't need to call Read at all :-)


Well, int is 32-bit and long is 64-bit, so there's always the possibility of losing some data with the cast if you're opening up 2GB files; on the other hand, that allocation of a byte array of fsLength would seem to indicate you're not expecting files that big. Put a check in to make sure that fs.Length isn't greater than 2,147,483,647, and you should be fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜