Skip to content

Commit

Permalink
Add new function to auto-delete temp files upon shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Mar 20, 2024
1 parent fade42f commit e243e10
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
72 changes: 48 additions & 24 deletions src/main/java/com/fazecast/jSerialComm/SerialPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Thread> shutdownHooks = new ArrayList<Thread>();
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
{
Expand Down Expand Up @@ -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
Expand All @@ -323,15 +332,15 @@ 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())
{
File[] files = path.listFiles();
if (files != null)
for (File file : files)
deleteDirectory(file.getCanonicalFile());
deleteDirectory(file);
}
path.delete();
}
Expand Down Expand Up @@ -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.
* <p>
* 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.
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/fazecast/jSerialComm/SerialPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit e243e10

Please sign in to comment.