From e243e102ef2dcec1bfd7aae8eb8aa9694406610a Mon Sep 17 00:00:00 2001 From: Will Hedgecock Date: Wed, 20 Mar 2024 13:51:04 -0500 Subject: [PATCH] Add new function to auto-delete temp files upon shutdown --- .../com/fazecast/jSerialComm/SerialPort.java | 72 ++++++++++++------- .../fazecast/jSerialComm/SerialPortTest.java | 1 + 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/fazecast/jSerialComm/SerialPort.java b/src/main/java/com/fazecast/jSerialComm/SerialPort.java index 1ef464b8..e235ed34 100644 --- a/src/main/java/com/fazecast/jSerialComm/SerialPort.java +++ b/src/main/java/com/fazecast/jSerialComm/SerialPort.java @@ -103,7 +103,7 @@ public class SerialPort static private final String versionString = "2.10.5"; static private final String tmpdirAppIdProperty = "fazecast.jSerialComm.appid"; static private final List shutdownHooks = new ArrayList(); - static private boolean isWindows = false, isAndroid = false, isAndroidDelete = false; + static private boolean isWindows = false, isAndroid = false, cleanUpOnShutdown = false, isAndroidDelete = false; static private volatile boolean isShuttingDown = false; static { @@ -272,31 +272,40 @@ else if (arch.contains("86") || arch.contains("amd")) } } } - } - catch (IOException e) { e.printStackTrace(); } - // Add a shutdown hook to ensure that all ports get closed - Runtime.getRuntime().addShutdownHook(SerialPortThreadFactory.get().newThread(new Runnable() - { - @Override - public void run() + // Add a shutdown hook to ensure that all ports get closed + Runtime.getRuntime().addShutdownHook(SerialPortThreadFactory.get().newThread(new Runnable() { - // Run any user-specified shutdown hooks - try { - for (Thread hook : shutdownHooks) - { - hook.start(); - hook.join(); + @Override + public void run() + { + // Run any user-specified shutdown hooks + try { + for (Thread hook : shutdownHooks) + { + hook.start(); + hook.join(); + } } + catch (InterruptedException ignored) {} + + // Un-initialize the native library + isShuttingDown = true; + if (!isAndroid) + uninitializeLibrary(); + + // Optionally delete all native library files + if (cleanUpOnShutdown) + try + { + deleteDirectory(new File(tempFileDirectory, "..").getCanonicalFile()); + deleteDirectory(new File(userHomeDirectory, "..").getCanonicalFile()); + } + catch (IOException ignored) {} } - catch (InterruptedException ignored) {} - - // Un-initialize the native library - isShuttingDown = true; - if (!isAndroid) - uninitializeLibrary(); - } - })); + })); + } + catch (IOException e) { e.printStackTrace(); } } // Static symbolic link testing function @@ -323,7 +332,7 @@ static private void cleanUpDirectory(File path) throws IOException } // Static recursive directory deletion function - static private void deleteDirectory(File path) throws IOException + static private void deleteDirectory(File path) { // Recursively delete all files and directories if (path.isDirectory()) @@ -331,7 +340,7 @@ static private void deleteDirectory(File path) throws IOException File[] files = path.listFiles(); if (files != null) for (File file : files) - deleteDirectory(file.getCanonicalFile()); + deleteDirectory(file); } path.delete(); } @@ -419,6 +428,21 @@ static public boolean setAndroidContext(Object androidApp) { } return false; } + + /** + * Causes the library to attempt to clean up after itself upon shutdown and automatically delete + * all temporary files it may have created. + *

+ * It is not recommended to use this function, as its use will increase the overhead of unpacking + * the library and searching for the correct machine architecture every time the library is loaded; + * however, if you have a specific need to ensure that temporary files are removed (for example, if + * you are using a dynamically generated "App ID" with the "fazecast.jSerialComm.appid" Java + * property, then this functionality might make sense. + */ + static public void autoCleanupAtShutdown() + { + cleanUpOnShutdown = true; + } /** * Returns a list of all available serial ports on this machine. diff --git a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java index ef654897..00ec70cf 100644 --- a/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java +++ b/src/test/java/com/fazecast/jSerialComm/SerialPortTest.java @@ -82,6 +82,7 @@ public void serialEvent(SerialPortEvent event) static public void main(String[] args) { System.out.println("\nUsing Library Version v" + SerialPort.getVersion()); + SerialPort.autoCleanupAtShutdown(); SerialPort.addShutdownHook(new Thread() { public void run() { System.out.println("\nRunning shutdown hook"); } }); SerialPort[] ports = SerialPort.getCommPorts(); System.out.println("\nAvailable Ports:\n");