2 // I18N.CJK.DbcsEncoding
5 // Alan Tam Siu Lung (Tam@SiuLung.com)
14 internal class DbcsEncoding : Encoding
16 internal DbcsConvert convert;
18 public DbcsEncoding(int codePage) : base(codePage) {}
20 // Get the number of bytes needed to encode a character buffer.
21 public override int GetByteCount(char[] chars, int index, int count)
24 throw new ArgumentNullException("chars");
25 if (index < 0 || index > chars.Length)
26 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
27 if (count < 0 || index + count > chars.Length)
28 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
29 byte[] buffer = new byte[count * 2];
30 return GetBytes(chars, index, count, buffer, 0);
33 // Get the bytes that result from encoding a character buffer.
34 public override int GetBytes(char[] chars, int charIndex, int charCount,
35 byte[] bytes, int byteIndex)
38 throw new ArgumentNullException("chars");
40 throw new ArgumentNullException("bytes");
41 if (charIndex < 0 || charIndex > chars.Length)
42 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
43 if (charCount < 0 || charIndex + charCount > chars.Length)
44 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_Array"));
45 if (byteIndex < 0 || byteIndex > bytes.Length)
46 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
47 return 0; // For subclasses to implement
50 // Get the number of characters needed to decode a byte buffer.
51 public override int GetCharCount(byte[] bytes, int index, int count)
54 throw new ArgumentNullException("bytes");
55 if (index < 0 || index > bytes.Length)
56 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
57 if (count < 0 || index + count > bytes.Length)
58 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
59 char[] buffer = new char[count];
60 return GetChars(bytes, index, count, buffer, 0);
63 // Get the characters that result from decoding a byte buffer.
64 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
65 char[] chars, int charIndex)
68 throw new ArgumentNullException("bytes");
70 throw new ArgumentNullException("chars");
71 if (byteIndex < 0 || byteIndex > bytes.Length)
72 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
73 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
74 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
75 if (charIndex < 0 || charIndex > chars.Length)
76 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
77 return 0; // For subclasses to implement
80 // Get the maximum number of bytes needed to encode a
81 // specified number of characters.
82 public override int GetMaxByteCount(int charCount)
85 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
89 // Get the maximum number of characters needed to decode a
90 // specified number of bytes.
91 public override int GetMaxCharCount(int byteCount)
94 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
99 // Get a decoder that handles a rolling state.
100 public override Decoder GetDecoder()
102 return new DbcsDecoder(convert);
105 // Determine if this encoding can be displayed in a Web browser.
106 public override bool IsBrowserDisplay
111 // Determine if this encoding can be saved from a Web browser.
112 public override bool IsBrowserSave
117 // Determine if this encoding can be displayed in a mail/news agent.
118 public override bool IsMailNewsDisplay
123 // Determine if this encoding can be saved from a mail/news agent.
124 public override bool IsMailNewsSave
129 // Decoder that handles a rolling state.
130 internal class DbcsDecoder : Decoder
132 protected DbcsConvert convert;
133 protected int lastByte;
136 public DbcsDecoder(DbcsConvert convert)
138 this.convert = convert;
142 // Override inherited methods.
143 public override int GetCharCount(byte[] bytes, int index, int count)
146 throw new ArgumentNullException("bytes");
147 if (index < 0 || index > bytes.Length)
148 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
149 if (count < 0 || count > (bytes.Length - index))
150 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
151 char[] buffer = new char[count * 2];
152 return GetChars(bytes, index, count, buffer, 0);
155 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
156 char[] chars, int charIndex)
159 throw new ArgumentNullException("bytes");
161 throw new ArgumentNullException("chars");
162 if (byteIndex < 0 || byteIndex > bytes.Length)
163 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
164 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
165 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
166 if (charIndex < 0 || charIndex > chars.Length)
167 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
168 return 0; // For subclasses to implement