2007年6月5日火曜日

[JAVA>JYMSG]How to use JYMSG Java Library for Yahoo Japan

How to use JYMSG Java Library for Yahoo Japan

References
  • JYMSG Homepage: http://jymsg9.sourceforge.net/

Description
Now, the messenger server for Yahoo Japan is separated from Yahoo US, and the some spec of messenger client for Yahoo Japan is different from the client for Yahoo US. While implementing messenger for Yahoo Japan,I had a character encoding problem.

Only when sending a message from Yahoo Japan client to Yahoo Japan Messenger Server, Yahoo Japan Messenger Client is sending a packet encoded by using SJIS characterset (not using UTF-8).
This problem is occured only when using Yahoo Japan Client, not Yahoo US Client. (I don't know why yahoo japan uses SJIS character encoding.) I customized the JYMSG library source code for supporting Yahoo Japan as follows.

customization steps
  1. added following in YMSG9InputStream.java to handle user defined encoding.

    private String characterEncoding="UTF-8";
    public void setCharacterEncoding(String pCharacterEncoding){
    if(pCharacterEncoding==null||pCharacterEncoding.equals("")){
    return;
    }
    characterEncoding=pCharacterEncoding;
    }
    public String getCharacterEncoding(){
    return characterEncoding;
    }

  2. at 68 in YMSG9InputStream.java, change hardcoded UTF-8 characterset to variable named characterEncoding

    for(int i=0; i<body.length-1;i++)
    { if(u2i(body[i])==0xc0 && u2i(body[i+1])==0x80)
    { // -----Create a UTF-8 string and add to array
    //changed by tatsuya anno
    Commented out ==> //String s = new String(body,start,i-start,"UTF-8");
    added new line ==> String s=new String(body,start,i-start,characterEncoding);
    //end of customization
    v.addElement(s);
    if(dbis!=null) System.out.println(">>"+s);
    // -----Skip over second byte in separator, reset string start
    i++; start=i+1;
    }
    }

  3. added following in DirectConnectionHandler.java

    //added by tatsuya anno
    public void setInputStreamCharacterEncoding(String pCharacterEncoding){
    if(ips==null){
    return;
    }

    ips.setCharacterEncoding(pCharacterEncoding);
    }

  4. example using this customization

    //create ConnectionHander object to connect to yahoo japan
    DirectConnectionHandler connectionHandler=new DirectConnectionHandler("",);
    //create Session object
    Session yahooConnection=new Session(connectionHandler);
    yahooConnection.addSessionListener(new YahooPacketListener());
    yahooConnection.login(yahooClientUsername,yahooClientPassword);
    yahooConnection.setStatus(StatusConstants.STATUS_AVAILABLE);

    //after logged in, Set InputStream Encoding Charset
    //if you use this library for Yahoo US, you don't need to this line.
    connectionHandler.setInputStreamCharacterEncoding("SJIS");

0 件のコメント: