Example usage for java.nio.charset UnmappableCharacterException getMessage

List of usage examples for java.nio.charset UnmappableCharacterException getMessage

Introduction

In this page you can find the example usage for java.nio.charset UnmappableCharacterException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the message.

Usage

From source file:org.eclipse.californium.proxy.HttpTranslator.java

/**
 * Change charset./*  w  w w  . j a va2  s  . c  om*/
 * 
 * @param payload
 *            the payload
 * @param fromCharset
 *            the from charset
 * @param toCharset
 *            the to charset
 * 
 * 
 * @return the byte[] * @throws TranslationException the translation
 *         exception
 */
private static byte[] changeCharset(byte[] payload, Charset fromCharset, Charset toCharset)
        throws TranslationException {
    try {
        // decode with the source charset
        CharsetDecoder decoder = fromCharset.newDecoder();
        CharBuffer charBuffer = decoder.decode(ByteBuffer.wrap(payload));
        decoder.flush(charBuffer);

        // encode to the destination charset
        CharsetEncoder encoder = toCharset.newEncoder();
        ByteBuffer byteBuffer = encoder.encode(charBuffer);
        encoder.flush(byteBuffer);
        payload = byteBuffer.array();
    } catch (UnmappableCharacterException e) {
        // thrown when an input character (or byte) sequence is valid but
        // cannot be mapped to an output byte (or character) sequence.
        // If the character sequence starting at the input buffer's current
        // position cannot be mapped to an equivalent byte sequence and the
        // current unmappable-character
        LOGGER.finer("Charset translation: cannot mapped to an output char byte: " + e.getMessage());
        return null;
    } catch (CharacterCodingException e) {
        LOGGER.warning("Problem in the decoding/encoding charset: " + e.getMessage());
        throw new TranslationException("Problem in the decoding/encoding charset", e);
    }

    return payload;
}