2010-04-15 Jb Evain <jbevain@novell.com>
[mcs.git] / class / I18N / CJK / DbcsEncoding.cs
blob5768ee43171e67f3e4b8343982476a3344ec5d2e
1 //
2 // I18N.CJK.DbcsEncoding
3 //
4 // Author:
5 // Alan Tam Siu Lung (Tam@SiuLung.com)
6 //
8 using System;
9 using System.Text;
10 using I18N.Common;
12 namespace I18N.CJK
14 [Serializable]
15 internal abstract class DbcsEncoding : MonoEncoding
17 public DbcsEncoding (int codePage) : this (codePage, 0)
21 public DbcsEncoding (int codePage, int windowsCodePage)
22 : base (codePage, windowsCodePage)
26 internal abstract DbcsConvert GetConvert ();
28 // Get the number of bytes needed to encode a character buffer.
29 public override int GetByteCount(char[] chars, int index, int count)
31 if (chars == null)
32 throw new ArgumentNullException("chars");
33 if (index < 0 || index > chars.Length)
34 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
35 if (count < 0 || index + count > chars.Length)
36 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
37 byte[] buffer = new byte[count * 2];
38 return GetBytes(chars, index, count, buffer, 0);
42 // Get the bytes that result from encoding a character buffer.
43 public override int GetBytes(char[] chars, int charIndex, int charCount,
44 byte[] bytes, int byteIndex)
46 if (chars == null)
47 throw new ArgumentNullException("chars");
48 if (bytes == null)
49 throw new ArgumentNullException("bytes");
50 if (charIndex < 0 || charIndex > chars.Length)
51 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
52 if (charCount < 0 || charIndex + charCount > chars.Length)
53 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_Array"));
54 if (byteIndex < 0 || byteIndex > bytes.Length)
55 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
56 return 0; // For subclasses to implement
60 // Get the number of characters needed to decode a byte buffer.
61 public override int GetCharCount(byte[] bytes, int index, int count)
63 if (bytes == null)
64 throw new ArgumentNullException("bytes");
65 if (index < 0 || index > bytes.Length)
66 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
67 if (count < 0 || index + count > bytes.Length)
68 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
69 char[] buffer = new char[count];
70 return GetChars(bytes, index, count, buffer, 0);
73 // Get the characters that result from decoding a byte buffer.
74 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
75 char[] chars, int charIndex)
77 if (bytes == null)
78 throw new ArgumentNullException("bytes");
79 if (chars == null)
80 throw new ArgumentNullException("chars");
81 if (byteIndex < 0 || byteIndex > bytes.Length)
82 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
83 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
84 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
85 if (charIndex < 0 || charIndex > chars.Length)
86 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));
87 return 0; // For subclasses to implement
90 // Get the maximum number of bytes needed to encode a
91 // specified number of characters.
92 public override int GetMaxByteCount(int charCount)
94 if (charCount < 0)
95 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
96 return charCount * 2;
99 // Get the maximum number of characters needed to decode a
100 // specified number of bytes.
101 public override int GetMaxCharCount(int byteCount)
103 if (byteCount < 0) {
104 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
106 return byteCount;
109 // Determine if this encoding can be displayed in a Web browser.
110 public override bool IsBrowserDisplay
112 get { return true; }
115 // Determine if this encoding can be saved from a Web browser.
116 public override bool IsBrowserSave
118 get { return true; }
121 // Determine if this encoding can be displayed in a mail/news agent.
122 public override bool IsMailNewsDisplay
124 get { return true; }
127 // Determine if this encoding can be saved from a mail/news agent.
128 public override bool IsMailNewsSave
130 get { return true; }
133 // Decoder that handles a rolling state.
134 internal abstract class DbcsDecoder : Decoder
136 protected DbcsConvert convert;
138 // Constructor.
139 public DbcsDecoder(DbcsConvert convert)
141 this.convert = convert;
144 internal void CheckRange (byte[] bytes, int index, int count)
146 if (bytes == null)
147 throw new ArgumentNullException("bytes");
148 if (index < 0 || index > bytes.Length)
149 throw new ArgumentOutOfRangeException("index", Strings.GetString("ArgRange_Array"));
150 if (count < 0 || count > (bytes.Length - index))
151 throw new ArgumentOutOfRangeException("count", Strings.GetString("ArgRange_Array"));
154 internal void CheckRange (byte[] bytes, int byteIndex, int byteCount,
155 char[] chars, int charIndex)
157 if (bytes == null)
158 throw new ArgumentNullException("bytes");
159 if (chars == null)
160 throw new ArgumentNullException("chars");
161 if (byteIndex < 0 || byteIndex > bytes.Length)
162 throw new ArgumentOutOfRangeException("byteIndex", Strings.GetString("ArgRange_Array"));
163 if (byteCount < 0 || byteIndex + byteCount > bytes.Length)
164 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_Array"));
165 if (charIndex < 0 || charIndex > chars.Length)
166 throw new ArgumentOutOfRangeException("charIndex", Strings.GetString("ArgRange_Array"));