文字のデコード

戻る
import java.nio.*;
import java.nio.charset.*;
import java.net.*;
import java.io.*;

/**
$Id: nio-test.html,v 1.1 2009/06/22 16:12:18 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class MyProto1 {
    private HttpURLConnection connection;
    private StringBuffer contentStringBuffer;
    private URL url;

    public MyProto1( URL url ) throws Exception {
        this.url = url;

        contentStringBuffer = new StringBuffer();

        // まずUTF8でデコード
        try {
            connect();
            retrieve( "UTF8" );
            return ;

        } catch ( Exception e ) {
            System.err.println( "UTF8でデコードできません!" );
            e.printStackTrace();
        }

        // 失敗したらJISAutoDetectでデコード
        try {
            disconnect();
            connect();
            retrieve( "JISAutoDetect" );
            return ;
        } catch ( Exception e ) {
            System.err.println( "JISAutoDetectでデコードできません!" );
            e.printStackTrace();
        }

        // 失敗したらEUC-JPでデコード
        try {
            disconnect();
            connect();
            retrieve( "EUC-JP" );
            return ;
        } catch ( Exception e ) {
            System.err.println( "EUC-JPでデコードできません!" );
            e.printStackTrace();
        }

        // 失敗したらWindows-31Jでデコード
        try {
            disconnect();
            connect();
            retrieve( "Windows-31J" );
            return ;
        } catch ( Exception e ) {
            System.err.println( "Windows-31Jでデコードできません!" );
            e.printStackTrace();
        }

        // 失敗したらISO-2022-JP(=JIS)でデコード
        try {
            disconnect();
            connect();
            retrieve( "ISO-2022-JP" );
            return ;
        } catch ( Exception e ) {
            System.err.println( "ISO-2022-JPでデコードできません!" );
            e.printStackTrace();
        }

        // 失敗したらShift_JISでデコード
        try {
            disconnect();
            connect();
            retrieve( "Shift_JIS" );
            return ;
        } catch ( Exception e ) {
            System.err.println( "Shift_JISでデコードできません!" );
            e.printStackTrace();
        }

    }

    private void connect() throws Exception {
        // コネクションを取得
        //--------------------
        connection = ( HttpURLConnection ) url.openConnection(); // HttpURLConnectionクラスにキャストする必要あり
    }
    private void disconnect() throws Exception {
        connection.disconnect();
    }

    private void retrieve( String sourceEncoding ) throws Exception {

        // バッファを空にする
        contentStringBuffer.setLength( 0 );

        try {

            CharsetDecoder decoder = Charset.forName( sourceEncoding ).newDecoder();
            BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                        connection.getInputStream(), decoder
                                    )
                                );

            while ( true ) {
                String line = br.readLine();
                if ( line == null ) {
                    break;
                }
                // System.out.println( line );

                contentStringBuffer.append( line + "\n" );

            }
            br.close();

        } catch ( Exception e ) {
            throw( e );
        }

    }

    public String getContent() {
        return contentStringBuffer.toString();
    }

    static public void main( String[] args ) {

        if ( args.length != 1 ) {
            System.out.println( "Usage: java -cp . MyProto1 [URL]" );
            System.exit( 1 );
        }

        MyProto1 mp = null;
        try {

            mp = new MyProto1( new URL( args[ 0 ] ) );

        } catch ( Exception e ) {
            e.printStackTrace();
        }

        System.out.println( mp.getContent() );

        System.err.println( "-----------------------------------" );
        System.err.println( "END." );
    }

}

戻る inserted by FC2 system