178 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HexEditorFX.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.string {color: #6a8759}
.number {color: #6897bb}
.ST1 {color: #9876aa}
.ST3 {color: #ffc66d}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/HexEditorFX.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
<span class="literal">import</span> <span class="literal">static</span> com.calebfontenot.mp5.files_calebfontenot.SplitFilesFX.alert;
<span class="comment">import</span> <span class="comment">static</span> <span class="comment">com</span><span class="comment">.</span><span class="comment">calebfontenot</span><span class="comment">.</span><span class="comment">mp5</span><span class="comment">.</span><span class="comment">files_calebfontenot</span><span class="comment">.</span><span class="comment">SplitFilesFX</span><span class="comment">.</span><span class="comment">file</span><span class="comment">;</span>
<span class="literal">import</span> java.io.File;
<span class="literal">import</span> java.io.FileInputStream;
<span class="literal">import</span> java.io.FileNotFoundException;
<span class="literal">import</span> java.io.FileOutputStream;
<span class="literal">import</span> java.io.IOException;
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ArrayList</span><span class="comment">;</span>
<span class="literal">import</span> javafx.application.Application;
<span class="literal">import</span> javafx.geometry.Pos;
<span class="literal">import</span> javafx.scene.Scene;
<span class="literal">import</span> javafx.scene.control.Button;
<span class="literal">import</span> javafx.scene.control.Label;
<span class="literal">import</span> javafx.scene.control.TextArea;
<span class="literal">import</span> javafx.scene.layout.BorderPane;
<span class="literal">import</span> javafx.scene.layout.GridPane;
<span class="literal">import</span> javafx.stage.FileChooser;
<span class="literal">import</span> javafx.stage.Stage;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> HexEditorFX <span class="literal">extends</span> Application {
<span class="literal">final</span> FileChooser <span class="ST1">fileChooser</span> = <span class="literal">new</span> FileChooser();
<span class="literal">static</span> File <span class="ST2">file</span> = <span class="literal">null</span>;
@Override
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">start</span>(Stage stage) <span class="literal">throws</span> Exception {
Label label = <span class="literal">new</span> Label(<span class="string">&quot;</span><span class="string">Open a file...</span><span class="string">&quot;</span>);
Button openFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Choose...</span><span class="string">&quot;</span>);
TextArea editorWindow = <span class="literal">new</span> TextArea();
Button saveFileButton = <span class="literal">new</span> Button(<span class="string">&quot;</span><span class="string">Save</span><span class="string">&quot;</span>);
editorWindow.setWrapText(<span class="literal">t</span><span class="literal">rue</span>);
BorderPane bp = <span class="literal">new</span> BorderPane();
GridPane openFileGridPane = <span class="literal">new</span> GridPane();
openFileGridPane.add(label, <span class="number">0</span>, <span class="number">0</span>);
openFileGridPane.add(openFileButton, <span class="number">1</span>, <span class="number">0</span>);
bp.setTop(openFileGridPane);
bp.setCenter(editorWindow);
bp.setBottom(saveFileButton);
bp.<span class="ST4">setAlignment</span>(saveFileButton, Pos.<span class="ST2">CENTER</span>);
openFileButton.setOnAction(e -&gt; {
<span class="ST2">file</span> = <span class="ST1">fileChooser</span>.showOpenDialog(openFileButton.getScene().getWindow());
<span class="literal">byte</span>[] data = <span class="literal">null</span>;
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Getting data from fileand encoding it as hex...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileInputStream dataIn = <span class="literal">new</span> FileInputStream(<span class="ST2">file</span>)) {
data = dataIn.readAllBytes();
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
}
String dataString = encodeHexString(data);
editorWindow.setText(dataString);
});
saveFileButton.setOnAction(e -&gt; {
<span class="literal">if</span> (<span class="ST2">file</span> == <span class="literal">null</span>) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setHeaderText(<span class="string">&quot;</span><span class="string">No file selected!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">You have to open a file first, silly!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
System.<span class="ST2">out</span>.println(<span class="string">&quot;</span><span class="string">Re-encoding hex back into a byte array...</span><span class="string">&quot;</span>);
<span class="literal">try</span> (FileOutputStream dataOut = <span class="literal">new</span> FileOutputStream(<span class="ST2">file</span>)) {
<span class="literal">byte</span>[] bytesToSave = decodeHexString(editorWindow.getText());
dataOut.write(bytesToSave);
} <span class="literal">catch</span> (FileNotFoundException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IOException ex) {
ex.printStackTrace();
} <span class="literal">catch</span> (IllegalArgumentException ex) {
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Invalid Hex!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">Invalid hex entered into the text box, unable to save.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
}
<span class="ST2">alert</span>.setTitle(<span class="string">&quot;</span><span class="string">Save successful!</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.setContentText(<span class="string">&quot;</span><span class="string">File saved successfully.</span><span class="string">&quot;</span>);
<span class="ST2">alert</span>.showAndWait();
});
Scene scene = <span class="literal">new</span> Scene(bp);
stage.setScene(scene);
stage.show();
}
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">toDigit</span>(<span class="literal">char</span> hexChar) {
<span class="literal">int</span> digit = Character.<span class="ST4">digit</span>(hexChar, <span class="number">1</span><span class="number">6</span>);
<span class="literal">if</span> (digit == -<span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid Hexadecimal Character: </span><span class="string">&quot;</span> + hexChar);
}
<span class="literal">return</span> digit;
}
<span class="literal">public</span> <span class="literal">byte</span> <span class="ST3">hexToByte</span>(String hexString) {
<span class="literal">int</span> firstDigit = toDigit(hexString.charAt(<span class="number">0</span>));
<span class="literal">int</span> secondDigit = toDigit(hexString.charAt(<span class="number">1</span>));
<span class="literal">return</span> (<span class="literal">byte</span>) ((firstDigit &lt;&lt; <span class="number">4</span>) + secondDigit);
}
<span class="literal">public</span> <span class="literal">byte</span>[] <span class="ST3">decodeHexString</span>(String hexString) {
<span class="literal">if</span> (hexString.length() % <span class="number">2</span> == <span class="number">1</span>) {
<span class="literal">throw</span> <span class="literal">new</span> IllegalArgumentException(
<span class="string">&quot;</span><span class="string">Invalid hexadecimal String supplied.</span><span class="string">&quot;</span>);
}
<span class="literal">byte</span>[] bytes = <span class="literal">new</span> <span class="literal">byte</span>[hexString.length() / <span class="number">2</span>];
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; hexString.length(); i += <span class="number">2</span>) {
bytes[i / <span class="number">2</span>] = hexToByte(hexString.substring(i, i + <span class="number">2</span>));
}
<span class="literal">return</span> bytes;
}
<span class="literal">public</span> String <span class="ST3">byteToHex</span>(<span class="literal">byte</span> num) {
<span class="literal">char</span>[] hexDigits = <span class="literal">new</span> <span class="literal">char</span>[<span class="number">2</span>];
hexDigits[<span class="number">0</span>] = Character.<span class="ST4">forDigit</span>((num &gt;&gt; <span class="number">4</span>) &amp; <span class="number">0xF</span>, <span class="number">1</span><span class="number">6</span>);
hexDigits[<span class="number">1</span>] = Character.<span class="ST4">forDigit</span>((num &amp; <span class="number">0xF</span>), <span class="number">1</span><span class="number">6</span>);
<span class="literal">return</span> <span class="literal">new</span> String(hexDigits);
}
<span class="literal">public</span> String <span class="ST3">encodeHexString</span>(<span class="literal">byte</span>[] byteArray) {
StringBuffer hexStringBuffer = <span class="literal">new</span> StringBuffer();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; byteArray.<span class="ST1">length</span>; i++) {
hexStringBuffer.append(byteToHex(byteArray[i]));
}
<span class="literal">return</span> hexStringBuffer.toString();
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
<span class="ST4">launch</span>(args);
}
}
</pre></body>
</html>