Java Byte Array from getBytesFromText(String text, String charset)

Here you can find the source of getBytesFromText(String text, String charset)

Description

Converts a text into a byte array using the given charset, if possible.

License

Mozilla Public License

Parameter

Parameter Description
text The text to be converted.
charset The charset to be used for converting.

Declaration

public static final byte[] getBytesFromText(String text, String charset) 

Method Source Code


//package com.java2s;
/*/*from   w ww .  j av  a  2s  .  c  om*/
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Converts a text into a byte array using the given charset, if possible.
     * 
     * @param text     The text to be converted.
     * @param charset  The charset to be used for converting.
     * @return
     */
    public static final byte[] getBytesFromText(String text, String charset) {

        try {
            return text.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
        }

        return text.getBytes();
    }
}

Related

  1. getBytesFromObject(Serializable data)
  2. getBytesFromObject(Serializable obj)
  3. getBytesFromResource(String resource)
  4. getBytesFromStream(int length, ByteArrayInputStream bais)
  5. getBytesFromString(final String str, final int length, final String coding)
  6. getBytesInCodePage(final String string, final int codepage)
  7. getBytesInUsAscii(String s)
  8. getBytesISO88591(String s)
  9. getBytesLengthOfEncoding(String encoding, String str)