~ read.

"MalformedInputException: Input length = 1 " exception solution for Scala and Java

Recently i was adding more values to the resource file in my Fabricator project and , looke like added values that are invalid for UTF-8 encoding. My next trial to execute tests for the project brought me to "MalformedInputException: Input length = 1 " exception. For scala code i managed to find a very easy solution.
Instead of

Json.parse(Source.fromInputStream(getClass().getClassLoader().
getResourceAsStream(lang + ".json")).mkString)

I'm using now this line of code :

Json.parse(Source.fromInputStream(getClass().getClassLoader().
getResourceAsStream(lang + ".json"))("UTF-8").mkString)

For Java, you'll have to write a bit more lines of code, but eventually you'll be able to find your "failing" symbols and maybe change them to UTF-8-proper-ones.

FileInputStream input;
    String result = null;
    try {
        input = new FileInputStream(new File("invalid.txt"));
        CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
        decoder.onMalformedInput(CodingErrorAction.IGNORE);
        InputStreamReader reader = new InputStreamReader(input, decoder);
        BufferedReader bufferedReader = new BufferedReader( reader );
        StringBuilder sb = new StringBuilder();
        String line = bufferedReader.readLine();
        while( line != null ) {
            sb.append( line );
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        result = sb.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch( IOException e ) {
        e.printStackTrace();
    }
    System.out.println(result);

If your file has for example hellö wörld in it, then with .REPLACE command you see the standard unicode replacement character being used:

    //"h�ellö� wö�rld�"

With .IGNORE, you see the invalid bytes ignored:

    //"hellö wörld"

Without specifying .onMalformedInput, you get:

    java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)

The solution was found on stackoverflow

comments powered by Disqus
comments powered by Disqus