Here you can find the source of toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName)
public static byte[] toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName) throws UnsupportedEncodingException
//package com.java2s; /*/*from w w w . j a va2 s . com*/ * Copyright 2013 Primeton. * * Licensed 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. */ import java.io.UnsupportedEncodingException; import java.nio.CharBuffer; import java.nio.charset.Charset; public class Main { public static byte[] toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName) throws UnsupportedEncodingException { if (bytes == null) { throw new IllegalArgumentException("bytes is null!"); } if (toCharsetName == null || toCharsetName.trim().length() == 0) { throw new IllegalArgumentException("toCharsetName is null!"); } if (fromCharsetName == null || fromCharsetName.trim().length() == 0) { fromCharsetName = System.getProperty("file.encoding"); } return new String(bytes, fromCharsetName).getBytes(toCharsetName); } public static String toCharSet(String str, String charsetName) throws UnsupportedEncodingException { if (str == null || str.trim().length() == 0) { throw new IllegalArgumentException("str is null!"); } if (charsetName == null || charsetName.trim().length() == 0) { throw new IllegalArgumentException("charsetName is null!"); } return new String(Charset.forName(charsetName).encode(CharBuffer.wrap(str)).array(), charsetName); } }