Java String Encode by Charset canEncode(String charset, CharSequence text, String unicodeValue)

Here you can find the source of canEncode(String charset, CharSequence text, String unicodeValue)

Description

can Encode

License

Open Source License

Parameter

Parameter Description
charset non null
text non null
unicodeValue a parameter

Return

null if text could be encoded, error message otherwise

Declaration

public static String canEncode(String charset, CharSequence text, String unicodeValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Andrei Loskutov./*from   www  .j  a  v a 2 s .  c o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 * Contributor:  Andrei Loskutov - initial API and implementation
 *******************************************************************************/

import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;

public class Main {
    /**
     *
     * @param charset non null
     * @param text non null
     * @param unicodeValue
     * @return null if text could be encoded, error message otherwise
     */
    public static String canEncode(String charset, CharSequence text, String unicodeValue) {
        Charset cs;
        try {
            cs = Charset.forName(charset);
        } catch (IllegalCharsetNameException e) {
            return "Charset name '" + charset + "' is illegal.";
        } catch (UnsupportedCharsetException e) {
            return "Charset '" + charset + "' is not supported.";
        }
        if (cs.canEncode() && cs.newEncoder().canEncode(text)) {
            return null;
        }
        return "Charset '" + charset + "' does not support encoding for \\u" + unicodeValue + ".";
    }
}

Related

  1. canEncode(String sz, String charset)
  2. charsetForNameOrDefault(String encoding)
  3. checkEncoder(CharsetEncoder encoder)
  4. deflate(int level, String str, Charset encoding)