BinHexEncoderAsync.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. using System.Threading.Tasks;
  2. namespace System.Xml {
  3. internal static partial class BinHexEncoder {
  4. internal static async Task EncodeAsync( byte[] buffer, int index, int count, XmlWriter writer ) {
  5. if ( buffer == null ) {
  6. throw new ArgumentNullException( "buffer" );
  7. }
  8. if ( index < 0 ) {
  9. throw new ArgumentOutOfRangeException( "index" );
  10. }
  11. if ( count < 0 ) {
  12. throw new ArgumentOutOfRangeException( "count" );
  13. }
  14. if ( count > buffer.Length - index ) {
  15. throw new ArgumentOutOfRangeException( "count" );
  16. }
  17. char[] chars = new char[ ( count * 2 ) < CharsChunkSize ? ( count * 2 ) : CharsChunkSize ];
  18. int endIndex = index + count;
  19. while ( index < endIndex ) {
  20. int cnt = ( count < CharsChunkSize/2 ) ? count : CharsChunkSize/2;
  21. int charCount = Encode( buffer, index, cnt, chars );
  22. await writer.WriteRawAsync( chars, 0, charCount ).ConfigureAwait(false);
  23. index += cnt;
  24. count -= cnt;
  25. }
  26. }
  27. } // class
  28. } // namespace