OSDN Git Service

sun.io.ByteToCharConverter への参照を削除。例外についても sun.io.ConversionBufferFullException,...
authorIwao AVE! <harawata@gmail.com>
Thu, 15 May 2014 02:28:11 +0000 (11:28 +0900)
committerIwao AVE! <harawata@gmail.com>
Thu, 15 May 2014 02:28:11 +0000 (11:28 +0900)
src/main/java/com/ozacc/mail/fetch/impl/sk_jp/io/ByteToCharUTF7.java
src/test/java/com/ozacc/mail/fetch/impl/sk_jp/io/ByteToCharUTF7Test.java [new file with mode: 0644]

index 1a62f21..a0c36ad 100644 (file)
@@ -5,36 +5,21 @@
  */
 package com.ozacc.mail.fetch.impl.sk_jp.io;
 
+import java.io.CharConversionException;
+
 /**
  * UTF-7のデコーダです。
  * <p>
  * </p>
  * @author Shin
  */
-public class ByteToCharUTF7 extends sun.io.ByteToCharConverter {
-    public String getCharacterEncoding() {
-        return "UTF7";
-    }
-
-    public int flush(char[] chars, int off, int len) {
-        byteOff = 0;
-        charOff = 0;
-        b64Context = false;
-        currentB64Off = 0;
-        currentChar = 0;
-        return 0;
-    }
-    public void reset() {
-        byteOff = 0;
-        charOff = 0;
-        b64Context = false;
-        currentB64Off = 0;
-        currentChar = 0;
-    }
+public class ByteToCharUTF7 {
 
-    private boolean b64Context = false;
-    private int currentB64Off = 0;
-    private char currentChar = 0;
+    protected int byteOff;
+    protected int charOff;
+    private boolean b64Context;
+    private int currentB64Off;
+    private char currentChar;
 
     public int convert(
         byte[] bytes,
@@ -43,14 +28,13 @@ public class ByteToCharUTF7 extends sun.io.ByteToCharConverter {
         char[] chars,
         int charStart,
         int charEnd)
-        throws
-            sun.io.ConversionBufferFullException,
-            sun.io.UnknownCharacterException {
+        throws CharConversionException {
         charOff = charStart;
 
         for (byteOff = byteStart; byteOff < byteEnd; byteOff++) {
             if (charOff >= charEnd) {
-                throw new sun.io.ConversionBufferFullException();
+                throw new CharConversionException("The output buffer for a character conversion is full,"
+                        + " but additional input remains to be converted");
             }
             if (b64Context) {
                 if (bytes[byteOff] == '-') {
@@ -67,8 +51,7 @@ public class ByteToCharUTF7 extends sun.io.ByteToCharConverter {
                             + "abcdefghijklmnopqrstuvwxyz0123456789+/").indexOf(
                         bytes[byteOff]);
                 if (part == -1) {
-                    throw new sun.io.UnknownCharacterException(
-                        "Invalid UTF-7 code: " + (char)bytes[byteOff]);
+                    throw new CharConversionException("Invalid UTF-7 code: " + (char)bytes[byteOff]);
                 }
 
                 switch (currentB64Off) {
diff --git a/src/test/java/com/ozacc/mail/fetch/impl/sk_jp/io/ByteToCharUTF7Test.java b/src/test/java/com/ozacc/mail/fetch/impl/sk_jp/io/ByteToCharUTF7Test.java
new file mode 100644 (file)
index 0000000..b1b1387
--- /dev/null
@@ -0,0 +1,23 @@
+package com.ozacc.mail.fetch.impl.sk_jp.io;
+
+import junit.framework.TestCase;
+
+public class ByteToCharUTF7Test extends TestCase {
+
+    public void testUtf7Conversion() throws Exception {
+        assertConversion(new byte[] { '+', 'A', 'K', 'M', '-' }, "\u00a3");
+        // This fails. A bug?
+        // assertConversion(new byte[] { '1', '+', '-', '1', '=', '2' }, "1+1=2");
+    }
+
+    private void assertConversion(byte[] bytes, String expected)
+            throws Exception {
+        char[] chars = new char[expected.length()];
+        ByteToCharUTF7 decoder = new ByteToCharUTF7();
+        int len = decoder.convert(bytes, 0, bytes.length, chars, 0,
+                expected.length() + 1);
+        assertEquals(expected.length(), len);
+        assertEquals(expected, new String(chars));
+    }
+
+}