merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / I18N / CJK / DbcsEncoding.cs
1 //
2 // I18N.CJK.DbcsEncoding
3 //
4 // Author:
5 //   Alan Tam Siu Lung (Tam@SiuLung.com)
6 //
7
8 using System;
9 using System.Text;
10 using I18N.Common;
11
12 namespace I18N.CJK
13 {
14         [Serializable]
15         internal abstract class DbcsEncoding : MonoEncoding
16         {
17                 public DbcsEncoding (int codePage) : this (codePage, 0)
18                 {
19                 }
20
21                 public DbcsEncoding (int codePage, int windowsCodePage)
22                         : base (codePage, windowsCodePage)
23                 {
24                 }
25
26                 internal abstract DbcsConvert GetConvert ();
27
28                 // Get the number of bytes needed to encode a character buffer.
29                 public override int GetByteCount(char[] chars, int index, int count)
30                 {
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);
39                 }
40                 
41 /*
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)
45                 {
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
57                 }
58 */
59                 
60                 // Get the number of characters needed to decode a byte buffer.
61                 public override int GetCharCount(byte[] bytes, int index, int count)
62                 {
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);
71                 }
72                 
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)
76                 {
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
88                 }
89                 
90                 // Get the maximum number of bytes needed to encode a
91                 // specified number of characters.
92                 public override int GetMaxByteCount(int charCount)
93                 {
94                         if (charCount < 0)
95                                 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
96                         return charCount * 2;
97                 }
98                 
99                 // Get the maximum number of characters needed to decode a
100                 // specified number of bytes.
101                 public override int GetMaxCharCount(int byteCount)
102                 {
103                         if (byteCount < 0) {
104                                 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
105                         }
106                         return byteCount;
107                 }
108                 
109                 // Determine if this encoding can be displayed in a Web browser.
110                 public override bool IsBrowserDisplay
111                 {
112                         get { return true; }
113                 }
114                 
115                 // Determine if this encoding can be saved from a Web browser.
116                 public override bool IsBrowserSave
117                 {
118                         get { return true; }
119                 }
120                 
121                 // Determine if this encoding can be displayed in a mail/news agent.
122                 public override bool IsMailNewsDisplay
123                 {
124                         get { return true; }
125                 }
126                 
127                 // Determine if this encoding can be saved from a mail/news agent.
128                 public override bool IsMailNewsSave
129                 {
130                         get { return true; }
131                 }
132                 
133                 // Decoder that handles a rolling state.
134                 internal abstract class DbcsDecoder : Decoder
135                 {
136                         protected DbcsConvert convert;
137                         
138                         // Constructor.
139                         public DbcsDecoder(DbcsConvert convert)
140                         {
141                                 this.convert = convert;
142                         }
143                         
144                         internal void CheckRange (byte[] bytes, int index, int count)
145                         {
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"));
152                         }
153
154                         internal void CheckRange (byte[] bytes, int byteIndex, int byteCount,
155                                                      char[] chars, int charIndex)
156                         {
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"));
167                         }
168                 }
169         }
170 }