2 // I18N.CJK.DbcsEncoding
5 // Alan Tam Siu Lung (Tam@SiuLung.com)
13 using MonoEncoder = I18N.Common.MonoSafeEncoder;
14 using MonoEncoding = I18N.Common.MonoSafeEncoding;
20 internal abstract class DbcsEncoding : MonoEncoding
22 public DbcsEncoding (int codePage) : this (codePage, 0)
26 public DbcsEncoding (int codePage, int windowsCodePage)
27 : base (codePage, windowsCodePage)
31 internal abstract DbcsConvert GetConvert ();
33 // Get the number of bytes needed to encode a character buffer.
34 public override int GetByteCount(char[] chars, int index, int count)
37 throw new ArgumentNullException("chars");
38 if (index < 0 || index > chars.Length)
39 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
40 if (count < 0 || index + count > chars.Length)
41 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
42 byte[] buffer = new byte[count * 2];
43 return GetBytes(chars, index, count, buffer, 0);
47 // Get the bytes that result from encoding a character buffer.
48 public override int GetBytes(char[] chars, int charIndex, int charCount,
49 byte[] bytes, int byteIndex)
52 throw new ArgumentNullException("chars");
54 throw new ArgumentNullException("bytes");
55 if (charIndex < 0 || charIndex > chars.Length)
56 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
57 if (charCount < 0 || charIndex + charCount > chars.Length)
58 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_Array"));
59 if (byteIndex < 0 || byteIndex > bytes.Length)
60 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
61 return 0; // For subclasses to implement
65 // Get the number of characters needed to decode a byte buffer.
66 public override int GetCharCount(byte[] bytes, int index, int count)
69 throw new ArgumentNullException("bytes");
70 if (index < 0 || index > bytes.Length)
71 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
72 if (count < 0 || index + count > bytes.Length)
73 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
74 char[] buffer = new char[count];
75 return GetChars(bytes, index, count, buffer, 0);
78 // Get the characters that result from decoding a byte buffer.
79 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
80 char[] chars, int charIndex)
83 throw new ArgumentNullException("bytes");
85 throw new ArgumentNullException("chars");
86 if (byteIndex < 0 || byteIndex > bytes.Length)
87 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
88 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
89 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
90 if (charIndex < 0 || charIndex > chars.Length)
91 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
92 return 0; // For subclasses to implement
95 // Get the maximum number of bytes needed to encode a
96 // specified number of characters.
97 public override int GetMaxByteCount(int charCount)
100 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
101 return charCount * 2;
104 // Get the maximum number of characters needed to decode a
105 // specified number of bytes.
106 public override int GetMaxCharCount(int byteCount)
109 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
114 // Determine if this encoding can be displayed in a Web browser.
115 public override bool IsBrowserDisplay
120 // Determine if this encoding can be saved from a Web browser.
121 public override bool IsBrowserSave
126 // Determine if this encoding can be displayed in a mail/news agent.
127 public override bool IsMailNewsDisplay
132 // Determine if this encoding can be saved from a mail/news agent.
133 public override bool IsMailNewsSave
138 // Decoder that handles a rolling state.
139 internal abstract class DbcsDecoder : Decoder
141 protected DbcsConvert convert;
144 public DbcsDecoder(DbcsConvert convert)
146 this.convert = convert;
149 internal void CheckRange (byte[] bytes, int index, int count)
152 throw new ArgumentNullException("bytes");
153 if (index < 0 || index > bytes.Length)
154 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
155 if (count < 0 || count > (bytes.Length - index))
156 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
159 internal void CheckRange (byte[] bytes, int byteIndex, int byteCount,
160 char[] chars, int charIndex)
163 throw new ArgumentNullException("bytes");
165 throw new ArgumentNullException("chars");
166 if (byteIndex < 0 || byteIndex > bytes.Length)
167 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
168 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
169 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
170 if (charIndex < 0 || charIndex > chars.Length)
171 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));