Search in sources :

Example 21 with CoderResult

use of java.nio.charset.CoderResult in project randomizedtesting by randomizedtesting.

the class WriterOutputStream method processInput.

/**
     * Decode the contents of the input ByteBuffer into a CharBuffer.
     * 
     * @param endOfInput indicates end of input
     * @throws IOException if an I/O error occurs
     */
private void processInput(boolean endOfInput) throws IOException {
    // Prepare decoderIn for reading
    decoderIn.flip();
    CoderResult coderResult;
    while (true) {
        coderResult = decoder.decode(decoderIn, decoderOut, endOfInput);
        if (coderResult.isOverflow()) {
            flushOutput();
        } else if (coderResult.isUnderflow()) {
            break;
        } else {
            // so we should not get here.
            throw new IOException("Unexpected coder result");
        }
    }
    // Discard the bytes that have been read
    decoderIn.compact();
}
Also used : IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 22 with CoderResult

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

the class CharsetEncoderTest method implTestEncodeCharBufferByteBufferbooleanException.

protected void implTestEncodeCharBufferByteBufferbooleanException(boolean endOfInput) throws CharacterCodingException {
    ByteBuffer out = ByteBuffer.allocate(100);
    // MalformedException:
    CharBuffer in = getMalformedCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        encoder.reset();
        CoderResult r = encoder.encode(in, out, endOfInput);
        assertTrue(r.isMalformed());
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.IGNORE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytes);
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onMalformedInput(CodingErrorAction.REPLACE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
    } else {
        System.out.println("Cannot find malformed char buffer for " + cs.name());
    }
    // Unmapped Exception:
    in = getUnmapCharBuffer();
    encoder.onMalformedInput(CodingErrorAction.REPORT);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    if (in != null) {
        encoder.reset();
        out.clear();
        assertTrue(encoder.encode(in, out, endOfInput).isUnmappable());
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytes);
        encoder.reset();
        out.clear();
        in.rewind();
        encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
        assertSame(CoderResult.UNDERFLOW, encoder.encode(in, out, endOfInput));
        assertCodingErrorAction(endOfInput, out, in, unibytesWithRep);
    } else {
        System.out.println("Cannot find unmapped char buffer for " + cs.name());
    }
    // RuntimeException
    try {
        encoder.encode(getExceptionCharBuffer());
        fail("should throw runtime exception");
    } catch (RuntimeException e) {
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 23 with CoderResult

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

the class CharsetDecoderTest method test_ByteArray_decode_with_offset.

// http://code.google.com/p/android/issues/detail?id=4237
public void test_ByteArray_decode_with_offset() throws Exception {
    CharsetDecoder decoder = Charset.forName("UTF-16").newDecoder();
    byte[] arr = encode("UTF-16", "Android");
    arr = prependByteToByteArray(arr, new Integer(1).byteValue());
    int offset = 1;
    ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
    CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
    decoder.reset();
    CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
    assertFalse(coderResult.toString(), coderResult.isError());
    decoder.flush(outBuffer);
    outBuffer.flip();
    assertEquals("Android", outBuffer.toString().trim());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 24 with CoderResult

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

the class CharsetEncoderTest method testSurrogatePairAllAtOnce.

public void testSurrogatePairAllAtOnce() throws Exception {
    // okay: surrogate pair seen all at once is decoded to U+20b9f.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '�', '�' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, bb.position());
    assertEquals((byte) 0x00, bb.get(0));
    assertEquals((byte) 0x02, bb.get(1));
    assertEquals((byte) 0x0b, bb.get(2));
    assertEquals((byte) 0x9f, bb.get(3));
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 25 with CoderResult

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

the class OutputStreamWriter method drainEncoder.

private void drainEncoder() throws IOException {
    // Strictly speaking, I think it's part of the CharsetEncoder contract that you call
    // encode with endOfInput true before flushing. Our ICU-based implementations don't
    // actually need this, and you'd hope that any reasonable implementation wouldn't either.
    // CharsetEncoder.encode doesn't actually pass the boolean through to encodeLoop anyway!
    CharBuffer chars = CharBuffer.allocate(0);
    while (true) {
        CoderResult result = encoder.encode(chars, bytes, true);
        if (result.isError()) {
            result.throwException();
        } else if (result.isOverflow()) {
            flushBytes(false);
            continue;
        }
        break;
    }
    // Some encoders (such as ISO-2022-JP) have stuff to write out after all the
    // characters (such as shifting back into a default state). In our implementation,
    // this is actually the first time ICU is told that we've run out of input.
    CoderResult result = encoder.flush(bytes);
    while (!result.isUnderflow()) {
        if (result.isOverflow()) {
            flushBytes(false);
            result = encoder.flush(bytes);
        } else {
            result.throwException();
        }
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)123 CharBuffer (java.nio.CharBuffer)81 ByteBuffer (java.nio.ByteBuffer)35 IOException (java.io.IOException)25 CharsetDecoder (java.nio.charset.CharsetDecoder)25 Charset (java.nio.charset.Charset)15 CharacterCodingException (java.nio.charset.CharacterCodingException)13 CharsetEncoder (java.nio.charset.CharsetEncoder)13 BufferUnderflowException (java.nio.BufferUnderflowException)3 BufferOverflowException (java.nio.BufferOverflowException)2 CloseReason (javax.websocket.CloseReason)2 ArrayDecoder (sun.nio.cs.ArrayDecoder)2 ArrayEncoder (sun.nio.cs.ArrayEncoder)2 JSONException (com.alibaba.fastjson.JSONException)1 SimpleDiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition)1 ParseException (io.druid.java.util.common.parsers.ParseException)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Cipher (javax.crypto.Cipher)1