2007年6月8日金曜日

[JAVA>Rome, RSS/XML] When using Rome RSS Reader or Rome Fetcher(XML Reader), MalformedInputException occured

References
  1. Rome WebSite
    https://rome.dev.java.net/
Problems
When using Rome RSS Reader or Rome Fetcher to get RSS Data from any Website, sometimes sun.io.MalformedInputException error occured randomly. In case of fetching RSS from same Website, sometimes error occured, but sometimes not occured. Error stacks were as follows.

sun.io.MalformedInputException
at sun.io.ByteToCharUTF8.flush(ByteToCharUTF8.java:139)
at sun.nio.cs.StreamDecoder$ConverterSD.flushInto(StreamDecoder.java:333)
at sun.nio.cs.StreamDecoder$ConverterSD.implRead(StreamDecoder.java:357)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:250)
at java.io.InputStreamReader.read(InputStreamReader.java:212)
at java.io.BufferedReader.fill(BufferedReader.java:157)
at java.io.BufferedReader.readLine(BufferedReader.java:320)
at java.io.BufferedReader.readLine(BufferedReader.java:383)
at com.sun.syndication.io.XmlReader.getXmlProlog(XmlReader.java:534)
at com.sun.syndication.io.XmlReader.doHttpStream(XmlReader.java:325)
at com.sun.syndication.io.XmlReader.(XmlReader.java:248)


Reason why this errr occured
While finding the Character Encoding in RSS InputStream in getXmlProlog() function In XmlReader.java(rome-0.9/src/java/com/sun/syndication/io/XmlReader.java), System tries to get first 4096 bytes characters from Stream. In case of including Japanese or not Ascii characters, System sometimes truncates the bytes at the invalid byte position(Japanese uses 3 byte for each word in UTF-8), so this problem occures.

Solution
  • Fixed file
    rome-0.9/src/java/com/sun/syndication/io/XmlReader.java
  • Fixed function
    Change as follows in getXmlProlog() function.
  • Description
    We catches the MalformedInputException exception and ignore occuring this exception because the line string this exception occures doesn't usually need. Add try and catch block when readling the line.
  • Example

    /** Changed by tatsuya anno to avoid MalformedInputException*/
    try{
    String line = br.readLine();
    while (line != null) {
    prolog.append(line).append("\n");
    line = br.readLine();
    }
    }catch(sun.io.MalformedInputException eMalformedInput){
    //skip
    }


0 件のコメント: