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
6 package com.ozacc.mail.fetch.impl.sk_jp.io;
\r
14 public class ByteToCharUTF7 extends sun.io.ByteToCharConverter {
\r
15 public String getCharacterEncoding() {
\r
19 public int flush(char[] chars, int off, int len) {
\r
27 public void reset() {
\r
35 private boolean b64Context = false;
\r
36 private int currentB64Off = 0;
\r
37 private char currentChar = 0;
\r
47 sun.io.ConversionBufferFullException,
\r
48 sun.io.UnknownCharacterException {
\r
49 charOff = charStart;
\r
51 for (byteOff = byteStart; byteOff < byteEnd; byteOff++) {
\r
52 if (charOff >= charEnd) {
\r
53 throw new sun.io.ConversionBufferFullException();
\r
56 if (bytes[byteOff] == '-') {
\r
57 if (currentB64Off != 0 && currentChar > 0) {
\r
58 chars[charOff] = currentChar;
\r
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
\r
67 + "abcdefghijklmnopqrstuvwxyz0123456789+/").indexOf(
\r
70 throw new sun.io.UnknownCharacterException(
\r
71 "Invalid UTF-7 code: " + (char)bytes[byteOff]);
\r
74 switch (currentB64Off) {
\r
76 currentChar = (char) (part << 10);
\r
79 currentChar |= (char) (part << 4);
\r
82 currentChar |= (char) (part >> 2);
\r
83 chars[charOff] = currentChar;
\r
85 currentChar = (char) ((part & 0x03) << 14);
\r
88 currentChar |= (char) (part << 8);
\r
91 currentChar |= (char) (part << 2);
\r
94 currentChar |= (char) (part >> 4);
\r
95 chars[charOff] = currentChar;
\r
97 currentChar = (char) ((part & 0x0f) << 12);
\r
100 currentChar |= (char) (part << 6);
\r
103 currentChar |= (char)part;
\r
104 chars[charOff] = currentChar;
\r
108 currentB64Off = (currentB64Off + 1) % 8;
\r
112 if (bytes[byteOff] == '+') {
\r
114 // This is start of the Base64 sequence.
\r
119 chars[charOff] = (char)bytes[byteOff];
\r
122 return charOff - charStart;
\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
131 while ((len = System.in.read(b)) != -1) {
\r
132 o.write(b, 0, len);
\r
134 byte[] bytes = o.toByteArray();
\r
136 System.out.println(new String(bytes, "UTF7"));
\r