Java InputStream Read by Charset convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)

Here you can find the source of convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet)

Description

Converts the input text between the two specified character sets.

License

Apache License

Parameter

Parameter Description
input The input text as a byte array.
fromCharset The source character set.
toCharSet The destination character set.

Return

The input text, converted from the source to the destination character set.

Declaration

public static byte[] convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import java.util.Arrays;

public class Main {
    /**//from  w  w w  .  j a v a  2s .co m
     * Converts the input text between the two specified character sets.
     * @param input The input text as a byte array.
     * @param fromCharset The source character set.
     * @param toCharSet The destination character set.
     * @return The input text, converted from the source to the destination character set.
     */
    public static byte[] convertToCharacterSet(byte[] input, Charset fromCharset, Charset toCharSet) {
        if (input == null) {
            return null;
        }

        CharBuffer decodedData = fromCharset.decode(ByteBuffer.wrap(input));
        ByteBuffer encodedData = toCharSet.encode(decodedData);
        return Arrays.copyOf(encodedData.array(), encodedData.limit());
    }
}

Related

  1. collectStream(InputStream stream, Charset charset)
  2. consume(InputStream stream, Charset encoding)
  3. convertEncoding(Charset output_charset, String input_string)
  4. convertFromUnicode(String input, String targetCharset)
  5. convertStreamToString(final InputStream is, final Charset charset)
  6. copyToString(InputStream in, Charset charset)
  7. createInput(String s, String charsetName)
  8. createZipInputStream(InputStream inStream, Charset charset)
  9. forceEncoding(String inputString, String targetCharset)