Android Charset Decode canEncode(char c)

Here you can find the source of canEncode(char c)

Description

Determines whether or not this platform's Charset#defaultCharset default Charset ) can encode c and then decode it back to the exact same value.

License

Open Source License

Declaration

private static boolean canEncode(char c) 

Method Source Code

//package com.java2s;
/*//from   w w w  .  jav  a2 s.  co m
 Copyright ? 2008 Brent Boyer

 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

 You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;

public class Main {
    /**
     * Determines whether or not this platform's {@link Charset#defaultCharset default Charset})
     * can encode c and then decode it back to the exact same value.
     * <p>
     * The issue with some platforms is that they cannot handle certain chars correctly.
     * For example, windoze is notorious in that its default CharSet, Cp1252, has the following strange behavior:
     * <blockquote>
     *   [Cp1252 is] Microsoft Windows variant of Latin-1, NT default. Beware.
     *   Some unexpected translations occur when you read with this default encoding,
     *   e.g. codes 128..159 are translated to 16 bit chars with bits in the high order byte on.
     *   It does not just truncate the high byte on write and pad with 0 on read.
     *   For true Latin-1 see 8859-1.
     * </blockquote>
     * See the <a href="http://mindprod.com/jgloss/encoding.html">Java glossary article on encoding</a> for more details.
     * <p>
     * This method is fairly expensive to call: you should never use it on every char that you wish to encode.
     * It is currently only used in the UnitTest class, and is private.
     */
    private static boolean canEncode(char c) {
        try {
            char[] charArray = new char[] { c };
            ByteBuffer byteBuffer = Charset.defaultCharset().newEncoder()
                    .encode(java.nio.CharBuffer.wrap(charArray));
            char[] charArrayRestored = Charset.defaultCharset()
                    .decode(byteBuffer).array();
            return (charArray[0] == charArrayRestored[0]);
        } catch (CharacterCodingException cce) {
            //d.p.s("bad char = " + ((int) c));
            return false;
        }
    }
}

Related

  1. getBytes(String input, Charset charset)