Search in sources :

Example 6 with UnmappableCharacterException

use of java.nio.charset.UnmappableCharacterException in project robovm by robovm.

the class CharsetEncoderTest method testEncodeCharBufferException.

public void testEncodeCharBufferException() throws CharacterCodingException {
    ByteBuffer out;
    CharBuffer in;
    // MalformedException:
    in = getMalformedCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        try {
            // regression test for Harmony-1379
            encoder.encode(in);
            fail("should throw MalformedInputException");
        } catch (MalformedInputException e) {
        }
        encoder.reset();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.IGNORE);
        out = encoder.encode(in);
        assertByteArray(out, addSurrogate(unibytes));
        encoder.reset();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        out = encoder.encode(in);
        assertByteArray(out, addSurrogate(unibytesWithRep));
    }
    // Unmapped Exception:
    in = getUnmapCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        encoder.reset();
        try {
            encoder.encode(in);
            fail("should throw UnmappableCharacterException");
        } catch (UnmappableCharacterException e) {
        }
        encoder.reset();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        out = encoder.encode(in);
        assertByteArray(out, unibytes);
        encoder.reset();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        out = encoder.encode(in);
        assertByteArray(out, unibytesWithRep);
    }
    // RuntimeException
    try {
        encoder.encode(getExceptionCharBuffer());
        fail("should throw runtime exception");
    } catch (RuntimeException e) {
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 7 with UnmappableCharacterException

use of java.nio.charset.UnmappableCharacterException in project jdk8u_jdk by JetBrains.

the class BytesAndLines method testWriteLines.

/**
     * Exercise Files.write(Path, Iterable<? extends CharSequence>, Charset, OpenOption...)
     */
public void testWriteLines() throws IOException {
    // zero lines
    Path result = Files.write(tmpfile, Collections.<String>emptyList(), US_ASCII);
    assert (Files.size(tmpfile) == 0);
    assert (result == tmpfile);
    // two lines
    List<String> lines = Arrays.asList("hi", "there");
    Files.write(tmpfile, lines, US_ASCII);
    List<String> actual = Files.readAllLines(tmpfile, US_ASCII);
    assertTrue(actual.equals(lines), "Unexpected lines");
    // append two lines
    Files.write(tmpfile, lines, US_ASCII, APPEND);
    List<String> expected = new ArrayList<>();
    expected.addAll(lines);
    expected.addAll(lines);
    assertTrue(expected.size() == 4, "List should have 4 elements");
    actual = Files.readAllLines(tmpfile, US_ASCII);
    assertTrue(actual.equals(expected), "Unexpected lines");
    // UnmappableCharacterException
    try {
        String s = " ¡";
        Files.write(tmpfile, Arrays.asList(s), US_ASCII);
        fail("UnmappableCharacterException expected");
    } catch (UnmappableCharacterException ignore) {
    }
}
Also used : Path(java.nio.file.Path) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ArrayList(java.util.ArrayList)

Example 8 with UnmappableCharacterException

use of java.nio.charset.UnmappableCharacterException in project jdk8u_jdk by JetBrains.

the class DiacriticTest method main.

public static void main(String[] args) throws IOException {
    if (!isMacOSX) {
        System.out.println("This test is for Mac OS X only. Passing.");
        return;
    }
    String lang = System.getenv("LANG");
    if (lang != null && !lang.contains("UTF-8")) {
        System.out.println("LANG variable set to the language that " + "does not support unicode, test passes vacuously");
        return;
    }
    File sourceFile = new File(NAME_NFC + ".java");
    String source = "public class " + NAME_NFC + " { " + "    public static void main(String args[]) {\n" + "        System.out.println(\"Success!\");\n" + "    }\n" + "}\n";
    ArrayList<String> content = new ArrayList<>();
    content.add(source);
    try {
        createFile(sourceFile, content);
    } catch (UnmappableCharacterException | InvalidPathException ipe) {
        System.out.println("The locale or file system is configured in a way " + "that prevents file creation. Real testing impossible.");
        return;
    }
    HashMap<String, String> env = new HashMap<>();
    env.put("LC_CTYPE", "UTF-8");
    TestResult tr;
    tr = doExec(env, javacCmd, NAME_NFD + ".java");
    System.out.println(tr.testOutput);
    if (!tr.isOK()) {
        System.out.println(tr);
        throw new RuntimeException("Compilation failed");
    }
    tr = doExec(env, javaCmd, "-cp", ".", NAME_NFD);
    System.out.println(tr.testOutput);
    if (!tr.isOK()) {
        System.out.println(tr);
        throw new RuntimeException("Test execution failed");
    }
}
Also used : HashMap(java.util.HashMap) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) ArrayList(java.util.ArrayList) File(java.io.File) InvalidPathException(java.nio.file.InvalidPathException)

Aggregations

UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)8 ByteBuffer (java.nio.ByteBuffer)5 CharBuffer (java.nio.CharBuffer)3 MalformedInputException (java.nio.charset.MalformedInputException)3 CharacterCodingException (java.nio.charset.CharacterCodingException)2 ArrayList (java.util.ArrayList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Charset (java.nio.charset.Charset)1 CharsetEncoder (java.nio.charset.CharsetEncoder)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 IFileInfo (org.eclipse.core.filesystem.IFileInfo)1