Example usage for java.util Formatter toString

List of usage examples for java.util Formatter toString

Introduction

In this page you can find the example usage for java.util Formatter toString.

Prototype

public String toString() 

Source Link

Document

Returns the result of invoking toString() on the destination for the output.

Usage

From source file:models.Attachment.java

/**
 * Moves a file to the Upload Directory.
 *
 * This method is used to move a file stored in temporary directory by
 * PlayFramework to the Upload Directory managed by Yobi.
 *
 * @param file/*  w  ww. ja  v a  2s. co m*/
 * @return SHA1 hash of the file
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
private static String moveFileIntoUploadDirectory(File file) throws NoSuchAlgorithmException, IOException {
    // Compute sha1 checksum.
    MessageDigest algorithm = MessageDigest.getInstance("SHA1");
    byte buf[] = new byte[10240];
    FileInputStream fis = new FileInputStream(file);
    for (int size = 0; size >= 0; size = fis.read(buf)) {
        algorithm.update(buf, 0, size);
    }
    Formatter formatter = new Formatter();
    for (byte b : algorithm.digest()) {
        formatter.format("%02x", b);
    }
    String hash = formatter.toString();
    formatter.close();
    fis.close();

    // Store the file.
    // Before do that, create upload directory if it doesn't exist.
    File uploads = new File(uploadDirectory);
    uploads.mkdirs();
    if (!uploads.isDirectory()) {
        throw new NotDirectoryException("'" + file.getAbsolutePath() + "' is not a directory.");
    }
    File attachedFile = new File(uploadDirectory, hash);
    boolean isMoved = file.renameTo(attachedFile);

    if (!isMoved) {
        FileUtils.copyFile(file, attachedFile);
        file.delete();
    }

    // Close all resources.

    return hash;
}

From source file:org.gradle.util.GUtil.java

public static String toString(Iterable<?> names) {
    Formatter formatter = new Formatter();
    boolean first = true;
    for (Object name : names) {
        if (first) {
            formatter.format("'%s'", name);
            first = false;/*  www .  j a v a 2 s  .  c o m*/
        } else {
            formatter.format(", '%s'", name);
        }
    }
    return formatter.toString();
}

From source file:org.kalypso.shape.deegree.Shape2GML.java

private static String buildValueElementsDefinition(final IDBFField[] fields) {
    final Formatter formatter = new Formatter();

    for (final IDBFField field : fields) {
        final IMarshallingTypeHandler th = findTypeHandler(field);

        final String name = field.getName();
        final String type = th.getTypeName().getLocalPart();

        formatter.format("<element name='%s' type='%s'/>%n", name, type);
    }/* www  .  ja  v  a 2s.c om*/

    return formatter.toString();
}

From source file:net.ftb.util.DownloadUtils.java

public static String fileHash(File file, String type) throws IOException {
    if (!file.exists()) {
        return "";
    }/*from www . j a  v a  2 s .  c om*/
    if (type.equalsIgnoreCase("md5"))
        return fileMD5(file);
    if (type.equalsIgnoreCase("sha1"))
        return fileSHA(file);
    URL fileUrl = file.toURI().toURL();
    MessageDigest dgest = null;
    try {
        dgest = MessageDigest.getInstance(type);
    } catch (NoSuchAlgorithmException e) {
    }
    InputStream str = fileUrl.openStream();
    byte[] buffer = new byte[65536];
    int readLen;
    while ((readLen = str.read(buffer, 0, buffer.length)) != -1) {
        dgest.update(buffer, 0, readLen);
    }
    str.close();
    Formatter fmt = new Formatter();
    for (byte b : dgest.digest()) {
        fmt.format("%02X", b);
    }
    String result = fmt.toString();
    fmt.close();
    return result;
}

From source file:fi.mikuz.boarder.util.FileProcessor.java

private static String byteArray2Hex(byte[] hash) {
    Formatter formatter = new Formatter();
    for (byte b : hash) {
        formatter.format("%02x", b);
    }//from   www  . j av a 2  s. c  o m
    return formatter.toString();
}

From source file:org.openme.openme.java

private static String byteToHexString(byte[] bytes) {
    Formatter formatter = new Formatter();

    for (byte b : bytes)
        formatter.format("%02x", b);

    return formatter.toString();
}

From source file:com.ideateam.plugin.Version.java

public static String getSHA1FromFileContent(String filename) throws NoSuchAlgorithmException, IOException {

    final MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");

    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    final byte[] buffer = new byte[1024];

    for (int read = 0; (read = is.read(buffer)) != -1;) {
        messageDigest.update(buffer, 0, read);
    }/*from   w  w  w .  j a  va  2 s . co m*/

    is.close();

    // Convert the byte to hex format
    Formatter formatter = new Formatter();

    for (final byte b : messageDigest.digest()) {
        formatter.format("%02x", b);
    }

    String res = formatter.toString();

    formatter.close();

    return res;
}

From source file:org.opendatakit.appengine.updater.UpdaterWindow.java

public static String fmt(String id, Object... args) {
    StringBuilder sb = new StringBuilder();
    Formatter formatter = null;
    try {//from  ww  w  . java 2s  .  c om
        formatter = new Formatter(sb, Locale.getDefault());
        formatter.format(t(id), args);
        return formatter.toString();
    } finally {
        if (formatter != null) {
            formatter.close();
        }
    }
}

From source file:org.ohmage.db.DbHelper.java

public static String getSHA1Hash(String input) throws NoSuchAlgorithmException {
    Formatter formatter = new Formatter();
    MessageDigest md = MessageDigest.getInstance("SHA1");
    byte[] hash = md.digest(input.getBytes());

    for (byte b : hash) {
        formatter.format("%02x", b);
    }//  w  ww.  j  ava2  s .co  m

    return formatter.toString();
}

From source file:com.itemanalysis.psychometrics.reliability.KR21.java

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    Formatter f = new Formatter(builder);
    String f2 = "%.4f";
    f.format("%7s", "KR-21: ");
    f.format(f2, this.value());
    return f.toString();
}