Work on this assignment is going so freaking slow

This commit is contained in:
2023-11-13 09:20:57 -06:00
parent 9ef4210608
commit af97eb2bc8
18 changed files with 1713 additions and 396 deletions

View File

@@ -10,10 +10,16 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
@@ -28,12 +34,11 @@ import javafx.stage.Stage;
*/
public class CombineFilesFX extends Application {
static List<File> files = null;
static ArrayList<File> files = new ArrayList<File>();
final FileChooser fileChooser = new FileChooser();
@Override
public void start(final Stage stage) throws Exception
{
public void start(final Stage stage) throws Exception {
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
@@ -51,7 +56,30 @@ public class CombineFilesFX extends Application {
primaryBorderPane.setBottom(run);
openFilePicker.setOnAction(e -> {
files = fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow());
// When we initially get the files from the file chooser, it returns an unsortable list. Because of this, we need to create a new List with its data.
files.addAll(fileChooser.showOpenMultipleDialog(openFilePicker.getScene().getWindow()));
// The file picker appears to return files in a random order, so we need to sort them by file name.
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
return extractInt(o1.getName()) - extractInt(o2.getName());
}
String findMatch(String s) {
Pattern findNum = Pattern.compile("\\d+$");
Matcher match = findNum.matcher(s);
while (match.find()) {
return match.group();
}
return "";
}
int extractInt(String s) {
String num = findMatch(s);
System.out.println(num);
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
});
run.setOnAction(e -> {
@@ -66,33 +94,37 @@ public class CombineFilesFX extends Application {
stage.show();
}
public static void combineFiles(List<File> filesToCombine)
{
public static void combineFiles(List<File> filesToCombine) {
String outputPath = filesToCombine.get(0).getParent();
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" +filesToCombine.get(0).getName().substring( 0, ( filesToCombine.get(0).getName().length() - 2)));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)))) {
System.out.println("Writing to " + outputPath + "/" + "reconstructed_" + filesToCombine.get(0).getName().substring(0, (filesToCombine.get(0).getName().length() - 2)));
for (File file : filesToCombine) {
try (FileInputStream dataIn = new FileInputStream(file)) {
System.out.println("Opening the source file!");
byte[] buffer = new byte[4096]; // You can adjust the buffer size as needed
System.out.println("Opening the source file " + file.getName() + "!");
byte[] buffer = new byte[4096];
int bytesRead = -1;
int bytesRead;
while ((bytesRead = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, bytesRead);
while ((bytesRead = dataIn.read(buffer)) != -1) {
dataOut.write(buffer, 0, bytesRead);
}
}
}
catch (IOException ex) {
System.out.println(ex);
}
} catch (IOException ex) {
System.out.println(ex);
}
}
}
} catch (IOException ex) {
System.out.println(ex);
}
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Success!");
alert.setContentText("Successfully combined the files!");
alert.showAndWait();
}
public static void main(String[] args)
{
public static void main(String[] args) {
launch();
}
}
/*
*/

View File

@@ -0,0 +1,147 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp5.files_calebfontenot;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
import static com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
/**
*
* @author caleb
*/
public class HexEditorFX extends Application {
final FileChooser fileChooser = new FileChooser();
static File file = null;
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Open a file...");
Button openFileButton = new Button("Choose...");
TextArea editorWindow = new TextArea();
Button saveFileButton = new Button("Save");
editorWindow.setWrapText(true);
BorderPane bp = new BorderPane();
GridPane openFileGridPane = new GridPane();
openFileGridPane.add(label, 0, 0);
openFileGridPane.add(openFileButton, 1, 0);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.setAlignment(saveFileButton, Pos.CENTER);
openFileButton.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFileButton.getScene().getWindow());
byte[] data = null;
System.out.println("Getting data from fileand encoding it as hex...");
try (FileInputStream dataIn = new FileInputStream(file)) {
data = dataIn.readAllBytes();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -> {
if (file == null) {
alert.setTitle("No file selected!");
alert.setHeaderText("No file selected!");
alert.setContentText("You have to open a file first, silly!");
alert.showAndWait();
}
System.out.println("Re-encoding hex back into a byte array...");
try (FileOutputStream dataOut = new FileOutputStream(file)) {
byte[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (IllegalArgumentException ex) {
alert.setTitle("Invalid Hex!");
alert.setContentText("Invalid hex entered into the text box, unable to save.");
alert.showAndWait();
}
alert.setTitle("Save successful!");
alert.setContentText("File saved successfully.");
alert.showAndWait();
});
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
private int toDigit(char hexChar) {
int digit = Character.digit(hexChar, 16);
if (digit == -1) {
throw new IllegalArgumentException(
"Invalid Hexadecimal Character: " + hexChar);
}
return digit;
}
public byte hexToByte(String hexString) {
int firstDigit = toDigit(hexString.charAt(0));
int secondDigit = toDigit(hexString.charAt(1));
return (byte) ((firstDigit << 4) + secondDigit);
}
public byte[] decodeHexString(String hexString) {
if (hexString.length() % 2 == 1) {
throw new IllegalArgumentException(
"Invalid hexadecimal String supplied.");
}
byte[] bytes = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2) {
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
}
return bytes;
}
public String byteToHex(byte num) {
char[] hexDigits = new char[2];
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
hexDigits[1] = Character.forDigit((num & 0xF), 16);
return new String(hexDigits);
}
public String encodeHexString(byte[] byteArray) {
StringBuffer hexStringBuffer = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
return hexStringBuffer.toString();
}
public static void main(String[] args) {
launch(args);
}
}

View File

@@ -12,8 +12,10 @@ import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
@@ -27,12 +29,17 @@ import javafx.stage.Stage;
public class SplitFilesFX extends Application {
static File file = null;
final FileChooser fileChooser = new FileChooser();
static ProgressBar pb = new ProgressBar();
static Alert alert = new Alert(Alert.AlertType.INFORMATION);
@Override
public void start(final Stage stage) throws Exception
{
fileChooser.setTitle("Open File to Split...");
BorderPane primaryBorderPane = new BorderPane();
BorderPane secondaryBoarderPane = new BorderPane();
GridPane textFieldGridPane = new GridPane();
pb.prefWidthProperty().bind(stage.widthProperty());
Label infoLabel = new Label("If you split a file named tmp into 3 smaller files,\n the three smaller files are temp.txt.1, temp.txt.2, and temp.txt.3. ");
Label chooseFile = new Label("Choose a file to split: ");
@@ -44,11 +51,15 @@ public class SplitFilesFX extends Application {
textFieldGridPane.add(openFilePicker, 1, 0);
textFieldGridPane.add(splitCountLabel, 0, 1);
textFieldGridPane.add(splitCount, 1, 1);
primaryBorderPane.setAlignment(run, Pos.CENTER);
secondaryBoarderPane.setAlignment(run, Pos.CENTER);
primaryBorderPane.setTop(infoLabel);
primaryBorderPane.setCenter(textFieldGridPane);
primaryBorderPane.setBottom(run);
secondaryBoarderPane.setBottom(run);
secondaryBoarderPane.setTop(pb);
primaryBorderPane.setBottom(secondaryBoarderPane);
openFilePicker.setOnAction(e -> {
file = fileChooser.showOpenDialog(openFilePicker.getScene().getWindow());
@@ -68,12 +79,26 @@ public class SplitFilesFX extends Application {
public static void splitFile(File fileToSplit, int splitCount) {
int outputFileSize = (int) fileToSplit.length() / splitCount;
System.out.println("output file size will be: " + outputFileSize);
if (outputFileSize < 0) {
alert.setTitle("Output files too large");
alert.setHeaderText("Output files too large");
alert.setContentText("Please increase the amount of files to split!");
alert.showAndWait();
return;
}
double progress = 0;
String outputPath = fileToSplit.getParent();
// Open the original file
try (FileInputStream dataIn = new FileInputStream(fileToSplit)) {
System.out.println("Opening the source file!");
for (int i = 0; i < splitCount; ++i) {
//dataIn.mark(outputFileSize * (i + 1));
progress = (i * 100) / splitCount;
System.out.println(progress);
pb.setProgress(progress);
dataIn.mark(outputFileSize * (i + 1));
try (FileOutputStream dataOut = new FileOutputStream(outputPath + "/" + fileToSplit.getName() + "." + (i + 1))) {
dataOut.write(dataIn.readNBytes(outputFileSize), 0, outputFileSize);
System.out.println("Writing to " + outputPath + "/" + fileToSplit.getName() + "." + (i + 1));