View Javadoc

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