The Labs \ Source Viewer \ SSCLI \ System.Xml \ Base64Encoder

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Base64Encoder.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System.Text;
  16. using System.Diagnostics;
  17. namespace System.Xml
  18. {
  19.    
  20.     internal abstract class Base64Encoder
  21.     {
  22.        
  23.         byte[] leftOverBytes;
  24.         int leftOverBytesCount;
  25.         char[] charsLine;
  26.        
  27.         internal const int Base64LineSize = 76;
  28.         internal const int LineSizeInBytes = Base64LineSize / 4 * 3;
  29.        
  30.         internal Base64Encoder()
  31.         {
  32.             charsLine = new char[Base64LineSize];
  33.         }
  34.        
  35.         internal abstract void WriteChars(char[] chars, int index, int count);
  36.        
  37.         internal void Encode(byte[] buffer, int index, int count)
  38.         {
  39.             if (buffer == null) {
  40.                 throw new ArgumentNullException("buffer");
  41.             }
  42.             if (index < 0) {
  43.                 throw new ArgumentOutOfRangeException("index");
  44.             }
  45.             if (count < 0) {
  46.                 throw new ArgumentOutOfRangeException("count");
  47.             }
  48.             if (count > buffer.Length - index) {
  49.                 throw new ArgumentOutOfRangeException("count");
  50.             }
  51.            
  52.             // encode left-over buffer
  53.             if (leftOverBytesCount > 0) {
  54.                 int i = leftOverBytesCount;
  55.                 while (i < 3 && count > 0) {
  56.                     leftOverBytes[i++] = buffer[index++];
  57.                     count--;
  58.                 }
  59.                
  60.                 // the total number of buffer we have is less than 3 -> return
  61.                 if (count == 0 && i < 3) {
  62.                     leftOverBytesCount = i;
  63.                     return;
  64.                 }
  65.                
  66.                 // encode the left-over buffer and write out
  67.                 int leftOverChars = Convert.ToBase64CharArray(leftOverBytes, 0, 3, charsLine, 0);
  68.                 WriteChars(charsLine, 0, leftOverChars);
  69.             }
  70.            
  71.             // store new left-over buffer
  72.             leftOverBytesCount = count % 3;
  73.             if (leftOverBytesCount > 0) {
  74.                 count -= leftOverBytesCount;
  75.                 if (leftOverBytes == null) {
  76.                     leftOverBytes = new byte[3];
  77.                 }
  78.                 for (int i = 0; i < leftOverBytesCount; i++) {
  79.                     leftOverBytes[i] = buffer[index + count + i];
  80.                 }
  81.             }
  82.            
  83.             // encode buffer in 76 character long chunks
  84.             int endIndex = index + count;
  85.             int chunkSize = LineSizeInBytes;
  86.             while (index < endIndex) {
  87.                 if (index + chunkSize > endIndex) {
  88.                     chunkSize = endIndex - index;
  89.                 }
  90.                 int charCount = Convert.ToBase64CharArray(buffer, index, chunkSize, charsLine, 0);
  91.                 WriteChars(charsLine, 0, charCount);
  92.                
  93.                 index += chunkSize;
  94.             }
  95.         }
  96.        
  97.         internal void Flush()
  98.         {
  99.             if (leftOverBytesCount > 0) {
  100.                 int leftOverChars = Convert.ToBase64CharArray(leftOverBytes, 0, leftOverBytesCount, charsLine, 0);
  101.                 WriteChars(charsLine, 0, leftOverChars);
  102.                 leftOverBytesCount = 0;
  103.             }
  104.         }
  105.     }
  106.    
  107.     internal class XmlRawWriterBase64Encoder : Base64Encoder
  108.     {
  109.         XmlRawWriter rawWriter;
  110.        
  111.         internal XmlRawWriterBase64Encoder(XmlRawWriter rawWriter)
  112.         {
  113.             this.rawWriter = rawWriter;
  114.         }
  115.        
  116.         internal override void WriteChars(char[] chars, int index, int count)
  117.         {
  118.             rawWriter.WriteRaw(chars, index, count);
  119.         }
  120.     }
  121.    
  122.     internal class XmlTextWriterBase64Encoder : Base64Encoder
  123.     {
  124.         XmlTextEncoder xmlTextEncoder;
  125.        
  126.         internal XmlTextWriterBase64Encoder(XmlTextEncoder xmlTextEncoder)
  127.         {
  128.             this.xmlTextEncoder = xmlTextEncoder;
  129.         }
  130.        
  131.         internal override void WriteChars(char[] chars, int index, int count)
  132.         {
  133.             xmlTextEncoder.WriteRaw(chars, index, count);
  134.         }
  135.     }
  136. }

Developer Fusion