scala - Source.fromInputStream exception handling during reading lines -


i have created function take in parameter inputstream , return iterator consisting of string. accomplish follows:

def lineentry(fileinputstream:inputstream):iterator[string] = {    source.frominputstream(fileinputstream).getlines() } 

i use method follows:

val fstream = getsomeinputstreamfromsource() lineentry(fstream).foreach{   processtheline(_) } 

now quite possible method lineentry might blow if encounters bad character while it's iterating on inputstream using foreach.

what of ways counter situation?

quick solution (for scala 2.10):

def lineentry(fileinputstream:inputstream):iterator[string] = {   implicit val codec = codec.utf8 // or other   codec.onmalformedinput(codingerroraction.ignore)    source.frominputstream(fileinputstream).getlines() } 

in scala 2.9 there's small difference:

implicit val codec = codec(codec.utf8) 

codec has few more configuration options can tune behaviour in such cases.


Comments