Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a MalformedInputException from this code?

Tags:

I'm a newbie in Scala, and I wanted to write some sourcecodes from myself for me to get better. I've written a simple object (with a main entry) in order to simulate a "grep" call on all files of the current directory. (I launch the program from Eclipse Indigo, and in Debian Squeeze) :

package com.gmail.bernabe.laurent.scala.tests  import java.io.File  import scala.io.Source  object DealWithFiles {    def main(args:Array[String]){     for (result <- grepFilesHere(".*aur.*"))       println(result)   }    private def grepFilesHere(pattern:String):Array[String] = {     val filesHere = new File(".").listFiles      def linesOfFile(file:File) =       Source.fromFile(file).getLines.toList      for (file <- filesHere;         if file.isFile     )       yield linesOfFile(file)(0)   }  } 

But I get a java.nio.charset.MalformedInputException, which I am not able to solve :

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:260) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) at java.io.InputStreamReader.read(InputStreamReader.java:167) at java.io.BufferedReader.fill(BufferedReader.java:136) at java.io.BufferedReader.readLine(BufferedReader.java:299) at java.io.BufferedReader.readLine(BufferedReader.java:362) at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:67) at scala.collection.Iterator$class.foreach(Iterator.scala:772) at scala.io.BufferedSource$BufferedLineIterator.foreach(BufferedSource.scala:43) at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48) at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:130) at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:242) at scala.io.BufferedSource$BufferedLineIterator.toList(BufferedSource.scala:43) at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.linesOfFile$1(DealWithFiles.scala:18) at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$$anonfun$grepFilesHere$2.apply(DealWithFiles.scala:23) at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$$anonfun$grepFilesHere$2.apply(DealWithFiles.scala:20) at scala.collection.TraversableLike$WithFilter$$anonfun$map$2.apply(TraversableLike.scala:697) at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34) at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38) at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:696)    at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.grepFilesHere(DealWithFiles.scala:20) at com.gmail.bernabe.laurent.scala.tests.DealWithFiles$.main(DealWithFiles.scala:10) at com.gmail.bernabe.laurent.scala.tests.DealWithFiles.main(DealWithFiles.scala) 

Thanks in advance for helps :)

like image 407
loloof64 Avatar asked Jun 01 '12 08:06

loloof64


2 Answers

From the JavaDoc:

MalformedInputException thrown when an input byte sequence is not legal for given charset, or an input character sequence is not a legal sixteen-bit Unicode sequence.

Pass the currect encoding as parameter to Source.fromFile method.

like image 116
Prince John Wesley Avatar answered Oct 03 '22 19:10

Prince John Wesley


You can handle this character encoding exception by adding below snippet in your code

import scala.io.Codec import java.nio.charset.CodingErrorAction   implicit val codec = Codec("UTF-8") codec.onMalformedInput(CodingErrorAction.REPLACE) codec.onUnmappableCharacter(CodingErrorAction.REPLACE) 
like image 38
Madhu Avatar answered Oct 03 '22 21:10

Madhu