Example usage for java.lang String getBytes

List of usage examples for java.lang String getBytes

Introduction

In this page you can find the example usage for java.lang String getBytes.

Prototype

public byte[] getBytes(Charset charset) 

Source Link

Document

Encodes this String into a sequence of bytes using the given java.nio.charset.Charset charset , storing the result into a new byte array.

Usage

From source file:Main.java

public static String utf8Encoding(String value, String sourceCharsetName) {
    try {//from   w  w w.  ja  v a  2 s.c o  m
        return new String(value.getBytes(sourceCharsetName), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

public static String utf8Encoding(String value, String sourceCharsetName) {
    try {//from   w w  w.  j a va  2 s  .  com
        return new String(value.getBytes(sourceCharsetName), UTF_8);
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

/**
 * Write string.// w w w  .  ja v a2s. co m
 *
 * @param os the os
 * @param s the s
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeString(OutputStream os, String s) throws IOException {
    byte[] b = s.getBytes("UTF-8");
    writeLong(os, b.length);
    os.write(b, 0, b.length);
}

From source file:Main.java

private static byte[] utf8(String keyString) {
    try {//from  w w  w . j av a  2 s  . com
        return keyString.getBytes("utf-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e); // utf-8 is supported!
    }
}

From source file:Main.java

public static void writeToFile(String fileName, String content) {
    try {/*from ww w.j  av a  2 s.c  o  m*/
        writeToFile(fileName, content.getBytes("UTF-8"));
    } catch (Exception e) {

    }
}

From source file:Main.java

public static String base64Encode(String s) {
    byte[] data = new byte[0];
    try {//from  w  ww  . j  a v a2 s.  c  om
        data = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } finally {
        return Base64.encodeToString(data, Base64.NO_WRAP);
    }
}

From source file:Main.java

public static byte[] getBytes(String str) {
    if (str != null) {
        try {//from ww w  .j av a  2s .c  om
            return str.getBytes(ENCODE_CHARSET);
        } catch (UnsupportedEncodingException e) {
            return str.getBytes();
        }
    } else {
        return new byte[0];
    }
}

From source file:Main.java

public static void writeString(String file, String content, String charset) {
    try {//from   www  .j  av a  2  s.c  o  m
        byte[] data = content.getBytes(charset);
        writeBytes(file, data);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:Main.java

public static byte[] conversionEncoding(byte[] text, String fromCharset, String newCharset) {
    try {// www.  j  a va 2  s  .  c o  m
        String str = new String(text, fromCharset);
        return str.getBytes(newCharset);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] generatePlainClientResponse(String userName, String userPassword) throws Exception {
    byte[] password = userPassword.getBytes("UTF8");
    byte user[] = userName.getBytes("UTF8");
    byte response[] = new byte[password.length + user.length + 2];
    int size = 0;
    response[size++] = SEPARATOR;//from  w ww .java 2  s  .  c  om
    System.arraycopy(user, 0, response, size, user.length);
    size += user.length;
    response[size++] = SEPARATOR;
    System.arraycopy(password, 0, response, size, password.length);
    return response;
}