Java Byte Array to String by Charset getBytesUnchecked(String string, String charsetName)

Here you can find the source of getBytesUnchecked(String string, String charsetName)

Description

Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte array.

License

Open Source License

Parameter

Parameter Description
string the String to encode, may be <code>null</code>
charsetName The name of a required java.nio.charset.Charset

Exception

Parameter Description
IllegalStateException Thrown when a UnsupportedEncodingException is caught,which should never happen for a required charset name.

Return

encoded bytes, or null if the input string was null

Declaration

public static byte[] getBytesUnchecked(String string, String charsetName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from  w  ww .j  a v a  2  s  .  c  o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Encodes the given string into a sequence of bytes using the named
     * charset, storing the result into a new byte array.
     * <p>
     * This method catches {@link UnsupportedEncodingException} and rethrows it
     * as {@link IllegalStateException}, which should never happen for a
     * required charset name. Use this method when the encoding is required to
     * be in the JRE.
     * </p>
     * 
     * @param string
     *            the String to encode, may be <code>null</code>
     * @param charsetName
     *            The name of a required {@link java.nio.charset.Charset}
     * @return encoded bytes, or <code>null</code> if the input string was
     *         <code>null</code>
     * @throws IllegalStateException
     *             Thrown when a {@link UnsupportedEncodingException} is caught,
     *             which should never happen for a required charset name.
     * @see CharEncoding
     * @see String#getBytes(String)
     */
    public static byte[] getBytesUnchecked(String string, String charsetName) {
        if (string == null) {
            return null;
        }
        try {
            return string.getBytes(charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getAverageBytesPerCharacter(Charset charset)
  2. getBytes(final char[] data, String charset)
  3. getBytes(final String string, final Charset charset)
  4. getBytes(String string, Charset charset)
  5. getBytesByCharset(String str, Charset charset)
  6. getBytesUnchecked(String string, String charsetName)
  7. getChars(byte[] data, String charset)
  8. getContentAsString(byte[] content, Charset charset)
  9. getMaxBytes(String string, int maxBytes, Charset charSet)