views:

251

answers:

1

I am transferring messages to mobile devices via a web service. The data is an xml string, which I compress using GZipStream, then encode using Base64. I am getting out-of memory exceptions in the emulator and looking to optimise the process so I have stopped passing the string around by value and removed unecessary copies of byte arrays. Now I'm wondering about the Base64 encoding. It increases the size of the message, the processing and the memory requirements. Is it strictly necessary?

Edit: Here is how I decompress:

public static byte[] ConvertMessageStringToByteArray(ref string isXml)
{
  return fDecompress(Convert.FromBase64String(isXml));
}

public static byte[] fDecompress(byte[] ivBytes)
{
  const int INT_BufferSize = 2048;

  using (MemoryStream lvMSIn = new MemoryStream(ivBytes))
  using (GZipInputStream lvZipStream = new GZipInputStream(lvMSIn, ivBytes.Length))
  using (MemoryStream lvMSOut = new MemoryStream())
  {
    byte[] lvBuffer = new byte[INT_BufferSize];
    int liSize;
    while (true)
    {
      liSize = lvZipStream.Read(lvBuffer, 0, INT_BufferSize);
      if (liSize <= 0)
        break;

      lvMSOut.Write(lvBuffer, 0, liSize);
    }

    return lvMSOut.ToArray();
  }
}
+1  A: 

gzip (which is inside the GZipStream) produces binary data - they won't fit into a 7-bit text message (SOAP is a text message) unless you do somethning like base64 encoding on them.

Perhaps the solution is to not gzip/encode (decode/ungzip) a whole buffer, but use streams for that - connect a gzipping stream to an encoding stream and read the result from the output of the latter (or connect the decoding stream to the ungzipping stream). This way you have a chance to consume less memory.

sharptooth