Java UTF8 Encode getUTF8Bytes(String string)

Here you can find the source of getUTF8Bytes(String string)

Description

Returns the UTF8 bytes for string and handles the unlikely case where UTF-8 is not supported.

License

LGPL

Parameter

Parameter Description
string the <tt>String</tt> whose bytes we'd like to obtain.

Return

string's bytes.

Declaration

public static byte[] getUTF8Bytes(String string) 

Method Source Code


//package com.java2s;
/*//from  w w  w .jav a 2 s  .  c  om
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

import java.io.*;

public class Main {
    /**
     * Returns the UTF8 bytes for <tt>string</tt> and handles the unlikely case
     * where UTF-8 is not supported.
     *
     * @param string the <tt>String</tt> whose bytes we'd like to obtain.
     *
     * @return <tt>string</tt>'s bytes.
     */
    public static byte[] getUTF8Bytes(String string) {
        try {
            return string.getBytes("UTF-8");
        } catch (UnsupportedEncodingException exc) {
            // shouldn't happen. UTF-8 is always supported, anyways ... if
            //this happens, we'll cheat
            return string.getBytes();
        }
    }
}

Related

  1. encodeUtf8(String target)
  2. encodeUTF8(String text)
  3. encodeUTF8(String text)
  4. encodeUtf8(String url)
  5. getBufferedWriter(final File outFile, final String outEncoding)
  6. getUTF8Bytes(String string)
  7. getUTF8Decoder(boolean ignoreEncodingErrors)
  8. getUTF8StringLength(String string)
  9. isUTF8(String encoding)