get Charset from charset name - Android java.lang

Android examples for java.lang:Character

Description

get Charset from charset name

Demo Code

/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/
//package com.java2s;

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

import android.util.Log;

public class Main {
    private static boolean Debug = true;
    private static String TAG = "CharsetUtil";

    public static java.nio.charset.Charset getCharset(String charsetName) {
        String defaultCharset = "ISO-8859-1";
        // Use the default chareset if given charset is null
        if (charsetName == null)
            charsetName = defaultCharset;
        try {//from w w w . jav  a  2  s.  c  o  m
            return java.nio.charset.Charset.forName(charsetName);
        } catch (IllegalCharsetNameException e) {
            if (Debug)
                Log.d(TAG, "Illegal charset " + charsetName
                        + ", fallback to " + defaultCharset + ": " + e);
            // Use default charset on exception
            return java.nio.charset.Charset.forName(defaultCharset);
        } catch (UnsupportedCharsetException ex) {
            if (Debug)
                Log.d(TAG, "Unsupported charset " + charsetName
                        + ", fallback to " + defaultCharset + ": " + ex);
            // Use default charset on exception
            return java.nio.charset.Charset.forName(defaultCharset);
        }

    }
}

Related Tutorials