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 void print(OutputStream out, String str) throws IOException {
    out.write(str.getBytes("UTF-8")); //$NON-NLS-1$
}

From source file:Main.java

public static void writeFile(String data, File file) {
    writeFile(data.getBytes(Charset.forName("UTF-8")), file);
}

From source file:Main.java

public static void writeUTF(DataOutputStream os, String str) throws IOException {
    byte[] data = str.getBytes(CHARSET);
    writeShort(os, (short) data.length);
    os.write(data);//from   w  w  w  .  j  av a  2  s.  co  m
}

From source file:com.lantos.base64password.CreateAndValidatePassword.java

public static String base64Encode(String string) {
    byte[] tmp = string.getBytes(Charset.forName("UTF-8"));
    String newString = new org.apache.commons.codec.binary.Base64().encodeAsString(tmp);
    return newString;
}

From source file:Main.java

/**
 * encodes the given charArray as UTF8 encoded byte array.
 * @param charArray      char array to be encoded
 * @return            UTF8 encoded byte array 
 * @throws UnsupportedEncodingException   if UTF8 encoding is not supported
 *//*from w  w w .  ja  va 2  s. c  o  m*/
public static byte[] utf8Encoder(char[] charArray) throws UnsupportedEncodingException {
    String javaString = new String(charArray);
    return javaString.getBytes("UTF8");
}

From source file:Main.java

private static byte[] getBytes(String s) {
    try {/*from   w ww. j a va 2s .  co  m*/
        return s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        return s.getBytes();
    }
}

From source file:Main.java

public static byte[] strToBytes(String str, String charset) {
    try {/*from w  w  w.  j  a v a2s .  c  om*/
        return str.getBytes(charset);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] stringToBytes(String string) {
    try {/*from  w  ww .  j a  v  a2 s  .  c  o m*/
        return string.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("UTF8 not supported", e);
    }
}

From source file:Main.java

public static String encode(String plain, Charset charset) {
    return Base64.encodeToString(plain.getBytes(charset), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] toBytes(String base64) {
    try {//from www  .  j  a v a  2 s.  com
        return Base64.decode(base64.getBytes(CHARSET), Base64.NO_WRAP);
    } catch (Exception e) {
        return null;
    }
}