Java String Encode by Charset canEncode(String sz, String charset)

Here you can find the source of canEncode(String sz, String charset)

Description

can Encode

License

Open Source License

Declaration

public static boolean canEncode(String sz, String charset) 

Method Source Code


//package com.java2s;
/* /*from ww w .  j a v a  2s. c om*/
 * Serposcope - SEO rank checker https://serposcope.serphacker.com/
 * 
 * Copyright (c) 2016 SERP Hacker
 * @author Pierre Nogues <support@serphacker.com>
 * @license https://opensource.org/licenses/MIT MIT License
 */

import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class Main {
    public static boolean canEncode(String sz, String charset) {
        try {
            Charset cs = Charset.forName(charset);
            if (cs.canEncode()) {
                CharsetEncoder cse = cs.newEncoder();
                return cse.encode(CharBuffer.wrap(sz)) != null;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
}

Related

  1. canEncode(String charset, CharSequence text, String unicodeValue)
  2. charsetForNameOrDefault(String encoding)
  3. checkEncoder(CharsetEncoder encoder)
  4. deflate(int level, String str, Charset encoding)
  5. encode(Charset charset, String string)