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 readStreamToString(InputStream in, String encoding) throws IOException {
    StringBuffer b = new StringBuffer();
    InputStreamReader r = new InputStreamReader(in, encoding);
    int c;//from w ww.  j a v a 2s .c o  m
    while ((c = r.read()) != -1) {
        b.append((char) c);
    }
    return b.toString();
}

From source file:Main.java

public static String getRandomString(int len) {
    StringBuffer sb = new StringBuffer(len);
    for (int i = 0; i < len; i++) {
        int ndx = (int) (Math.random() * ALPHA_NUM.length());
        sb.append(ALPHA_NUM.charAt(ndx));
    }//from ww w  . j  av  a2  s .co m
    return sb.toString();
}

From source file:Main.java

public static String sha512(String what_to_encode) {
    final MessageDigest sha512;
    try {/*from  ww w.  j a va  2s.c o m*/
        sha512 = MessageDigest.getInstance("SHA-512");
    } catch (NoSuchAlgorithmException e) {
        return "404";
    }
    sha512.update(what_to_encode.getBytes());
    byte byteData[] = sha512.digest();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }
    return sb.toString();
}

From source file:Main.java

private static String toHexString(byte[] bytes) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < bytes.length; i++) {
        sb.append(Integer.toHexString(bytes[i] >> 4 & 0x0f)).append(Integer.toHexString(bytes[i] & 0x0f));
    }//w w  w  .  ja  v  a 2s  .c  om
    return sb.toString();
}

From source file:Main.java

/**
 * Configures a label as if it was an hyperlink.
 * //w  w  w. j  av  a 2  s . c o  m
 * @param label
 *            the label to configure.
 */
public static void configureLabelAsHyperlink(JLabel label) {
    if (label == null) {
        return;
    }

    StringBuffer html = new StringBuffer();
    html.append("<html><font color=\"blue\"><u>");
    html.append(label.getText());
    html.append("</u></font></html>");

    label.setText(html.toString());
    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

From source file:com.mmj.app.common.util.SerialNumGenerator.java

/** ? */
public static String RandomString(int length) {
    String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    Random random = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < length; i++) {
        int num = random.nextInt(62);
        buf.append(str.charAt(num));/*ww  w. ja va 2  s  .c  o m*/
    }
    return buf.toString();
}

From source file:Main.java

public static String digest(InputStream in) throws Exception {
    MessageDigest messageDigest = null;
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    messageDigest = MessageDigest.getInstance("MD5");
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
            messageDigest.update(event.asStartElement().getName().toString().getBytes());
        } else if (event.isEndElement()) {
            messageDigest.update(event.asEndElement().getName().toString().getBytes());
        }//from   w  w  w .  j a va2  s.  c  om
    }
    StringBuffer result = new StringBuffer();
    byte[] digest = messageDigest.digest();
    for (byte b : digest) {
        result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
    }
    return result.toString();
}

From source file:Main.java

public static String join(String[] array, String separator) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < array.length; i++) {
        if (i > 0)
            buf.append(separator);/*from  w  w  w  .j  a  v  a  2s .  c o  m*/
        buf.append(array[i]);
    }
    return buf.toString();
}

From source file:StringUtils.java

/**
 *  Replaces a part of a string with a new String.
 *
 *  @param start Where in the original string the replacing should start.
 *  @param end Where the replacing should end.
 *  @param orig Original string.  Null is safe.
 *  @param text The new text to insert into the string.
 *  @return The string with the orig replaced with text.
 */// w  w  w.  j  a  va2 s.  c om
public static String replaceString(String orig, int start, int end, String text) {
    if (orig == null)
        return null;

    StringBuffer buf = new StringBuffer(orig);

    buf.replace(start, end, text);

    return buf.toString();
}

From source file:com.ibm.watson.catalyst.corpus.tfidf.Template.java

public static Pattern getTemplatePattern(JsonNode document, String before, String after) {
    JsonNode terms = document.get("words");
    List<String> words = new ArrayList<String>();
    for (JsonNode termNode : terms) {
        if (termNode.get("frequency").asInt() < 5)
            continue;
        if (termNode.get("string").asText().length() < 2)
            continue;
        Term t = new Term(termNode);
        words.add(t.getTerm());//  www .  j  a va  2  s  .c  o  m
    }

    StringBuffer sb = new StringBuffer("");
    sb.append(before).append(makeOr(words)).append(after);

    Pattern p = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
    return p;
}