2005-04-12 Dick Porter <dick@ximian.com>
[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         internal class DbcsEncoding : Encoding
15         {
16                 internal DbcsConvert convert;
17                 
18                 public DbcsEncoding(int codePage) : base(codePage) {}
19                 
20                 // Get the number of bytes needed to encode a character buffer.
21                 public override int GetByteCount(char[] chars, int index, int count)
22                 {
23                         if (chars == null)
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);
31                 }
32                 
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)
36                 {
37                         if (chars == null)
38                                 throw new ArgumentNullException("chars");
39                         if (bytes == null)
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
48                 }
49                 
50                 // Get the number of characters needed to decode a byte buffer.
51                 public override int GetCharCount(byte[] bytes, int index, int count)
52                 {
53                         if (bytes == null)
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);
61                 }
62                 
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)
66                 {
67                         if (bytes == null)
68                                 throw new ArgumentNullException("bytes");
69                         if (chars == null)
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
78                 }
79                 
80                 // Get the maximum number of bytes needed to encode a
81                 // specified number of characters.
82                 public override int GetMaxByteCount(int charCount)
83                 {
84                         if (charCount < 0)
85                                 throw new ArgumentOutOfRangeException("charCount", Strings.GetString("ArgRange_NonNegative"));
86                         return charCount * 2;
87                 }
88                 
89                 // Get the maximum number of characters needed to decode a
90                 // specified number of bytes.
91                 public override int GetMaxCharCount(int byteCount)
92                 {
93                         if (byteCount < 0) {
94                                 throw new ArgumentOutOfRangeException("byteCount", Strings.GetString("ArgRange_NonNegative"));
95                         }
96                         return byteCount;
97                 }
98                 
99                 // Get a decoder that handles a rolling state.
100                 public override Decoder GetDecoder()
101                 {
102                         return new DbcsDecoder(convert);
103                 }
104                 
105                 // Determine if this encoding can be displayed in a Web browser.
106                 public override bool IsBrowserDisplay
107                 {
108                         get { return true; }
109                 }
110                 
111                 // Determine if this encoding can be saved from a Web browser.
112                 public override bool IsBrowserSave
113                 {
114                         get { return true; }
115                 }
116                 
117                 // Determine if this encoding can be displayed in a mail/news agent.
118                 public override bool IsMailNewsDisplay
119                 {
120                         get { return true; }
121                 }
122                 
123                 // Determine if this encoding can be saved from a mail/news agent.
124                 public override bool IsMailNewsSave
125                 {
126                         get { return true; }
127                 }
128                 
129                 // Decoder that handles a rolling state.
130                 internal class DbcsDecoder : Decoder
131                 {
132                         protected DbcsConvert convert;
133                         protected int lastByte;
134                         
135                         // Constructor.
136                         public DbcsDecoder(DbcsConvert convert)
137                         {
138                                 this.convert = convert;
139                                 this.lastByte = 0;
140                         }
141                         
142                         // Override inherited methods.
143                         public override int GetCharCount(byte[] bytes, int index, int count)
144                         {
145                                 if (bytes == null)
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);
153                         }
154                         
155                         public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
156                                                      char[] chars, int charIndex)
157                         {
158                                 if (bytes == null)
159                                         throw new ArgumentNullException("bytes");
160                                 if (chars == null)
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
169                         }
170                 }
171         }
172 }