Skip to content

Commit 7a444a2

Browse files
committed
Fixed Hot Filer a bit
For some reason the progress bar runs again after the first time, without strings in it
1 parent 9caf177 commit 7a444a2

File tree

5 files changed

+62
-39
lines changed

5 files changed

+62
-39
lines changed

src/main/java/com/i_comit/windows/AES.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private static void doCrypto(int cipherMode, String key, File inputFile,
8484
| IllegalBlockSizeException ex) {
8585
throw new CryptoException("Error encrypting/decrypting file", ex);
8686
} catch (IOException | UncheckedIOException ex) {
87-
System.out.println("Last File Was " + inputFile.getName());
88-
System.exit(0);
87+
//System.out.println("Last File Was " + inputFile.getName());
88+
8989
}
9090
}
9191

@@ -117,7 +117,7 @@ public static void AESQuery() {
117117
AES.t.interrupt();
118118
}
119119
try {
120-
List<Path> paths = listPaths(path);
120+
List<Path> paths = listAESPaths(path);
121121
File[] contents = directory.listFiles();
122122

123123
if (contents != null) {
@@ -145,10 +145,8 @@ public static void AESQuery() {
145145
} else {
146146
GUI.labelCutterThread(jAlertLabel, "i-ncript folder does not exist", 40, 40, 1000);
147147
}
148-
} catch (IOException | UncheckedIOException ex) {
148+
} catch (IOException ex) {
149149
//ex.printStackTrace();
150-
System.out.println("USB disconnected");
151-
System.exit(0);
152150
}
153151

154152
try {
@@ -165,7 +163,6 @@ public static void AESQuery() {
165163
}
166164
} catch (IOException | UncheckedIOException ex) {
167165
System.out.println("USB disconnected");
168-
System.exit(0);
169166
}
170167
}
171168

src/main/java/com/i_comit/windows/GUI.java

+24-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ public static int countFiles(Path path) throws IOException {
6565
}
6666
return result;
6767
}
68+
69+
public static int countFiles2(Path path) throws IOException {
70+
int result = 0;
71+
try ( Stream<Path> walk = Files.walk(path)) {
72+
int result2 = 0;
73+
switch (Statics.AESMode) {
74+
case 0 -> {
75+
result2 = Math.toIntExact(walk.filter(Files::isRegularFile).filter(p -> !p.getFileName().toString().endsWith(".enc")).count());
76+
result = result2;
77+
}
78+
case 1 -> {
79+
result2 = Math.toIntExact(walk.filter(Files::isRegularFile).filter(p -> p.getFileName().toString().endsWith(".enc")).count());
80+
result = result2;
81+
}
82+
}
83+
}
84+
return result;
85+
}
6886
};
6987

7088
class progressBar_T implements Runnable {
@@ -79,7 +97,6 @@ public void run() {
7997
System.exit(0);
8098
} catch (IOException | UncheckedIOException ex) {
8199
System.out.println("USB disconnected");
82-
System.exit(0);
83100
}
84101
}
85102

@@ -102,6 +119,8 @@ public static void progressBar() throws InterruptedException, IOException {
102119

103120
switch (Statics.AESMode) {
104121
case 0 -> {
122+
AES.t.interrupt();
123+
105124
jProgressBar1.setMaximum(100);
106125
jProgressBar1.setValue(100);
107126
GUI.labelCutterThread(jAlertLabel, "encryption of " + Statics.fileCount + " files complete", 20, 20, 600);
@@ -118,6 +137,8 @@ public static void progressBar() throws InterruptedException, IOException {
118137

119138
}
120139
case 1 -> {
140+
AES.t.interrupt();
141+
121142
jProgressBar1.setMaximum(100);
122143
jProgressBar1.setValue(100);
123144
GUI.labelCutterThread(jAlertLabel, "decryption of " + Statics.fileCount + " files complete", 20, 20, 600);
@@ -139,8 +160,8 @@ public static void progressBar() throws InterruptedException, IOException {
139160
}
140161
}
141162
} catch (IOException | UncheckedIOException ex) {
142-
System.out.println("USB disconnected");
143-
System.exit(0);
163+
System.out.println("USB disconnected");
164+
System.exit(0);
144165
}
145166
if (!jProgressBar1.isStringPainted()) {
146167
break;

src/main/java/com/i_comit/windows/HotFiler.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package com.i_comit.windows;
66

77
import static com.i_comit.windows.AES.*;
8+
import static com.i_comit.windows.HotFiler.listNewPaths;
89
import static com.i_comit.windows.Main.jAlertLabel;
910
import java.io.File;
1011
import java.nio.file.Files;
@@ -26,17 +27,16 @@ public class HotFiler {
2627
public static void HotFilerThread() throws IOException {
2728
HotFiler_T hotFilerThread = new HotFiler_T();
2829
t = new Thread(hotFilerThread);
30+
2931
if (Statics.hotFilerState) {
3032
t.start();
31-
3233
} else {
3334
t.interrupt();
34-
3535
}
3636

3737
}
3838

39-
public static List<Path> listNewFiles(Path path) throws IOException {
39+
public static List<Path> listNewPaths(Path path) throws IOException {
4040

4141
List<Path> result;
4242
try ( Stream<Path> walk = Files.walk(path)) {
@@ -70,13 +70,13 @@ public static void folderWatcher() throws IOException {
7070
WatchKey key;
7171
while ((key = watchService.take()) != null) {
7272
for (WatchEvent<?> event : key.pollEvents()) {
73-
List<Path> paths = listNewFiles(Statics.path);
73+
List<Path> paths = listNewPaths(Statics.path);
7474
// paths.forEach(y -> System.out.println(y));
7575
// System.out.println(
7676
// "Event kind:" + event.kind()
7777
// + ". File affected: " + event.context() + ".");
78+
Main.jProgressBar1.setMaximum(countNewFiles(Statics.path));
7879
getLastModified();
79-
8080
}
8181

8282
key.reset();
@@ -88,17 +88,18 @@ public static void folderWatcher() throws IOException {
8888
}
8989

9090
public static void getLastModified() throws IOException {
91-
List<Path> paths = listNewFiles(Statics.path);
91+
List<Path> paths = listNewPaths(Statics.path);
9292
File[] contents = Statics.directory.listFiles();
93+
94+
// AES.AESThread();
9395
if (Statics.hotFilerState) {
9496
if (contents != null) {
9597
if (contents.length != 0) {
9698

9799
paths.forEach(x -> {
98100
try {
99-
Main.jProgressBar1.setMaximum(countNewFiles(Statics.path));
100101
encrypt(Hasher.modHash(Statics.password), x.toFile(), x.toFile());
101-
} catch (AES.CryptoException | IOException ex) {
102+
} catch (AES.CryptoException ex) {
102103
ex.printStackTrace();
103104
}
104105
});
@@ -125,7 +126,8 @@ public void run() {
125126
try {
126127
HotFiler.folderWatcher();
127128
} catch (IOException ex) {
128-
ex.printStackTrace();
129+
//ex.printStackTrace();
129130
}
130131
}
132+
131133
}

src/main/java/com/i_comit/windows/Main.form

-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@
196196
<Font name="Polentical Neon" size="12" style="0"/>
197197
</Property>
198198
<Property name="text" type="java.lang.String" value="ENCRYPT"/>
199-
<Property name="actionCommand" type="java.lang.String" value="ENCRYPT"/>
200199
</Properties>
201200
<Events>
202201
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jRadioButton0ActionPerformed"/>

src/main/java/com/i_comit/windows/Main.java

+23-19
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN
315315
}
316316
Statics.AESMode = 1;
317317
AES.AESThread();
318-
319318
try {
320-
Statics.fileCount = GUI.countFiles(Statics.path);
319+
Statics.fileCount = GUI.countFiles2(Statics.path);
321320
} catch (IOException ex) {
322321
ex.printStackTrace();
323322
}
@@ -326,6 +325,7 @@ private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN
326325

327326
Statics.hotFilerState = false;
328327
jToggleButton1.setSelected(false);
328+
329329
}//GEN-LAST:event_jRadioButton1ActionPerformed
330330

331331

@@ -360,24 +360,28 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
360360
private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
361361
Statics.hotFilerState ^= true;
362362
buttonGroup1.clearSelection();
363+
Statics.AESMode = 0;
364+
AES.AESThread();
363365

364-
// if (Statics.hotFilerState) {
365-
// GUI.labelCutterThread(jAlertLabel, "hot filer enabled", 30, 900);
366-
// } else {
367-
// GUI.labelCutterThread(jAlertLabel, "hot filer disabled", 30, 900);
368-
// }
369-
try {
370-
Statics.AESMode = 0;
371-
AES.AESThread();
372-
HotFiler.HotFilerThread();
373-
jRadioButton0.setSelected(true);
374-
Statics.fileCount = HotFiler.countNewFiles(Statics.path);
375-
GUI.progressBarThread();
376-
377-
// Statics.AESMode = 0;
378-
} catch (IOException ex) {
379-
ex.printStackTrace();
366+
GUI.progressBarThread();
367+
if (Statics.hotFilerState) {
368+
try {
369+
Statics.fileCount = HotFiler.countNewFiles(Statics.path);
370+
371+
GUI.progressBarThread();
372+
GUI.labelCutterThread(jAlertLabel, "hot filer enabled", 30, 30, 900);
373+
374+
HotFiler.HotFilerThread();
375+
jRadioButton0.setSelected(true);
376+
} catch (IOException ex) {
377+
ex.printStackTrace();
378+
}
379+
} else {
380+
AES.t.interrupt();
381+
GUI.labelCutterThread(jAlertLabel, "hot filer disabled", 30, 30, 900);
380382
}
383+
384+
381385
}//GEN-LAST:event_jToggleButton1ActionPerformed
382386

383387
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
@@ -408,7 +412,7 @@ private void jRadioButton0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN
408412
Statics.AESMode = 0;
409413
AES.AESThread();
410414
try {
411-
Statics.fileCount = GUI.countFiles(Statics.path);
415+
Statics.fileCount = GUI.countFiles2(Statics.path);
412416
} catch (IOException ex) {
413417
ex.printStackTrace();
414418
}

0 commit comments

Comments
 (0)