List of usage examples for java.util Formatter close
public void close()
From source file:com.base.service.WeixinService.java
private static String byteToHex(final byte[] hash) { Formatter formatter = new Formatter(); for (byte b : hash) { formatter.format("%02x", b); }/*from w ww. j ava 2 s .co m*/ String result = formatter.toString(); formatter.close(); return result; }
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/*from w w w. j ava 2s . c om*/ * @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:net.ftb.util.DownloadUtils.java
public static String fileHash(File file, String type) throws IOException { if (!file.exists()) { return ""; }/*from w ww .j a va 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: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 ww w.j a va2s .c o 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 {/*w w w .j a v a 2 s . c o m*/ formatter = new Formatter(sb, Locale.getDefault()); formatter.format(t(id), args); return formatter.toString(); } finally { if (formatter != null) { formatter.close(); } } }
From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java
/** * Calculates the hash of the certificate known as the "thumbprint" * and returns it as a string representation. * * @param cert The certificate to hash.// ww w . ja va2 s. c o m * @param algorithm The hash algorithm to use. * @return The SHA-1 hash of the certificate. * @throws CertificateException */ private static String getThumbprint(Certificate cert, String algorithm) throws CertificateException { MessageDigest digest; try { digest = MessageDigest.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { throw new CertificateException(e); } byte[] encodedCert = cert.getEncoded(); StringBuilder sb = new StringBuilder(encodedCert.length * 2); Formatter f = new Formatter(sb); try { for (byte b : digest.digest(encodedCert)) f.format("%02x", b); } finally { f.close(); } return sb.toString(); }
From source file:org.efaps.ui.wicket.models.EmbeddedLink.java
/** * Getter method for the instance variable {@link #tag}. * * @return value of instance variable {@link #tag} *//* w ww. j a va2 s . c om*/ public String getTag() { String ret = this.tag; final Formatter formatter = new Formatter(); formatter.format(ret, getId()); ret = formatter.toString(); formatter.close(); return ret; }
From source file:com.richtodd.android.quiltdesign.block.Swatch.java
public String getDescription() { Formatter formatter = new Formatter(); try {/* ww w. jav a2 s . c o m*/ formatter.format("#%02x%02x%02x", Color.red(m_color), Color.green(m_color), Color.blue(m_color)); return formatter.toString(); } finally { formatter.close(); } }
From source file:org.apache.hadoop.hive.metastore.tools.BenchmarkTool.java
@Override public void run() { LOG.info("Using warmup " + warmup + " spin " + spinCount + " nparams " + nParameters + " threads " + nThreads);// w ww . j ava 2 s . com StringBuilder sb = new StringBuilder(); BenchData bData = new BenchData(dbName, tableName); MicroBenchmark bench = new MicroBenchmark(warmup, spinCount); BenchmarkSuite suite = new BenchmarkSuite(); suite.setScale(scale).doSanitize(doSanitize).add("getNid", () -> benchmarkGetNotificationId(bench, bData)) .add("listDatabases", () -> benchmarkListDatabases(bench, bData)) .add("listTables", () -> benchmarkListAllTables(bench, bData)) .add("getTable", () -> benchmarkGetTable(bench, bData)) .add("createTable", () -> benchmarkTableCreate(bench, bData)) .add("dropTable", () -> benchmarkDeleteCreate(bench, bData)) .add("dropTableWithPartitions", () -> benchmarkDeleteWithPartitions(bench, bData, 1, nParameters)) .add("addPartition", () -> benchmarkCreatePartition(bench, bData)) .add("dropPartition", () -> benchmarkDropPartition(bench, bData)) .add("listPartition", () -> benchmarkListPartition(bench, bData)) .add("getPartition", () -> benchmarkGetPartitions(bench, bData, 1)) .add("getPartitionNames", () -> benchmarkGetPartitionNames(bench, bData, 1)) .add("getPartitionsByNames", () -> benchmarkGetPartitionsByName(bench, bData, 1)) .add("renameTable", () -> benchmarkRenameTable(bench, bData, 1)) .add("dropDatabase", () -> benchmarkDropDatabase(bench, bData, 1)); for (int howMany : instances) { suite.add("listTables" + '.' + howMany, () -> benchmarkListTables(bench, bData, howMany)) .add("dropTableWithPartitions" + '.' + howMany, () -> benchmarkDeleteWithPartitions(bench, bData, howMany, nParameters)) .add("listPartitions" + '.' + howMany, () -> benchmarkListManyPartitions(bench, bData, howMany)) .add("getPartitions" + '.' + howMany, () -> benchmarkGetPartitions(bench, bData, howMany)) .add("getPartitionNames" + '.' + howMany, () -> benchmarkGetPartitionNames(bench, bData, howMany)) .add("getPartitionsByNames" + '.' + howMany, () -> benchmarkGetPartitionsByName(bench, bData, howMany)) .add("addPartitions" + '.' + howMany, () -> benchmarkCreatePartitions(bench, bData, howMany)) .add("dropPartitions" + '.' + howMany, () -> benchmarkDropPartitions(bench, bData, howMany)) .add("renameTable" + '.' + howMany, () -> benchmarkRenameTable(bench, bData, howMany)) .add("dropDatabase" + '.' + howMany, () -> benchmarkDropDatabase(bench, bData, howMany)); } if (doList) { suite.listMatching(matches, exclude).forEach(System.out::println); return; } LOG.info("Using table '{}.{}", dbName, tableName); try (HMSClient client = new HMSClient(getServerUri(host, String.valueOf(port)), confDir)) { bData.setClient(client); if (!client.dbExists(dbName)) { client.createDatabase(dbName); } if (client.tableExists(dbName, tableName)) { client.dropTable(dbName, tableName); } // Arrange various benchmarks in a suite BenchmarkSuite result = suite.runMatching(matches, exclude); Formatter fmt = new Formatter(sb); if (doCSV) { result.displayCSV(fmt, csvSeparator); } else { result.display(fmt); } PrintStream output = System.out; if (outputFile != null) { output = new PrintStream(outputFile); } if (outputFile != null) { // Print results to stdout as well StringBuilder s = new StringBuilder(); Formatter f = new Formatter(s); result.display(f); System.out.print(s); f.close(); } output.print(sb.toString()); fmt.close(); if (dataSaveDir != null) { saveData(result.getResult(), dataSaveDir, scale); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.richtodd.android.quiltdesign.block.Swatch.java
public String getSortString() { if (m_sortString == null) { Color.colorToHSV(m_color, m_hsv); Formatter formatter = new Formatter(); try {//from w w w . j a v a2 s .c om formatter.format("%03d-%03d-%03d", (int) m_hsv[0], (int) (m_hsv[1] * 100f), (int) (m_hsv[2] * 100f)); m_sortString = formatter.toString(); } finally { formatter.close(); } } return m_sortString; }