Example usage for java.lang StringBuffer toString

List of usage examples for java.lang StringBuffer toString

Introduction

In this page you can find the example usage for java.lang StringBuffer toString.

Prototype

@Override
    @HotSpotIntrinsicCandidate
    public synchronized String toString() 

Source Link

Usage

From source file:Main.java

public static String getMd5String(String md5) {
    try {/*w w w  . ja va 2 s . c o m*/
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String byte2String(byte[] bytes) {
    StringBuffer buff = new StringBuffer();
    for (int i = 0; i < bytes.length; ++i) {
        if (i != 0) {
            buff.append(", ");
        }/*from   www.  j  a  va 2 s .  co m*/
        buff.append(bytes[i]);
    }
    return buff.toString();
}

From source file:Main.java

public static String getTreadStackTrace(Thread t) {
    if (java14) {
        return "";
    }//from   w  w  w  .j  a v  a  2s  . c o m
    try {
        // Java 1.5 thread.getStackTrace();
        Method m = t.getClass().getMethod("getStackTrace", null);

        StackTraceElement[] trace = (StackTraceElement[]) m.invoke(t, null);
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < trace.length; i++) {
            b.append("\n\tat ").append(trace[i]);
        }
        return b.toString();
    } catch (Throwable e) {
        java14 = true;
        return "";
    }
}

From source file:Main.java

private static String getHexString(byte[] data) {
    StringBuffer stringBuffer = new StringBuffer();
    for (int i = 0; i < data.length; i++) {
        stringBuffer.append(Integer.toString((data[i] & 0xff) + 0x100, 16).substring(1));
    }/*from  w ww. j av  a  2s. c  o m*/
    return stringBuffer.toString();
}

From source file:Main.java

public static String removeCharInc(String str, String inc) {
    if (str == null) {
        return str;
    }//from   www.j  ava2s  .c o m
    char[] carr = str.toCharArray();
    StringBuffer buf = new StringBuffer();
    for (int idx = 0; idx < carr.length; idx++) {
        if (inc.indexOf(carr[idx]) != -1) {
            buf.append(carr[idx]);
        }
    }
    return buf.toString();
}

From source file:Main.java

public static String getRandomKey(int length) {
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer sb = new StringBuffer();

    for (int i = 0; i < length; ++i) {
        int number = random.nextInt(str.length() - 1);//[0,62)

        sb.append(str.charAt(number));//from   ww w.  j a v a  2 s .c om
    }
    return sb.toString();
}

From source file:Main.java

public static String calculateMD5(String string, String encoding) {
    try {//from ww w.j a  va  2s . c  om
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] array = md.digest(string.getBytes(Charset.forName(encoding)));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:Main.java

public static String toHexString(byte[] bytes) {
    StringBuffer buffer = new StringBuffer();
    for (byte i : bytes) {
        buffer.append(Character.forDigit((i >> 4) & 0xF, 16));
        buffer.append(Character.forDigit((i & 0xF), 16));
    }//from   w ww  . j  a v a2  s.co m

    return buffer.toString();
}

From source file:Main.java

private static String arrayToString(JSONArray ar) throws JSONException {
    StringBuffer bfr = new StringBuffer();
    for (int i = 0; i < ar.length(); i++) {
        bfr.append(ar.getString(i));/*  w  w  w .  jav a  2s . c o  m*/
        if (i != ar.length() - 1)
            bfr.append(",");
    }
    return bfr.toString();
}

From source file:com.billing.ng.crypto.util.HashUtils.java

/**
 * Generates a string of random alphanumeric characters of the given length. The generated salt can be
 * used to add entropy to a hashing algorithm or token generator, thus making the result harder to crack.
 *
 * @param length length of salt string to generate
 * @return salt string/* w  w w.j a  v  a 2  s.com*/
 */
public static String generateHashSalt(int length) {
    SecureRandom random = new SecureRandom();

    StringBuffer salt = new StringBuffer(length);
    for (int i = 0; i < length; i++)
        salt.append(chars[random.nextInt(chars.length)]);

    return salt.toString();
}