2

我得到了一个非常大的 XML (>1GB),其CDATA内容包含一个带有 76 个字符和 CRLF 的 base64,最后一个内容用“=”填充,没有 CRLF。

获得 base64 解码的最自然方法是使用 XmlReader reader.ReadElementContentAsBase64(...) 但在 100Mb 时停止工作。(=OOM)

我在寻找一种流式传输到 Base64 转换的方法后发现了这一点。

string Original = "foo bar, this is an example";

byte[] ToBase64; string Decoded;

using ( MemoryStream ms = new MemoryStream() )  using ( CryptoStream cs = new CryptoStream( ms, new ToBase64Transform(),
                                                CryptoStreamMode.Write ) )  using ( StreamWriter st = new StreamWriter( cs ) )  {
    st.Write( Original );
    st.Flush();
    ToBase64 = ms.ToArray();  }

using ( MemoryStream ms = new MemoryStream( ToBase64 ) )  using ( CryptoStream cs = new CryptoStream( ms, new FromBase64Transform(), 
                                                    CryptoStreamMode.Read ) )  using ( StreamReader sr = new StreamReader( cs ) )      {
    Decoded = sr.ReadToEnd(); }

Console.WriteLine( Original );  Console.WriteLine( Encoding.Default.GetString( ToBase64 ) );  Console.WriteLine( Decoded ); 

此示例有一个字符串作为输入,但我需要转换此代码以处理文件并从文件中的某个位置开始/停止读取。

              CData Start                         CDataEnd
                  |                                 |
                  V                                 V

[xml..[Envelope]...[Body]base64 with 76 char + CRLF + padding "="...[/Body]...[/Envelope]

4

1 回答 1

0

这是我能创造的最好的.. int readbytes = 0;

                long bytesToRead = (CDataEnd - CDataStart);
                using (CryptoStream cryptoStremFromBase64 = new CryptoStream(context.OutStream, new FromBase64Transform(FromBase64TransformMode.IgnoreWhiteSpaces), CryptoStreamMode.Write))
                {


                    byte[] bytebuffer = new byte[bytesToRead % 10485760];
                    readbytes = context.InStream.Read(bytebuffer, 0, bytebuffer.Length);
                    cryptoStremFromBase64.Write(bytebuffer, 0, bytebuffer.Length);

                    while (context.InStream.Position < CDataEnd)
                    {
                        WriteToLog("Context.Instream.Position = " + context.InStream.Position.ToString(), context, startingTime);
                        byte[] bytebuffer2 = new byte[10485760];
                        readbytes = context.InStream.Read(bytebuffer2, 0, bytebuffer2.Length);
                        cryptoStremFromBase64.Write(bytebuffer2, 0, bytebuffer2.Length);
                    }


                    cryptoStremFromBase64.Flush();

                }

Context.InStream 是带有 base64 上下文的 SOAP。Outstream 是从 base64 转换后的主体输出

有什么办法可以改善吗?

于 2012-10-18T07:04:16.330 回答