ASCIIEncoding.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // System.Text.ASCIIEncoding.cs
  3. //
  4. // Author:
  5. // Sean MacIsaac ([email protected])
  6. // Dietmar Maurer ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. namespace System.Text {
  11. public class ASCIIEncoding : Encoding
  12. {
  13. public ASCIIEncoding () : base ()
  14. {
  15. encoding_name = "US-ASCII";
  16. body_name = "us-ascii";
  17. header_name = "us-ascii";
  18. web_name = "us-ascii";
  19. is_browser_display = false;
  20. is_browser_save = false;
  21. is_mail_news_display = true;
  22. is_mail_news_save = true;
  23. }
  24. public override int GetByteCount (string chars)
  25. {
  26. if (chars == null)
  27. throw new ArgumentNullException ();
  28. return chars.Length;
  29. }
  30. public override int GetByteCount (char[] chars)
  31. {
  32. if (chars == null)
  33. throw new ArgumentNullException ();
  34. return chars.Length;
  35. }
  36. public override int GetByteCount (char[] chars, int index, int count)
  37. {
  38. if (chars == null)
  39. throw new ArgumentNullException ();
  40. if ((index < 0) || (count <= 0) || ((index + count) > chars.Length))
  41. throw new ArgumentOutOfRangeException ();
  42. return count;
  43. }
  44. public override int GetBytes (char[] chars, int charIndex, int charCount,
  45. byte[] bytes, int byteIndex)
  46. {
  47. if ((bytes == null) || (chars == null))
  48. throw new ArgumentNullException ();
  49. if ((byteIndex < 0) || (charIndex < 0) || (charCount < 0) ||
  50. ((charIndex + charCount) > chars.Length) ||
  51. (byteIndex >= bytes.Length))
  52. throw new ArgumentOutOfRangeException ();
  53. if ((bytes.Length - byteIndex) < charCount)
  54. throw new ArgumentException ();
  55. for (int i = 0; i < charCount; i++)
  56. if (chars[charIndex+i] > 0x7f)
  57. bytes[byteIndex+i] = (byte) '?';
  58. else
  59. bytes[byteIndex+i] = (byte) chars[charIndex+i];
  60. return charCount;
  61. }
  62. public override int GetBytes (string chars, int charIndex, int charCount,
  63. byte[] bytes, int byteIndex)
  64. {
  65. return GetBytes (chars.ToCharArray (), charIndex, charCount,
  66. bytes, byteIndex);
  67. }
  68. public override int GetCharCount (byte[] bytes)
  69. {
  70. if (bytes == null)
  71. throw new ArgumentNullException ();
  72. return bytes.Length;
  73. }
  74. public override int GetCharCount (byte[] bytes, int index, int count)
  75. {
  76. if (bytes == null)
  77. throw new ArgumentNullException ();
  78. if ((index < 0) || (count <= 0) || ((index + count) > bytes.Length))
  79. throw new ArgumentOutOfRangeException ();
  80. return count;
  81. }
  82. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  83. char[] chars, int charIndex)
  84. {
  85. if ((bytes == null) || (chars == null))
  86. throw new ArgumentNullException ();
  87. if ((byteIndex < 0) || (charIndex < 0) || (byteCount < 0) ||
  88. ((byteIndex + byteCount) > bytes.Length) ||
  89. (charIndex >= chars.Length))
  90. throw new ArgumentOutOfRangeException ();
  91. if ((chars.Length - charIndex) < byteCount)
  92. throw new ArgumentException ();
  93. for (int i = 0; i < byteCount; i++)
  94. if (bytes[byteIndex+i] > 0x7f)
  95. chars[charIndex+i] = '?';
  96. else
  97. chars[charIndex+i] = (char) bytes[byteIndex+i];
  98. return byteCount;
  99. }
  100. public override int GetMaxByteCount (int charCount)
  101. {
  102. if (charCount < 0)
  103. throw new ArgumentOutOfRangeException ();
  104. return charCount;
  105. }
  106. public override int GetMaxCharCount (int byteCount)
  107. {
  108. if (byteCount < 0)
  109. throw new ArgumentOutOfRangeException ();
  110. return byteCount;
  111. }
  112. public override string GetString (byte[] bytes)
  113. {
  114. if (bytes == null)
  115. throw new ArgumentNullException ();
  116. return new String (GetChars (bytes, 0, bytes.Length));
  117. }
  118. public override string GetString (byte[] bytes, int byteIndex, int byteCount)
  119. {
  120. if (bytes == null)
  121. throw new ArgumentNullException ();
  122. if ((byteIndex < 0) || (byteCount <= 0) ||
  123. ((byteIndex + byteCount) >= bytes.Length))
  124. throw new ArgumentOutOfRangeException ();
  125. return new String (GetChars (bytes, byteIndex, byteCount));
  126. }
  127. }
  128. }