OSDN Git Service

CVS最新版の全ファイルを追加
[spring-ext/ozacc-mail.git] / src / java / com / ozacc / mail / fetch / impl / sk_jp / io / ByteToCharUTF7.java
1 /*\r
2  * @(#) $Id: ByteToCharUTF7.java,v 1.1.2.1 2004/09/29 00:57:59 otsuka Exp $\r
3  * $Revision: 1.1.2.1 $\r
4  * Copyright (c) 2000 Shin Kinoshita All Rights Reserved.\r
5  */\r
6 package com.ozacc.mail.fetch.impl.sk_jp.io;\r
7 \r
8 /**\r
9  * UTF-7のデコーダです。\r
10  * <p>\r
11  * </p>\r
12  * @author Shin\r
13  */\r
14 public class ByteToCharUTF7 extends sun.io.ByteToCharConverter {\r
15     public String getCharacterEncoding() {\r
16         return "UTF7";\r
17     }\r
18 \r
19     public int flush(char[] chars, int off, int len) {\r
20         byteOff = 0;\r
21         charOff = 0;\r
22         b64Context = false;\r
23         currentB64Off = 0;\r
24         currentChar = 0;\r
25         return 0;\r
26     }\r
27     public void reset() {\r
28         byteOff = 0;\r
29         charOff = 0;\r
30         b64Context = false;\r
31         currentB64Off = 0;\r
32         currentChar = 0;\r
33     }\r
34 \r
35     private boolean b64Context = false;\r
36     private int currentB64Off = 0;\r
37     private char currentChar = 0;\r
38 \r
39     public int convert(\r
40         byte[] bytes,\r
41         int byteStart,\r
42         int byteEnd,\r
43         char[] chars,\r
44         int charStart,\r
45         int charEnd)\r
46         throws\r
47             sun.io.ConversionBufferFullException,\r
48             sun.io.UnknownCharacterException {\r
49         charOff = charStart;\r
50 \r
51         for (byteOff = byteStart; byteOff < byteEnd; byteOff++) {\r
52             if (charOff >= charEnd) {\r
53                 throw new sun.io.ConversionBufferFullException();\r
54             }\r
55             if (b64Context) {\r
56                 if (bytes[byteOff] == '-') {\r
57                     if (currentB64Off != 0 && currentChar > 0) {\r
58                         chars[charOff] = currentChar;\r
59                         charOff++;\r
60                     }\r
61                     b64Context = false;\r
62                     continue;\r
63                 }\r
64                 int part =\r
65                     (\r
66                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\r
67                             + "abcdefghijklmnopqrstuvwxyz0123456789+/").indexOf(\r
68                         bytes[byteOff]);\r
69                 if (part == -1) {\r
70                     throw new sun.io.UnknownCharacterException(\r
71                         "Invalid UTF-7 code: " + (char)bytes[byteOff]);\r
72                 }\r
73 \r
74                 switch (currentB64Off) {\r
75                     case 0 :\r
76                         currentChar = (char) (part << 10);\r
77                         break;\r
78                     case 1 :\r
79                         currentChar |= (char) (part << 4);\r
80                         break;\r
81                     case 2 :\r
82                         currentChar |= (char) (part >> 2);\r
83                         chars[charOff] = currentChar;\r
84                         charOff++;\r
85                         currentChar = (char) ((part & 0x03) << 14);\r
86                         break;\r
87                     case 3 :\r
88                         currentChar |= (char) (part << 8);\r
89                         break;\r
90                     case 4 :\r
91                         currentChar |= (char) (part << 2);\r
92                         break;\r
93                     case 5 :\r
94                         currentChar |= (char) (part >> 4);\r
95                         chars[charOff] = currentChar;\r
96                         charOff++;\r
97                         currentChar = (char) ((part & 0x0f) << 12);\r
98                         break;\r
99                     case 6 :\r
100                         currentChar |= (char) (part << 6);\r
101                         break;\r
102                     case 7 :\r
103                         currentChar |= (char)part;\r
104                         chars[charOff] = currentChar;\r
105                         charOff++;\r
106                         break;\r
107                 }\r
108                 currentB64Off = (currentB64Off + 1) % 8;\r
109                 continue;\r
110             }\r
111 \r
112             if (bytes[byteOff] == '+') {\r
113                 // shift character\r
114                 // This is start of the Base64 sequence.\r
115                 b64Context = true;\r
116                 currentB64Off = 0;\r
117                 continue;\r
118             }\r
119             chars[charOff] = (char)bytes[byteOff];\r
120             charOff++;\r
121         }\r
122         return charOff - charStart;\r
123     }\r
124 \r
125     /*\r
126         public static void main(String[] args) throws Exception {\r
127             System.setProperty("file.encoding.pkg", "com.sk_jp.io");\r
128             ByteArrayOutputStream o = new ByteArrayOutputStream();\r
129             byte[] b = new byte[2048];\r
130             int len;\r
131             while ((len = System.in.read(b)) != -1) {\r
132                 o.write(b, 0, len);\r
133             }\r
134             byte[] bytes = o.toByteArray();\r
135     \r
136             System.out.println(new String(bytes, "UTF7"));\r
137         }\r
138     */\r
139 }\r