Search in sources :

Example 11 with MalformedInputException

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

the class ASCIICharsetEncoderTest method testEncodeMapping.

public void testEncodeMapping() throws CharacterCodingException {
    encoder.reset();
    for (int i = 0; i <= MAXCODEPOINT; i++) {
        char[] chars = Character.toChars(i);
        CharBuffer cb = CharBuffer.wrap(chars);
        ByteBuffer bb = encoder.encode(cb);
        assertEquals(i, bb.get(0));
    }
    CharBuffer cb = CharBuffer.wrap("€");
    try {
        encoder.encode(cb);
    } catch (UnmappableCharacterException e) {
    //expected
    }
    cb = CharBuffer.wrap("�");
    try {
        encoder.encode(cb);
    } catch (MalformedInputException e) {
    //expected
    }
    ByteBuffer bb = ByteBuffer.allocate(0x10);
    cb = CharBuffer.wrap("A");
    encoder.reset();
    encoder.encode(cb, bb, false);
    try {
        encoder.encode(cb);
    } catch (IllegalStateException e) {
    //expected
    }
}
Also used : UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with MalformedInputException

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

the class CharsetDecoderTest method test_decode.

/**
	 * @tests java.nio.charset.CharsetDecoder#decode(java.nio.ByteBuffer)
	 */
public void test_decode() throws CharacterCodingException {
    // Regression for HARMONY-33
    //		ByteBuffer bb = ByteBuffer.allocate(1);
    //		bb.put(0, (byte) 77);
    //		CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
    //		decoder.onMalformedInput(CodingErrorAction.REPLACE);
    //		decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    //		decoder.decode(bb);
    // Regression for HARMONY-67
    //		byte[] b = new byte[] { (byte) 1 };
    //		ByteBuffer buf = ByteBuffer.wrap(b);
    //		CharBuffer charbuf = Charset.forName("UTF-16").decode(buf);
    //		assertEquals("Assert 0: charset UTF-16", 1, charbuf.length());
    //
    //		charbuf = Charset.forName("UTF-16BE").decode(buf);
    //		assertEquals("Assert 1: charset UTF-16BE", 0, charbuf.length());
    //
    //		charbuf = Charset.forName("UTF-16LE").decode(buf);
    //		assertEquals("Assert 2: charset UTF16LE", 0, charbuf.length());
    // Regression for HARMONY-99
    CharsetDecoder decoder2 = Charset.forName("UTF-16").newDecoder();
    decoder2.onMalformedInput(CodingErrorAction.REPORT);
    decoder2.onUnmappableCharacter(CodingErrorAction.REPORT);
    ByteBuffer in = ByteBuffer.wrap(new byte[] { 109, 97, 109 });
    try {
        decoder2.decode(in);
        fail("Assert 3: MalformedInputException should have thrown");
    } catch (MalformedInputException e) {
    //expected
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 13 with MalformedInputException

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

the class MalformedInputExceptionTest method testConstructor.

public void testConstructor() {
    MalformedInputException ex = new MalformedInputException(3);
    assertTrue(ex instanceof CharacterCodingException);
    assertNull(ex.getCause());
    assertEquals(ex.getInputLength(), 3);
    assertTrue(ex.getMessage().indexOf("3") != -1);
    ex = new MalformedInputException(-3);
    assertNull(ex.getCause());
    assertEquals(ex.getInputLength(), -3);
    assertTrue(ex.getMessage().indexOf("-3") != -1);
    ex = new MalformedInputException(0);
    assertNull(ex.getCause());
    assertEquals(ex.getInputLength(), 0);
    assertTrue(ex.getMessage().indexOf("0") != -1);
}
Also used : MalformedInputException(java.nio.charset.MalformedInputException) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 14 with MalformedInputException

use of java.nio.charset.MalformedInputException 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)

Aggregations

MalformedInputException (java.nio.charset.MalformedInputException)14 ByteBuffer (java.nio.ByteBuffer)5 CharBuffer (java.nio.CharBuffer)4 IOException (java.io.IOException)3 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)3 BufferedReader (java.io.BufferedReader)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 CharsetDecoder (java.nio.charset.CharsetDecoder)2 JSONException (com.alibaba.fastjson.JSONException)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 InvalidParameterException (java.security.InvalidParameterException)1 ArrayList (java.util.ArrayList)1 Settings (org.elasticsearch.common.settings.Settings)1 Environment (org.elasticsearch.env.Environment)1 Test (org.junit.Test)1