tags:

views:

36

answers:

2

I am loading a file into a byte array and then converting it to string using the following method.

string str1 = Convert.ToBase64String(fileBytes);

It works fine with the small files, but when the files fet too bigger like 170MB, the application throws 'system.outofmemory' exception.

So to avoid the problem, I am trying to break the byte array into chunks and convert and append it to the string. I need to make sure that I read all the chunks until append each chunk at the end of the string. Need the sample code for breaking into chunks and looping through..

A: 

You could try working on a line by line basis:

using (var sr = new System.IO.StreamReader(filePath))
{
    var line = sr.ReadLine();
}
ChaosPandion
A: 
  • Use CharrArray Fixed Size to Store Result - Compute it from FileLen
  • Use Bf size Factor 6 - 6 Bytes -> 8 Chr

char[] ChArr;
string Fname = @"File Location ...";
byte[] bf = new byte[0x60000]; // 128k * 3  - 6 Bytes -> 8 Asc64 chr
int pout = 0;
int pin = 0;
using (FileStream Fs = new FileStream(Fname, FileMode.Open, FileAccess.Read))
{
    int TotalBytes = (int)Fs.Length;
    ChArr = new char[(int)(Math.Ceiling (TotalBytes / 3 )) * 4];
    while (pin < TotalBytes)
    {
        int bytesRead = Fs.Read(bf, 0, bf.Length);
        if (bytesRead <= 0) throw new Exception("Bof Found");

        int bw = Convert.ToBase64CharArray(bf, 0, bytesRead, ChArr, pout);
        pin += bytesRead;
        pout += bw;
    }
}
string s = new string(ChArr, 0, pout);
x77
Byte Array : {0} -> AA==, {0,0} -> AAA=, {0,0,0} -> AAAA
x77
Thanks a lot for the response, the code worked as it is but had to convert TotalBytes to Decimal
Anu
Yes, or Divide by 3.0 to forze double division.
x77
Also using integer division: new char[(TotalBytes+2) / 3 * 4];
x77