java.nio.charset.MalformedInputException: Input length = 1 when printing length of Iterator

5,550 views
Skip to first unread message

Markus Jais

unread,
Mar 12, 2012, 3:38:05 PM3/12/12
to scala...@googlegroups.com
Hello,

I have an error message I don't understand:

This is my code:

val lines = scala.io.Source.fromURL("http://www.google.de").getLines
println(lines.size)

and I get this stacktrace:
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(CoderResult.java:277)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:338)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:154)
    at java.io.BufferedReader.readLine(BufferedReader.java:317)
    at java.io.BufferedReader.readLine(BufferedReader.java:382)
    at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:67)
    at scala.collection.Iterator$class.foreach(Iterator.scala:660)
    at scala.io.BufferedSource$BufferedLineIterator.foreach(BufferedSource.scala:43)
    at scala.collection.TraversableOnce$class.size(TraversableOnce.scala:104)
    at scala.io.BufferedSource$BufferedLineIterator.size(BufferedSource.scala:43)
    at com.markusjais.futures.jdk.downloader$.main(downloader.scala:21)
    at com.markusjais.futures.jdk.downloader.main(downloader.scala)

The "size" method just returns an Integer, so that Integer should be printed.  Or am I just blind
to the obvious error?
The same when  "length" is called on the Iterator.

Why is that?  Any ideas?

regards,

Markus

Daniel Sobral

unread,
Mar 12, 2012, 3:47:10 PM3/12/12
to Markus Jais, scala...@googlegroups.com
You are getting a page back that is not UTF-8. You can look into
providing a charset explicitly, but to handle the mess of http and
html correctly (or less wrongly, since there's really no "correct"
that will fit all) requires much more support than what you got there.

--
Daniel C. Sobral

I travel to the future all the time.

Markus Jais

unread,
Mar 13, 2012, 2:47:39 AM3/13/12
to scala...@googlegroups.com
Hi Daniel,

thanks. That makes sense. So this is not a problem of the size method but of reading
the input from the Source object. I will try to fix this or use another example. This
was mostly to demonstrate how futures work (JDK futures, Guava  Futures, Akka  Futures) for
a talk at work, so I can come up with something that takes a short while and is less trouble
than UTF-8, HTTP and HTML.

Cheers,

Markus


Von: Daniel Sobral <dcso...@gmail.com>
An: Markus Jais <marku...@yahoo.de>
CC: "scala...@googlegroups.com" <scala...@googlegroups.com>
Gesendet: 20:47 Montag, 12.März 2012
Betreff: Re: [scala-user] java.nio.charset.MalformedInputException: Input length = 1 when printing length of Iterator
Reply all
Reply to author
Forward
0 new messages