Java UTF8 Encode encodeUTF8(String str)

Here you can find the source of encodeUTF8(String str)

Description

Convenience method for encoding a UTF-8 byte string from a Java String .

License

Open Source License

Parameter

Parameter Description
str the string to encode.

Return

a UTF-8 encoded sequence containing the contents of 'str'.

Declaration

public static byte[] encodeUTF8(String str) 

Method Source Code


//package com.java2s;
/*-// w  w  w . j a  v a 2  s  . co m
 * jFUSE - FUSE bindings for Java
 * Copyright (C) 2008-2009  Erik Larsson <erik82@kth.se>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Convenience method for encoding a UTF-8 byte string from a Java
     * {@link String}.
     *
     * @param str the string to encode.
     * @return a UTF-8 encoded sequence containing the contents of 'str'.
     */
    public static byte[] encodeUTF8(String str) {
        try {
            // TODO: Encode properly and deal with errors.
            return str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8 charset not found! This should not happen...", e);
        }
    }
}

Related

  1. encodeStringUtf8(String text)
  2. encodeToUTF8(String str)
  3. encodeUtf8(byte[] bytes)
  4. encodeUTF8(byte[] bytes)
  5. encodeUTF8(String str)
  6. encodeUtf8(String string)
  7. encodeUtf8(String target)
  8. encodeUTF8(String text)
  9. encodeUTF8(String text)