- public override int GetBytes(String s, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- if(s == null)
- {
- throw new ArgumentNullException("s");
- }
- if(bytes == null)
- {
- throw new ArgumentNullException("bytes");
- }
- if(charIndex < 0 || charIndex > s.Length)
- {
- throw new ArgumentOutOfRangeException
- ("charIndex", _("ArgRange_StringIndex"));
- }
- if(charCount < 0 || charCount > (s.Length - charIndex))
- {
- throw new ArgumentOutOfRangeException
- ("charCount", _("ArgRange_StringRange"));
- }
- if(byteIndex < 0 || byteIndex > bytes.Length)
- {
- throw new ArgumentOutOfRangeException
- ("byteIndex", _("ArgRange_Array"));
- }
- if((bytes.Length - byteIndex) < charCount)
- {
- throw new ArgumentException(_("Arg_InsufficientSpace"));
- }
- int count = charCount;
- char ch;
- while(count-- > 0)
- {
- ch = s[charIndex++];
- if(ch < (char)0x80)
- {
- bytes[byteIndex++] = (byte)ch;
- }
- else
- {
- bytes[byteIndex++] = (byte)'?';
- }
- }
- return charCount;
+ public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
+ {
+#if NET_2_0
+// I know this #if is ugly, but I think it is the simplest switch.
+ EncoderFallbackBuffer buffer = null;
+ char [] fallback_chars = null;
+ return GetBytes (s, charIndex, charCount, bytes, byteIndex,
+ ref buffer, ref fallback_chars);
+ }
+
+ int GetBytes (String s, int charIndex, int charCount,
+ byte[] bytes, int byteIndex,
+ ref EncoderFallbackBuffer buffer,
+ ref char [] fallback_chars)
+ {
+#endif
+ if (s == null) {
+ throw new ArgumentNullException ("s");
+ }
+ if (bytes == null) {
+ throw new ArgumentNullException ("bytes");
+ }
+ if (charIndex < 0 || charIndex > s.Length) {
+ throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
+ }
+ if (charCount < 0 || charCount > (s.Length - charIndex)) {
+ throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
+ }
+ if (byteIndex < 0 || byteIndex > bytes.Length) {
+ throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
+ }
+ if ((bytes.Length - byteIndex) < charCount) {
+ throw new ArgumentException (_("Arg_InsufficientSpace"));
+ }
+ int count = charCount;
+ char ch;
+ while (count-- > 0) {
+ ch = s [charIndex++];
+ if (ch < (char)0x80) {
+ bytes [byteIndex++] = (byte)ch;
+ } else {
+#if NET_2_0
+ if (buffer == null)
+ buffer = EncoderFallback.CreateFallbackBuffer ();
+ if (Char.IsSurrogate (ch) && count > 1 &&
+ Char.IsSurrogate (s [charIndex]))
+ buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
+ else
+ buffer.Fallback (ch, charIndex - 1);
+ if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
+ fallback_chars = new char [buffer.Remaining];
+ for (int i = 0; i < fallback_chars.Length; i++)
+ fallback_chars [i] = buffer.GetNextChar ();
+ byteIndex += GetBytes (fallback_chars, 0,
+ fallback_chars.Length, bytes, byteIndex,
+ ref buffer, ref fallback_chars);
+#else
+ bytes [byteIndex++] = (byte)'?';
+#endif