Search in sources :

Example 21 with MalformedInputException

use of java.nio.charset.MalformedInputException in project j2objc by google.

the class ASCIICharsetEncoderTest method testEncodeMapping.

/* j2objc: iconv doesn't support ASCII multi-step encoding.
    public void testMultiStepEncode() throws CharacterCodingException {
        encoder.onMalformedInput(CodingErrorAction.REPORT);
        encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
        try {
            encoder.encode(CharBuffer.wrap("\ud800\udc00"));
            fail("should unmappable");
        } catch (UnmappableCharacterException e) {
        }
        encoder.reset();
        ByteBuffer out = ByteBuffer.allocate(10);
        assertEquals(CoderResult.UNDERFLOW,
                encoder.encode(CharBuffer.wrap("\ud800"), out, true));
        assertTrue(encoder.flush(out).isMalformed());
        encoder.reset();

        out = ByteBuffer.allocate(10);
        CharBuffer buffer1 = CharBuffer.wrap("\ud800");
        CharBuffer buffer2 = CharBuffer.wrap("\udc00");
        assertSame(CoderResult.UNDERFLOW, encoder.encode(buffer1, out, false));
        // We consume the entire input buffer because we're in an underflow
        // state. We can't make a decision on whether the char in this buffer
        // is unmappable or malformed without looking at the next input buffer.
        assertEquals(1, buffer1.position());
        assertTrue(encoder.encode(buffer2, out, true).isUnmappable());
        assertEquals(0, buffer2.position());
    }
    */
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("\u0080");
    try {
        encoder.encode(cb);
    } catch (UnmappableCharacterException | MalformedInputException e) {
    // j2objc: iconv limit
    // expected
    }
    cb = CharBuffer.wrap("\ud800");
    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 22 with MalformedInputException

use of java.nio.charset.MalformedInputException in project j2objc by google.

the class CharsetDecoderTest method testDecodeByteBufferException.

public void testDecodeByteBufferException() throws CharacterCodingException, UnsupportedEncodingException {
    CharBuffer out;
    ByteBuffer in;
    String replaceStr = decoder.replacement() + getString();
    // MalformedException:
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    in = getMalformedByteBuffer();
    if (in != null) {
        try {
            CharBuffer buffer = decoder.decode(in);
            assertTrue(buffer.remaining() > 0);
            fail("should throw MalformedInputException");
        } catch (MalformedInputException e) {
        }
        decoder.reset();
        in.rewind();
        decoder.onMalformedInput(CodingErrorAction.IGNORE);
        out = decoder.decode(in);
        assertCharBufferValue(getString(), out);
        decoder.reset();
        in.rewind();
        decoder.onMalformedInput(CodingErrorAction.REPLACE);
        out = decoder.decode(in);
        assertCharBufferValue(replaceStr, out);
    }
    // Unmapped Exception:
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    in = getUnmappedByteBuffer();
    if (in != null) {
        try {
            decoder.decode(in);
            fail("should throw UnmappableCharacterException");
        } catch (UnmappableCharacterException e) {
        }
        decoder.reset();
        in.rewind();
        decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        out = decoder.decode(in);
        assertCharBufferValue(getString(), out);
        decoder.reset();
        in.rewind();
        decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        out = decoder.decode(in);
        assertCharBufferValue(replaceStr, out);
    }
    // RuntimeException
    try {
        decoder.decode(getExceptionByteArray());
        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 23 with MalformedInputException

use of java.nio.charset.MalformedInputException in project j2objc by google.

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 24 with MalformedInputException

use of java.nio.charset.MalformedInputException in project j2objc by google.

the class CharsetDecoder2Test method testInvalidDecoding.

public void testInvalidDecoding() throws IOException {
    byte[][] invalidSequences = new byte[][] { // overlong NULL
    { (byte) 0xC0, (byte) 0x80 }, // overlong ascii 'A'
    { (byte) 0xC0, (byte) 0xC1 }, // overlong "/../"
    { (byte) 0x2F, (byte) 0xC0, (byte) 0xAE, (byte) 0x2E, (byte) 0x2F }, // Invalid encoding 2r11111000 (sequence too long)
    { (byte) 0xF8 }, // Invalid encoding 2r10000000 (sequence too short)
    { (byte) 0x80 } };
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    /*
         * When bytebuffer has a backing array...
         */
    for (byte[] bytes : invalidSequences) {
        try {
            CharBuffer cb = decoder.decode(ByteBuffer.wrap(bytes));
            fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
        } catch (MalformedInputException expected) {
        }
    }
    /*
         * When bytebuffer has _not_ got a backing array...
         */
    for (byte[] bytes : invalidSequences) {
        try {
            ByteBuffer bb = ByteBuffer.allocateDirect(8);
            bb.put(bytes).flip();
            CharBuffer cb = decoder.decode(bb);
            fail("No exception thrown on " + Arrays.toString(bytes) + " '" + cb + "'");
        } catch (MalformedInputException expected) {
        }
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) MalformedInputException(java.nio.charset.MalformedInputException) ByteBuffer(java.nio.ByteBuffer)

Example 25 with MalformedInputException

use of java.nio.charset.MalformedInputException in project j2objc by google.

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)

Aggregations

MalformedInputException (java.nio.charset.MalformedInputException)41 IOException (java.io.IOException)14 ByteBuffer (java.nio.ByteBuffer)12 CharBuffer (java.nio.CharBuffer)9 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)9 CharsetDecoder (java.nio.charset.CharsetDecoder)7 BufferedReader (java.io.BufferedReader)6 Path (java.nio.file.Path)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)5 Charset (java.nio.charset.Charset)5 Test (org.junit.Test)4 CharacterCodingException (java.nio.charset.CharacterCodingException)3 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)3 JSONException (com.alibaba.fastjson.JSONException)2 BufferedWriter (java.io.BufferedWriter)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 UncheckedIOException (java.io.UncheckedIOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2