Example usage for java.nio.charset CoderResult isMalformed

List of usage examples for java.nio.charset CoderResult isMalformed

Introduction

In this page you can find the example usage for java.nio.charset CoderResult isMalformed.

Prototype

public boolean isMalformed() 

Source Link

Document

Tells whether or not this object describes a malformed-input error.

Usage

From source file:net.sf.yal10n.analyzer.ExploratoryEncodingTest.java

/**
 * Test how malformed input is reported in a coder result.
 * @throws Exception any error/*from   w w w . j  a v a 2 s .c om*/
 */
@Test
public void testMalformedEncodingResult() throws Exception {
    ByteBuffer buffer = ByteBuffer.wrap(data);

    CharsetDecoder decoder = utf8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);
    CharBuffer chars = CharBuffer.allocate(100);
    CoderResult result = decoder.decode(buffer, chars, false);
    Assert.assertTrue(result.isMalformed());
}

From source file:org.openbel.framework.ws.utils.Converter.java

private static Byte encode(final CharsetEncoder encoder, final char c) {
    encoder.reset();//from  w w w  .ja v a2 s . c  om
    if (!encoder.canEncode(c)) {
        return null;
    }
    encoder.reset();
    ByteBuffer buffer = ByteBuffer.allocate(1);
    CharBuffer charBuffer = CharBuffer.allocate(1).put(c);
    charBuffer.flip();
    CoderResult result = null;

    result = encoder.encode(charBuffer, buffer, false);
    if (!result.isUnderflow()) {
        return null;
    }
    result = encoder.encode(charBuffer, buffer, true);
    if (result.isMalformed() || result.isUnmappable()) {
        return null;
    }
    result = encoder.flush(buffer);
    if (!result.isUnderflow()) {
        return null;
    }
    buffer.flip();
    return buffer.get(0);
}