Example usage for java.lang StringBuilder toString

List of usage examples for java.lang StringBuilder toString

Introduction

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

Prototype

@Override
    @HotSpotIntrinsicCandidate
    public String toString() 

Source Link

Usage

From source file:Main.java

private static String convertToHexString(byte[] array) {
    StringBuilder hexStr = new StringBuilder();
    for (int i = 0; i < array.length; ++i) {
        hexStr.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
    }//  w  ww . j av  a  2  s .  c  o  m
    return hexStr.toString();
}

From source file:Main.java

/**
 * Get the port for mesh usage(For mesh require port hex uppercase).
 * //w ww. j  a va  2  s  .  c  o m
 * @param port the port
 * @return the port for mesh usage
 */
public static String getPortForMesh(int port) {
    String portHexUppercase = Integer.toHexString(port).toUpperCase(Locale.US);
    int numberOfZero = 4 - portHexUppercase.length();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numberOfZero; i++) {
        sb.append("0");
    }
    sb.append(portHexUppercase);
    return sb.toString();
}

From source file:Main.java

public static String macToString(byte[] mac) {
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < mac.length; i++) {
        b.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
    }/*from   w  w  w.  jav a2  s .  com*/

    return b.toString();
}

From source file:Main.java

/**
 * read file//  www. j  a  v  a2  s .  c  o m
 *
 * @param charsetName The name of a supported {@link java.nio.charset.Charset
 *                    </code>charset<code>}
 * @return if file not exist, return null, else return content of file
 * @throws RuntimeException if an error occurs while operator BufferedReader
 */
public static String readFile(File file, String charsetName) {
    StringBuilder fileContent = new StringBuilder("");
    if (file == null || !file.isFile()) {
        return fileContent.toString();
    }

    BufferedReader reader = null;
    try {
        InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);
        reader = new BufferedReader(is);
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!fileContent.toString().equals("")) {
                fileContent.append("\r\n");
            }
            fileContent.append(line);
        }
        reader.close();
    } catch (IOException e) {
        throw new RuntimeException("IOException occurred. ", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                throw new RuntimeException("IOException occurred. ", e);
            }
        }
    }
    return fileContent.toString();
}

From source file:Main.java

public static String getHash(String stringToHash) {

    MessageDigest digest = null;/*from   w w  w  .jav a2 s  .  c o  m*/
    try {
        digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    byte[] result = null;

    try {
        result = digest.digest(stringToHash.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    StringBuilder sb = new StringBuilder();

    for (byte b : result) {
        sb.append(String.format("%02X", b));
    }

    String messageDigest = sb.toString();
    return messageDigest;
}

From source file:Main.java

private static String sha256Hex(final String filePath) throws NoSuchAlgorithmException, IOException {
    final InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
    final MessageDigest md = MessageDigest.getInstance("SHA-256");

    final byte[] dataBytes = new byte[1024];

    int nread;/*from  www. j a  v a2 s.c o  m*/
    while ((nread = fis.read(dataBytes)) != -1)
        md.update(dataBytes, 0, nread);
    final byte[] mdbytes = md.digest();

    final StringBuilder sb = new StringBuilder();
    for (final byte b : mdbytes)
        sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));

    return sb.toString();
}

From source file:Main.java

public static String getAtLeastOne(StringTokenizer stringT) {
    StringBuilder text = new StringBuilder(stringT.nextToken());
    while (stringT.hasMoreTokens()) {
        text.append(' ');
        text.append(stringT.nextToken());
    }//  w w w. ja  va2s  . c o m
    return text.toString();
}

From source file:cn.ctyun.amazonaws.util.StringUtils.java

public static Boolean toBoolean(StringBuilder value) {
    return Boolean.getBoolean(value.toString());
}

From source file:Main.java

/**
 * Prepare and sanitize a string to be used as parameter for a command
 * @param s//w ww . j  av a2 s. c om
 * @return A string safe to use as parameter for a command
 */
public static String shellEscape(String s) {
    StringBuilder sb = new StringBuilder(s.length() + 200);
    sb.append("'");
    sb.append(s.replace("'", "\\'").replace("\n", "\\\n"));
    sb.append("'");
    return sb.toString();
}