写入Byte Array时出现MemoryOutOfException。 [英] MemoryOutOfException while writing in to Byte Array.

查看:53
本文介绍了写入Byte Array时出现MemoryOutOfException。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


 大家好,


        我正在开发一个wp8中的应用程序,我必须读取大文件并将它们转换为字节数组。


在读取它们时,我可以读取两个到 三个不同大小的文件成功并且在阅读大文件时我得到了记忆,请帮助我如何一次阅读所有文件。

     


 这是我的代码

 public byte [] GetBytes(string filePath)
{
尝试
{
使用(IsolatedStorageFile appIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if(appIsolatedStorage.FileExists(filePath))
{
using (IsolatedStorageFileStream fileStream = appIsolatedStorage.OpenFile(filePath,FileMode.Open,FileAccess.Read))
{
try
{
using(BinaryReader reader = new BinaryReader(fileStream))
{

long length = reader.BaseStream.Length;
int rr = Convert.ToInt32(length);
byte [] buffer = new byte [length];
int readCount = 0;
int actual = 1024 * 1024;
while(readCount< length)
{
reader.Read(buffer,0,actual);
readCount + = actual;
}
返回缓冲区;
}

}
catch(例外情况)
{
返回null;
}
}
}
其他
返回null;
}
}
catch(exception ex)
{
返回null;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;从路径我正在读取文件并将它们转换为字节并返回该字节数组。


谢谢。

解决方案

< blockquote>


 
public byte [] GetBytes(string filePath)
{
try
{
using(BinaryReader reader = new BinaryReader(fileStream))
{

long length = reader.BaseStream.Length;
int rr = Convert.ToInt32(length);
byte [] buffer = new byte [length];
int readCount = 0;
int actual = 1024 * 1024;
while(readCount< length)
{
reader.Read(buffer,0,actual);
readCount + = actual;
}
返回缓冲区;
}



这可能与也可能与你得到的例外,但你的阅读循环看起来不正确。  您经常从文件的开头读取并尝试读取每次读取1024 * 1024字节。  尝试修改循环以查看
更像是这样:

 using(BinaryReader reader = new BinaryReader(fileStream))
{
long length = reader.BaseStream.Length;
int rr = Convert.ToInt32(length);
byte [] buffer = new byte [length];
int readCount = 0;
int actual = 1024 * 1024;
int maxReadLen = Math.Min(1024 * 1024,length);
while(readCount< length)
{
actual = reader.Read(buffer,readCount,maxReadLen);
readCount + = actual;
maxReadLen = Math.Min(1024 * 1024,length - readCount);
}
返回缓冲区;
}

如果单个读取没有,这将阻止您的代码尝试从输入流的末尾读取获取所有字节或文件不是1024 * 1024的完全倍数


 Hi All,

         I am developing an application in wp8 , which i have to read large files and converting them into byte array.

while reading them i can read two to  three files of different sizes successfully and while reading large files i am getting memoryoutofexception ,please help me how can i read all files at a time.
     

 here is my code

 public byte[] GetBytes(string filePath)
        {
            try
            {
                using (IsolatedStorageFile appIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (appIsolatedStorage.FileExists(filePath))
                    {
                        using (IsolatedStorageFileStream fileStream = appIsolatedStorage.OpenFile(filePath, FileMode.Open, FileAccess.Read))
                        {
                            try
                            {
                                using (BinaryReader reader = new BinaryReader(fileStream))
                                {
                                    
                                    long length = reader.BaseStream.Length;
                                    int rr = Convert.ToInt32(length);
                                    byte[] buffer = new byte[length];
                                    int readCount = 0;
                                    int actual = 1024 * 1024;
                                    while (readCount < length)
                                    {
                                        reader.Read(buffer, 0, actual);
                                        readCount += actual;
                                    }
                                    return buffer;
                                }

                            }
                            catch (Exception ex)
                            {
                                return null;
                            }
                        }
                    }
                    else
                        return null;
                }
            }
            catch (Exception ex)
            {
                return null;

            from path i am reading file and converting them to byte and returning that byte array.

Thank you.

解决方案


public byte[] GetBytes(string filePath) { try { using (BinaryReader reader = new BinaryReader(fileStream)) { long length = reader.BaseStream.Length; int rr = Convert.ToInt32(length); byte[] buffer = new byte[length]; int readCount = 0; int actual = 1024 * 1024; while (readCount < length) { reader.Read(buffer, 0, actual); readCount += actual; } return buffer; }

This may or may not be related to the exception you are getting, but your read loop does not look right.  You are constantly reading from the beginning of the file and attempting to read 1024*1024 bytes per read.  Try modifying the loop to look more like this:

using (BinaryReader reader = new BinaryReader(fileStream))
{
  long length = reader.BaseStream.Length;
  int rr = Convert.ToInt32(length);
  byte[] buffer = new byte[length];
  int readCount = 0;
  int actual = 1024 * 1024;
  int maxReadLen = Math.Min(1024 * 1024, length);
  while (readCount < length)
  {
    actual = reader.Read(buffer, readCount, maxReadLen);
    readCount += actual;
    maxReadLen = Math.Min(1024 * 1024, length - readCount);
  }
  return buffer;
}

This will prevent your code from attempting to read from beyond the end of the input stream if a single read did not get all bytes or the file was not an exact multiple of 1024*1024


这篇关于写入Byte Array时出现MemoryOutOfException。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆