public static Path createTempFile( final Path dir,
final String prefix,
final String suffix,
final FileAttribute<?>... attrs )
throws IllegalArgumentException, UnsupportedOperationException, IOException, SecurityException {
checkNotNull( "dir", dir );
if ( notExists( dir ) ) {
throw new NoSuchFileException( dir.toString() );
}
final StringBuilder sb = new StringBuilder();
if ( prefix != null && prefix.trim().length() > 0 ) {
sb.append( prefix ).append( "-" );
}
final String baseName = sb.append( System.currentTimeMillis() ).append( "-" ).toString();
final String realSufix;
if ( suffix != null && suffix.trim().length() > 0 ) {
realSufix = normalizeSuffix( suffix );
} else {
realSufix = ".tmp";
}
for ( int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++ ) {
try {
return createFile( dir.resolve( baseName + counter + realSufix ), attrs );
} catch ( Exception ex ) {
}
}
throw new IllegalStateException( "Failed to create directory within "
+ TEMP_DIR_ATTEMPTS + " attempts (tried "
+ baseName + "0 to " + baseName + ( TEMP_DIR_ATTEMPTS - 1 ) + ')' );
}