Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's due to a <a href="https://stackoverflow.com/questions/1835430/byte-order-mark-screws-up-file-reading-in-java">Byte Order Mark problem</a>. Here is a JUnit test case that demonstrates the problem and the fix:</p> <pre><code>package rss; import org.xml.sax.InputSource; import java.io.*; import java.net.*; import com.sun.syndication.io.*; import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.BOMInputStream; import org.junit.Test; public class RssEncodingTest { String url = "http://www.moneydj.com/KMDJ/RssCenter.aspx?svc=NH&amp;fno=1&amp;arg=X0000000"; // This works because we use InputSource direct from the UrlConnection's InputStream @Test public void test01() throws MalformedURLException, IOException, IllegalArgumentException, FeedException { try (InputStream is = new URL(url).openConnection().getInputStream()) { InputSource source = new InputSource(is); System.out.println("description: " + new SyndFeedInput().build(source).getDescription()); } } // But a String input fails because the byte order mark problem @Test public void test02() throws MalformedURLException, IOException, IllegalArgumentException, FeedException { String html = IOUtils.toString(new URL(url).openConnection() .getInputStream()); Reader reader = new StringReader(html); System.out.println("description: " + new SyndFeedInput().build(reader).getDescription()); } // We can use Apache Commons IO to fix the byte order mark @Test public void test03() throws MalformedURLException, IOException, IllegalArgumentException, FeedException { String html = IOUtils.toString(new URL(url).openConnection() .getInputStream()); try (BOMInputStream bomIn = new BOMInputStream( IOUtils.toInputStream(html))) { String f = IOUtils.toString(bomIn); Reader reader = new StringReader(f); System.out.println("description: " + new SyndFeedInput().build(reader).getDescription()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload