Merge pull request #228 from QuickJack/3e163743eda89cc8c239779a75dd245be12aee3c
[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 #if DISABLE_UNSAFE
13 using MonoEncoder = I18N.Common.MonoSafeEncoder;
14 using MonoEncoding = I18N.Common.MonoSafeEncoding;
15 #endif
16
17 namespace I18N.CJK
18 {
19         [Serializable]
20         internal abstract class DbcsEncoding : MonoEncoding
21         {
22                 public DbcsEncoding (int codePage) : this (codePage, 0)
23                 {
24                 }
25
26                 public DbcsEncoding (int codePage, int windowsCodePage)
27                         : base (codePage, windowsCodePage)
28                 {
29                 }
30
31                 internal abstract DbcsConvert GetConvert ();
32
33                 // Get the number of bytes needed to encode a character buffer.
34                 public override int GetByteCount(char[] chars, int index, int count)
35                 {
36                         if (chars == null)
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);
44                 }
45                 
46 /*
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)
50                 {
51                         if (chars == null)
52                                 throw new ArgumentNullException("chars");
53                         if (bytes == null)
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
62                 }
63 */
64                 
65                 // Get the number of characters needed to decode a byte buffer.
66                 public override int GetCharCount(byte[] bytes, int index, int count)
67                 {
68                         if (bytes == null)
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);
76                 }
77                 
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)
81                 {
82                         if (bytes == null)
83                                 throw new ArgumentNullException("bytes");
84                         if (chars == null)
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
93                 }
94                 
95                 // Get the maximum number of bytes needed to encode a
96                 // specified number of characters.
97                 public override int GetMaxByteCount(int charCount)
98                 {
99                         if (charCount < 0)
100                                 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
101                         return charCount * 2;
102                 }
103                 
104                 // Get the maximum number of characters needed to decode a
105                 // specified number of bytes.
106                 public override int GetMaxCharCount(int byteCount)
107                 {
108                         if (byteCount < 0) {
109                                 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
110                         }
111                         return byteCount;
112                 }
113                 
114                 // Determine if this encoding can be displayed in a Web browser.
115                 public override bool IsBrowserDisplay
116                 {
117                         get { return true; }
118                 }
119                 
120                 // Determine if this encoding can be saved from a Web browser.
121                 public override bool IsBrowserSave
122                 {
123                         get { return true; }
124                 }
125                 
126                 // Determine if this encoding can be displayed in a mail/news agent.
127                 public override bool IsMailNewsDisplay
128                 {
129                         get { return true; }
130                 }
131                 
132                 // Determine if this encoding can be saved from a mail/news agent.
133                 public override bool IsMailNewsSave
134                 {
135                         get { return true; }
136                 }
137                 
138                 // Decoder that handles a rolling state.
139                 internal abstract class DbcsDecoder : Decoder
140                 {
141                         protected DbcsConvert convert;
142                         
143                         // Constructor.
144                         public DbcsDecoder(DbcsConvert convert)
145                         {
146                                 this.convert = convert;
147                         }
148                         
149                         internal void CheckRange (byte[] bytes, int index, int count)
150                         {
151                                 if (bytes == null)
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"));
157                         }
158
159                         internal void CheckRange (byte[] bytes, int byteIndex, int byteCount,
160                                                      char[] chars, int charIndex)
161                         {
162                                 if (bytes == null)
163                                         throw new ArgumentNullException("bytes");
164                                 if (chars == null)
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"));
172                         }
173                 }
174         }
175 }