Java UTF from toUTF(String str)

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

Description

to UTF

License

Open Source License

Declaration

public static byte[] toUTF(String str) throws RuntimeException 

Method Source Code


//package com.java2s;
/*//from  w w  w .  j  a  v a 2 s  . co  m
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import java.io.UnsupportedEncodingException;

public class Main {
    public static final String UTF8 = "UTF-8";
    private static final byte[] NO_BYTES = {};

    public static byte[] toUTF(String str) throws RuntimeException {
        if (str == null) {
            return null;
        }

        if (str.length() == 0) {
            return NO_BYTES;
        }

        try {
            return str.getBytes(UTF8);
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
    }
}

Related

  1. toUTF(String inpara)
  2. ToUTF(String s)
  3. toUTF(String... str)
  4. toUTF8(byte[] outputBuffer, String string, char[] workingBuffer)
  5. toUtf8(final String string)
  6. toUtf8(String hex)