Java Byte Array to String by Charset getBytesByCharset(String str, Charset charset)

Here you can find the source of getBytesByCharset(String str, Charset charset)

Description

get Bytes By Charset

License

Open Source License

Declaration

public static byte[] getBytesByCharset(String str, Charset charset) 

Method Source Code


//package com.java2s;

import java.nio.charset.Charset;

public class Main {
    public static byte[] getBytesByCharset(String str, Charset charset) {
        if (isNotBlank(str)) {
            return str.getBytes(charset);
        }//from  ww  w  .  j  a v a  2 s .c o  m
        return null;
    }

    /**
     * <code>
     *  StringUtils.isNotBlank("") return false;
     *  StringUtils.isNotBlank(" ") return false;
     *  StringUtils.isNotBlank(null) return false;
     *  StringUtils.isNotBlank("abc") return true;
     * </code>
     * @param str
     * @return
     */
    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * <code>
     *  StringUtils.isBlank("") return true;
     *  StringUtils.isBlank(" ") return true;
     *  StringUtils.isBlank(null) return true;
     *  StringUtils.isBlank("abc") return false;
     * </code>
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        return trimToNull(str) == null;
    }

    /**
     * <code>
     *  StringUtils.trimToNull("") return null;
     *  StringUtils.trimToNull("  ") return null;
     *  StringUtils.trimToNull(null) return null;
     *  StringUtils.trimToNull(" abc ") return "abc";
     * </code>
     * @param str
     * @return
     */
    public static String trimToNull(String str) {
        if (str == null)
            return null;
        str = str.trim();
        if ("".equals(str))
            return null;
        return str;
    }
}

Related

  1. encodingIsCorrect(byte[] bytes, String charset)
  2. getAverageBytesPerCharacter(Charset charset)
  3. getBytes(final char[] data, String charset)
  4. getBytes(final String string, final Charset charset)
  5. getBytes(String string, Charset charset)
  6. getBytesUnchecked(String string, String charsetName)
  7. getBytesUnchecked(String string, String charsetName)
  8. getChars(byte[] data, String charset)
  9. getContentAsString(byte[] content, Charset charset)