Store temporary platform binary into directory instead of root tmp

This commit is contained in:
hedgecrw85 2019-08-28 19:48:37 -05:00
parent 058a4dcc3a
commit 6634692ac9
2 changed files with 27 additions and 30 deletions

2
NOTICE
View File

@ -1,5 +1,5 @@
Fazecast jSerialComm
Copyright (C) 2012-2018 Fazecast, Inc.
Copyright (C) 2012-2019 Fazecast, Inc.
This product includes software developed at Fazecast, Inc: you can
redistribute it and/or modify it under the terms of either the Apache

View File

@ -2,7 +2,7 @@
* SerialPort.java
*
* Created on: Feb 25, 2012
* Last Updated on: Jul 23, 2019
* Last Updated on: Aug 28, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2019 Fazecast, Inc.
@ -30,7 +30,6 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
@ -57,23 +56,14 @@ public final class SerialPort
private static volatile boolean isWindows = false;
static
{
// Determine the temporary file directory for Java
// Determine the temporary file directory for Java and remove any previous versions of this library
String OS = System.getProperty("os.name").toLowerCase();
String libraryPath = "", fileName = "";
String tempFileDirectory = System.getProperty("java.io.tmpdir");
if ((tempFileDirectory.charAt(tempFileDirectory.length()-1) != '\\') &&
(tempFileDirectory.charAt(tempFileDirectory.length()-1) != '/'))
if ((tempFileDirectory.charAt(tempFileDirectory.length()-1) != '\\') && (tempFileDirectory.charAt(tempFileDirectory.length()-1) != '/'))
tempFileDirectory += "/";
// Force remove any previous versions of this library
File directory = new File(tempFileDirectory);
if (directory.exists())
{
File directoryListing[] = directory.listFiles();
for (File listing : directoryListing)
if (listing.isFile() && listing.toString().contains("jSerialComm"))
listing.delete();
}
tempFileDirectory += "jSerialComm/";
deleteDirectory(new File(tempFileDirectory));
// Determine Operating System and architecture
if (System.getProperty("java.vm.vendor").toLowerCase().contains("android"))
@ -210,12 +200,14 @@ public final class SerialPort
System.exit(-1);
}
// Get path of native library and copy file to working directory
String tempFileName = tempFileDirectory + (new Date()).getTime() + "-" + fileName;
File tempNativeLibrary = new File(tempFileName);
tempNativeLibrary.deleteOnExit();
// Copy platform-specific binary to a temporary location
try
{
// Get path of native library and copy file to working directory
File tempNativeLibrary = new File(tempFileDirectory + (new Date()).getTime() + "-" + fileName);
tempNativeLibrary.getParentFile().mkdirs();
tempNativeLibrary.deleteOnExit();
// Load the native jSerialComm library
InputStream fileContents = SerialPort.class.getResourceAsStream("/" + libraryPath + "/" + fileName);
if ((fileContents == null) && isAndroid)
@ -231,17 +223,13 @@ public final class SerialPort
else
{
// Copy the native library to the system temp directory
try
{
FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary);
byte transferBuffer[] = new byte[4096];
int numBytesRead;
FileOutputStream destinationFileContents = new FileOutputStream(tempNativeLibrary);
byte transferBuffer[] = new byte[4096];
int numBytesRead;
while ((numBytesRead = fileContents.read(transferBuffer)) > 0)
destinationFileContents.write(transferBuffer, 0, numBytesRead);
destinationFileContents.close();
}
catch (FileNotFoundException e) {};
while ((numBytesRead = fileContents.read(transferBuffer)) > 0)
destinationFileContents.write(transferBuffer, 0, numBytesRead);
destinationFileContents.close();
fileContents.close();
// Load and initialize native library
@ -259,6 +247,15 @@ public final class SerialPort
return !canonicalFile.getCanonicalFile().equals(canonicalFile.getAbsoluteFile());
}
// Static recursive directory deletion function
private static void deleteDirectory(File path)
{
if (path.isDirectory())
for (File file : path.listFiles())
deleteDirectory(file);
path.delete();
}
/**
* Returns the same output as calling {@link #getPortDescription()}. This may be useful for display containers which call a Java Object's default toString() method.
*