Code example for CharsetDecoder

Methods: decodeflushreset

0
    public void testFlushIllegalState() throws CharacterCodingException {
        ByteBuffer in = ByteBuffer.wrap(new byte[] { 98, 98 });
        CharBuffer out = CharBuffer.allocate(5);
 
        // Illegal state: after reset. 
        decoder.reset();
        try { 
            decoder.flush(out);
            fail();
        } catch (IllegalStateException expected) {
        } 
 
        // Normal case: after decode with endOfInput is true 
        decoder.reset();
        decoder.decode(in, out, true);
        out.rewind();
        CoderResult result = decoder.flush(out);
        assertSame(result, CoderResult.UNDERFLOW);
 
        // Good state: flush twice 
        decoder.flush(out);
 
        // Illegal state: flush after decode with endOfInput is false 
        decoder.reset();
        decoder.decode(in, out, false);
        try { 
            decoder.flush(out);
            fail();
        } catch (IllegalStateException expected) {
        } 
    }