Anu
Anu

Reputation: 1

Sample code to split byte array into chunks and convert and append it to the string

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..

Upvotes: 0

Views: 4287

Answers (2)

x77
x77

Reputation: 737

  • 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);

Upvotes: 1

ChaosPandion
ChaosPandion

Reputation: 78272

You could try working on a line by line basis:

using (var sr = new System.IO.StreamReader(filePath))
{
    var line = sr.ReadLine();
}

Upvotes: 0

Related Questions