Java Byte Array to String by Charset convertCharset(byte[] content, Charset fromCharset, Charset toCharset)

Here you can find the source of convertCharset(byte[] content, Charset fromCharset, Charset toCharset)

Description

convert Charset

License

Apache License

Declaration

public static byte[] convertCharset(byte[] content, 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 {
    public static byte[] convertCharset(byte[] content, Charset fromCharset, Charset toCharset) {
        if (content == null || content.length == 0)
            return null;
        if (fromCharset == null || toCharset == null) {
            throw new IllegalArgumentException("Charset cannot be null!");
        }//from   ww w .ja v a 2  s . c  o  m
        CharBuffer inputContent = fromCharset.decode(ByteBuffer.wrap(content));
        ByteBuffer result = toCharset.encode(inputContent);
        return Arrays.copyOf(result.array(), result.limit());
    }
}

Related

  1. byteToString(byte[] b, String charset)
  2. byteToString(Charset charset, byte[] data)
  3. changeEncoding(byte[] byteArray, Charset charsetFrom, Charset charsetTo)
  4. charSequence2Bytes(CharSequence sb, Charset charset)
  5. charSetForBOM(byte[] buffer, int offset)
  6. convertToBytes(final String str, final Charset charset)
  7. convertToChars(final byte[] bytes, final Charset charset)
  8. convertToString(byte[] bytes, Charset charset)
  9. convertToString(final byte[] bytes, final Charset charset)