LoginSignup
2
2

More than 5 years have passed since last update.

UUDecode(UUEncodeのデコード)

Last updated at Posted at 2016-02-08

はじめに

事前に以下のライブラリを用意します。

実装例

サンプルでは、動作確認しやすいようにmainメソッドで実行できるようにしてあります。
結果だけを確認したい場合は、この記事の一番下のリンク先で使えるようにしてありますのでご覧ください。

UUDecoder.java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

/**
 *
 * @author tool-taro.com
 */
public class UUDecoder {

    public static void main(String[] args) throws MessagingException, IOException {

        //デコードしたい文字列
        String source = "begin 644 encoder.buf\n"
                + ")XX*_XX.MXX*F\n"
                + " \n"
                + "end";
        //デコード後に文字列に置き換える際のCharset
        Charset charset = StandardCharsets.UTF_8;

        //デコード処理
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        InputStream in = null;

        try {
            in = MimeUtility.decode(new ByteArrayInputStream(source.getBytes(charset)), "uuencode");
            byte[] buf = new byte[1024];
            int length;
            while (true) {
                length = in.read(buf);
                if (length == -1) {
                    break;
                }
                bout.write(buf, 0, length);
            }
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (IOException e) {
                }
            }
        }

        String result = new String(bout.toByteArray(), charset);
        //標準出力
        System.out.format("デコード結果=%1$s", result);
    }
}

動作確認

$ javac UUDecoder.java
$ java UUDecoder
$ デコード結果=タロウ

環境

  • 開発

    • Windows 10 Pro
    • JDK 1.8.0_112
    • NetBeans IDE 8.2
  • 動作検証

    • CentOS Linux release 7.2
    • JDK 1.8.0_112

上記の実装をベースにWebツールも公開しています。
UUDecode|Web便利ツール@ツールタロウ

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2