Skip to content

Instantly share code, notes, and snippets.

@inaz2
Last active Apr 26, 2016
Embed
What would you like to do?
package application;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
private final TableView<FileInfo> table = new TableView<>();
@Override
@SuppressWarnings("unchecked")
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,600,400); // expanded default size
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("SHA-1 Calculator FX");
// TableView settings
table.setEditable(true);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<FileInfo, String> filePathCol = new TableColumn<>("File Path");
filePathCol.setCellValueFactory(new PropertyValueFactory<>("path"));
filePathCol.setCellFactory(TextFieldTableCell.forTableColumn());
TableColumn<FileInfo, String> sha1sumCol = new TableColumn<>("sha1sum");
sha1sumCol.setCellValueFactory(new PropertyValueFactory<>("sha1sum"));
sha1sumCol.setCellFactory(TextFieldTableCell.forTableColumn());
table.getColumns().addAll(filePathCol, sha1sumCol);
ObservableList<FileInfo> data = FXCollections.observableArrayList();
table.setItems(data);
root.setCenter(table);
// enable file drop
scene.setOnDragOver(new DragOverHandler());
scene.setOnDragDropped(new DragDroppedHandler());
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
private class DragOverHandler implements EventHandler<DragEvent> {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
if (db.hasFiles()) {
event.acceptTransferModes(TransferMode.COPY);
} else {
event.consume();
}
}
}
private class DragDroppedHandler implements EventHandler<DragEvent> {
@Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasFiles()) {
success = true;
ObservableList<FileInfo> data = table.getItems();
for (File file : db.getFiles()) {
FileInfo fileinfo = new FileInfo(file.getAbsolutePath(), "");
data.add(fileinfo);
SHA1CalculationTask task = new SHA1CalculationTask(fileinfo);
fileinfo.sha1sumProperty().bind(task.messageProperty());
new Thread(task).start();
}
}
event.setDropCompleted(success);
event.consume();
}
}
class SHA1CalculationTask extends Task<Void> {
private final FileInfo fileinfo;
public SHA1CalculationTask(FileInfo fileinfo) {
this.fileinfo = fileinfo;
}
@Override
protected Void call() throws Exception {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// update digest with each 8MB chunk
String path = fileinfo.getPath();
long totalBytes = (new File(path)).length();
long currentBytes = 0;
try (FileInputStream istream = new FileInputStream(path)) {
byte[] buf = new byte[8*1024*1024];
int n;
while ((n = istream.read(buf, 0, buf.length)) != -1) {
md.update(Arrays.copyOf(buf, n));
currentBytes += n;
String message = String.format("calculating %d%%...", 100 * currentBytes / totalBytes);
updateMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
updateMessage(sb.toString());
return null;
}
}
public static class FileInfo {
private final SimpleStringProperty path = new SimpleStringProperty();
private final SimpleStringProperty sha1sum = new SimpleStringProperty();
private FileInfo(String path, String sha1sum) {
setPath(path);
setSha1sum(sha1sum);
}
public String getPath() {
return path.get();
}
public void setPath(String newPath) {
path.set(newPath);
}
public String getSha1sum() {
return sha1sum.get();
}
public void setSha1sum(String newSha1sum) {
sha1sum.set(newSha1sum);
}
public SimpleStringProperty sha1sumProperty() {
return sha1sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment