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() 

Source Link

Document

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Usage

From source file:Main.java

public static byte[] bitsToBytes(String bits) {
    BinaryCodec binaryCodec = new BinaryCodec();
    return binaryCodec.decode(bits.getBytes());
}

From source file:com.creditcloud.common.taglib.Functions.java

public static String base64(String value) {
    return Base64.encodeBase64URLSafeString(value.getBytes());
}

From source file:Main.java

private static void killProcess(String packageName) {
    OutputStream out = process.getOutputStream();
    String cmd = "am force-stop " + packageName + " \n";
    try {/*from  w w w  .  j a va2  s . c  om*/
        out.write(cmd.getBytes());
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Object xmlStrToObj(String inputStr, Class inputClass) throws Exception {
    byte[] byteArray = inputStr.getBytes();
    ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
    XMLInputFactory input = XMLInputFactory.newFactory();
    XMLStreamReader reader = input.createXMLStreamReader(byteStream);

    JAXBContext context = JAXBContext.newInstance(inputClass);
    Unmarshaller unmarsh = context.createUnmarshaller();
    Object result = unmarsh.unmarshal(reader);

    return result;
}

From source file:Main.java

public static String stringToMD5(String str) {

    try {/*  w  ww.  jav a 2  s . c  o  m*/
        byte[] strTemp = str.getBytes();
        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
        mdTemp.update(strTemp);
        return toHexString(mdTemp.digest());
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static List<byte[]> hexToListBytesArray(String s) throws DecoderException {
    List<String> listHex = new ArrayList<String>();
    if (s.length() == 2) {
        listHex.add(s);/*from www.j  a v a 2 s . c  o  m*/
    } else if (s.length() > 2 && ((s.length() % 2) == 0)) {
        for (int i = 0; i < s.length(); i++) {
            String ss = s.substring(i, i + 2);
            i = i + 1;
            listHex.add(ss);
        }
    } else {
        return null;
    }
    Hex hex = new Hex();

    List<byte[]> listByteArray = new ArrayList<byte[]>();
    for (String sh : listHex) {
        listByteArray.add(hex.decode(sh.getBytes()));
    }
    return listByteArray;
}

From source file:Main.java

public static <T> T unmarshal(Class<T> clazz, String xml) throws JAXBException {
    return unmarshal(clazz, new ByteArrayInputStream(xml.getBytes()));
}

From source file:Main.java

public static String trimStrInBytes(String strings, int Dstbytes) {
    byte[] origByte = strings.getBytes();
    if (origByte.length <= Dstbytes)
        return strings;

    System.out.println("old string" + strings + " len:" + strings.length() + " dstByte:" + Dstbytes);
    final int len = Dstbytes;
    String oldString = new String(origByte, 0, len);
    System.out.println("before trime string:" + oldString + " len:" + oldString.length());
    int oldLen = oldString.length();
    int newLen = 0;

    for (int i = 1; i <= len; i++) {
        String newString = new String(origByte, 0, len - i);
        System.out.println("new string:" + newString + " len:" + newString.length());
        newLen = newString.length();//from   w  w w.j  a  v a2  s.  c  om
        System.out.println("old:" + oldLen + " new:" + newLen);
        if (oldLen > newLen) {
            String trimmed = new String(origByte, 0, len - i);
            String sub = strings.substring(0, trimmed.length() + 1);
            if (sub.getBytes().length <= len)
                return sub;
            //                        System.out.println("trimmed string:"+new String(origByte, 0, len-i));
            return trimmed;
        }
    }
    return "";
}

From source file:Main.java

public static String decodeToFile(File desFile, String encodedString) {
    try {/*from w  w  w .ja  va 2s.  c  o m*/
        byte[] decodeBytes = Base64.decode(encodedString.getBytes(), Base64.NO_WRAP);
        FileOutputStream fos = new FileOutputStream(desFile);
        fos.write(decodeBytes);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] compress(String str) {
    if (str == null || str.length() == 0) {
        return str.getBytes();
    }/*from ww  w  .j  a  v a  2s .co m*/
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        final GZIPOutputStream gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes());
        gzip.close();
    } catch (final IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}