Here you can find the source of getBytesUnchecked(String string, String charsetName)
Parameter | Description |
---|---|
string | the String to encode, may be <code>null</code> |
charsetName | The name of a required java.nio.charset.Charset |
Parameter | Description |
---|---|
IllegalStateException | Thrown when a UnsupportedEncodingException is caught,which should never happen for a required charset name. |
null
if the input string was null
public static byte[] getBytesUnchecked(String string, String charsetName)
//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; } } }