| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| com.ozacc.mail.fetch.impl.sk_jp.io.ByteToCharUTF7 |
|
|
| 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 | 0 | public class ByteToCharUTF7 extends sun.io.ByteToCharConverter { |
| 15 | public String getCharacterEncoding() { |
|
| 16 | 0 | return "UTF7"; |
| 17 | } |
|
| 18 | ||
| 19 | public int flush(char[] class="keyword">chars, class="keyword">int off, class="keyword">int len) { |
|
| 20 | 0 | byteOff = 0; |
| 21 | 0 | charOff = 0; |
| 22 | 0 | b64Context = false; |
| 23 | 0 | currentB64Off = 0; |
| 24 | 0 | currentChar = 0; |
| 25 | 0 | return 0; |
| 26 | } |
|
| 27 | public void reset() { |
|
| 28 | 0 | byteOff = 0; |
| 29 | 0 | charOff = 0; |
| 30 | 0 | b64Context = false; |
| 31 | 0 | currentB64Off = 0; |
| 32 | 0 | currentChar = 0; |
| 33 | 0 | } |
| 34 | ||
| 35 | 0 | private boolean b64Context = false; |
| 36 | 0 | private int currentB64Off = 0; |
| 37 | 0 | private char currentChar = 0; |
| 38 | ||
| 39 | public int convert( |
|
| 40 | byte[] bytes, |
|
| 41 | int byteStart, |
|
| 42 | int byteEnd, |
|
| 43 | char[] class="keyword">chars, |
|
| 44 | int charStart, |
|
| 45 | int charEnd) |
|
| 46 | throws |
|
| 47 | sun.io.ConversionBufferFullException, |
|
| 48 | sun.io.UnknownCharacterException { |
|
| 49 | 0 | charOff = charStart; |
| 50 | ||
| 51 | 0 | for (byteOff = byteStart; byteOff < byteEnd; byteOff++) { |
| 52 | 0 | if (charOff >= charEnd) { |
| 53 | 0 | throw new sun.io.ConversionBufferFullException(); |
| 54 | } |
|
| 55 | 0 | if (b64Context) { |
| 56 | 0 | if (bytes[byteOff] == '-') { |
| 57 | 0 | if (currentB64Off != 0 && currentChar > 0) { |
| 58 | 0 | chars[charOff] = currentChar; |
| 59 | 0 | charOff++; |
| 60 | } |
|
| 61 | 0 | b64Context = false; |
| 62 | 0 | continue; |
| 63 | } |
|
| 64 | 0 | int part = |
| 65 | 0 | ( |
| 66 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
| 67 | 0 | + "abcdefghijklmnopqrstuvwxyz0123456789+/").indexOf( |
| 68 | 0 | bytes[byteOff]); |
| 69 | 0 | if (part == -1) { |
| 70 | 0 | throw new sun.io.UnknownCharacterException( |
| 71 | 0 | "Invalid UTF-7 code: " + (char)bytes[byteOff]); |
| 72 | } |
|
| 73 | ||
| 74 | 0 | switch (currentB64Off) { |
| 75 | case 0 : |
|
| 76 | 0 | currentChar = (char) (part << 10); |
| 77 | 0 | break; |
| 78 | case 1 : |
|
| 79 | 0 | currentChar |= (char) (part << 4); |
| 80 | 0 | break; |
| 81 | case 2 : |
|
| 82 | 0 | currentChar |= (char) (part >> 2); |
| 83 | 0 | chars[charOff] = currentChar; |
| 84 | 0 | charOff++; |
| 85 | 0 | currentChar = (char) ((part & 0x03) << 14); |
| 86 | 0 | break; |
| 87 | case 3 : |
|
| 88 | 0 | currentChar |= (char) (part << 8); |
| 89 | 0 | break; |
| 90 | case 4 : |
|
| 91 | 0 | currentChar |= (char) (part << 2); |
| 92 | 0 | break; |
| 93 | case 5 : |
|
| 94 | 0 | currentChar |= (char) (part >> 4); |
| 95 | 0 | chars[charOff] = currentChar; |
| 96 | 0 | charOff++; |
| 97 | 0 | currentChar = (char) ((part & 0x0f) << 12); |
| 98 | 0 | break; |
| 99 | case 6 : |
|
| 100 | 0 | currentChar |= (char) (part << 6); |
| 101 | 0 | break; |
| 102 | case 7 : |
|
| 103 | 0 | currentChar |= (char)part; |
| 104 | 0 | chars[charOff] = currentChar; |
| 105 | 0 | charOff++; |
| 106 | break; |
|
| 107 | } |
|
| 108 | 0 | currentB64Off = (currentB64Off + 1) % 8; |
| 109 | 0 | continue; |
| 110 | } |
|
| 111 | ||
| 112 | 0 | if (bytes[byteOff] == '+') { |
| 113 | // shift character |
|
| 114 | // This is start of the Base64 sequence. |
|
| 115 | 0 | b64Context = true; |
| 116 | 0 | currentB64Off = 0; |
| 117 | 0 | continue; |
| 118 | } |
|
| 119 | 0 | chars[charOff] = (char)bytes[byteOff]; |
| 120 | 0 | charOff++; |
| 121 | } |
|
| 122 | 0 | 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 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |