Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to skip invalid characters #255

Open
pathikrit opened this issue Aug 23, 2018 · 0 comments
Open

Ability to skip invalid characters #255

pathikrit opened this issue Aug 23, 2018 · 0 comments
Labels

Comments

@pathikrit
Copy link
Owner

Exception:

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at java.nio.file.Files.readAllLines(Files.java:3205)
at better.files.File.lines(File.scala:282)

Work around:

def linesSkipMalformed(file: File)(implicit charset: Charset = better.files.DefaultCharset): Traversable[String] = {
        @throws[IOException]
        def skippingBufferedReader(path: Path, cs: Charset): BufferedReader = {
            val decoder = cs.newDecoder.onMalformedInput(CodingErrorAction.IGNORE)
            val reader = new InputStreamReader(Files.newInputStream(path), decoder)
            new BufferedReader(reader)
        }

        @throws[IOException]
        def readAllLines(path: Path, cs: Charset): util.List[String] = {
            val reader: BufferedReader = skippingBufferedReader(path,cs)
            val result: util.List[String] = new util.ArrayList[String]

            var line: String = ""
            do {
                line = reader.readLine
                if(line != null) result.add(line)
            } while(line != null)

            if(reader != null) reader.close()

            result
        }

        readAllLines(file.path,charset).asScala
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant