Native window decorations: fixed slow application startup under particular conditions (issue #319)

This commit is contained in:
Karl Tauber
2021-05-13 00:54:22 +02:00
parent 866751ffc1
commit 359eedf773
2 changed files with 33 additions and 10 deletions

View File

@@ -53,6 +53,8 @@ FlatLaf Change Log
themes. themes.
- Native window decorations: Fixed occasional double window title bar when - Native window decorations: Fixed occasional double window title bar when
creating many frames or dialogs. (issue #315) creating many frames or dialogs. (issue #315)
- Native window decorations: Fixed slow application startup under particular
conditions. (e.g. incomplete custom JRE) (issue #319)
- Linux: Fixed/improved detection of user font settings. (issue #309) - Linux: Fixed/improved detection of user font settings. (issue #309)

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@@ -144,26 +145,46 @@ public class NativeLibrary
String suffix = (dot >= 0) ? name.substring( dot ) : ""; String suffix = (dot >= 0) ? name.substring( dot ) : "";
Path tempDir = getTempDir(); Path tempDir = getTempDir();
if( tempDir != null ) {
deleteTemporaryFiles( tempDir );
return Files.createTempFile( tempDir, prefix, suffix ); // Note:
} else // Not using Files.createTempFile() here because it uses random number generator SecureRandom,
return Files.createTempFile( prefix, suffix ); // which may take 5-10 seconds to initialize under particular conditions.
// Use current time in nanoseconds instead of a random number.
// To avoid (theoretical) collisions, append a counter.
long nanoTime = System.nanoTime();
for( int i = 0;; i++ ) {
String s = prefix + Long.toUnsignedString( nanoTime ) + i + suffix;
try {
return Files.createFile( tempDir.resolve( s ) );
} catch( FileAlreadyExistsException ex ) {
// ignore --> increment counter and try again
}
}
} }
private static Path getTempDir() throws IOException { private static Path getTempDir() throws IOException {
// get standard temporary directory
String tmpdir = System.getProperty( "java.io.tmpdir" );
if( SystemInfo.isWindows ) { if( SystemInfo.isWindows ) {
// On Windows, where File.delete() and File.deleteOnExit() does not work // On Windows, where File.delete() and File.deleteOnExit() does not work
// for loaded native libraries, they will be deleted on next application startup. // for loaded native libraries, they will be deleted on next application startup.
// The default temporary directory may contain hundreds or thousands of files. // The default temporary directory may contain hundreds or thousands of files.
// To make searching for "marked for deletion" files as fast as possible, // To make searching for "marked for deletion" files as fast as possible,
// use a sub directory that contains only our temporary native libraries. // use a sub directory that contains only our temporary native libraries.
Path tempDir = Paths.get( System.getProperty( "java.io.tmpdir" ) + "/flatlaf.temp" ); tmpdir += "\\flatlaf.temp";
Files.createDirectories( tempDir ); }
return tempDir;
} else // create temporary directory
return null; // use standard temporary directory Path tempDir = Paths.get( tmpdir );
Files.createDirectories( tempDir );
// delete no longer needed temporary files (from already exited applications)
if( SystemInfo.isWindows )
deleteTemporaryFiles( tempDir );
return tempDir;
} }
private static void deleteTemporaryFiles( Path tempDir ) { private static void deleteTemporaryFiles( Path tempDir ) {