Skip to content

Commit

Permalink
[Java] use Files.createTempFile instead (#8787)
Browse files Browse the repository at this point in the history
* use Files.createTempFile

* fix import

* add missing import
  • Loading branch information
wing328 committed Feb 23, 2021
1 parent d85f61f commit 1b6cabb
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
Expand Down Expand Up @@ -1027,15 +1028,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import java.lang.reflect.Type;
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
Expand Down Expand Up @@ -578,7 +580,7 @@ public class ApiClient {
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
* @return Temporary folder path
*/
public String getTempFolderPath() {
Expand Down Expand Up @@ -1077,15 +1079,15 @@ public class ApiClient {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -278,7 +279,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
* the system's default tempopary folder.
*
* @return the temporary folder path
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)"></a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
*/
public String getTempFolderPath() {
return tempFolderPath;
Expand Down Expand Up @@ -596,15 +597,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

Expand Down Expand Up @@ -202,9 +203,9 @@ public class ApiClient {
@Override
public File convert(ResponseBody value) throws IOException {
File file = File.createTempFile("retrofit-file", ".tmp");
Files.write(Paths.get(file.getPath()), value.bytes());
return file;
Path path = Files.createTempFile("retrofit-file", ".tmp");
Files.write(path, value.bytes());
return path.toFile();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
Expand Down Expand Up @@ -943,15 +944,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
Expand Down Expand Up @@ -943,15 +944,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
Expand Down Expand Up @@ -524,7 +526,7 @@ public ApiClient setDebugging(boolean debugging) {
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
* @return Temporary folder path
*/
public String getTempFolderPath() {
Expand Down Expand Up @@ -968,15 +970,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
Expand Down Expand Up @@ -513,7 +515,7 @@ public ApiClient setDebugging(boolean debugging) {
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
* @return Temporary folder path
*/
public String getTempFolderPath() {
Expand Down Expand Up @@ -969,15 +971,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.net.URI;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.SecureRandom;
Expand Down Expand Up @@ -513,7 +515,7 @@ public ApiClient setDebugging(boolean debugging) {
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
* @return Temporary folder path
*/
public String getTempFolderPath() {
Expand Down Expand Up @@ -969,15 +971,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -267,7 +268,7 @@ public ApiClient setDebugging(boolean debugging) {
* the system's default tempopary folder.
*
* @return the temporary folder path
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)"></a>
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
*/
public String getTempFolderPath() {
return tempFolderPath;
Expand Down Expand Up @@ -585,15 +586,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

Expand Down Expand Up @@ -199,9 +200,9 @@ public Converter<ResponseBody, File> responseBodyConverter(Type type,
@Override
public File convert(ResponseBody value) throws IOException {

File file = File.createTempFile("retrofit-file", ".tmp");
Files.write(Paths.get(file.getPath()), value.bytes());
return file;
Path path = Files.createTempFile("retrofit-file", ".tmp");
Files.write(path, value.bytes());
return path.toFile();
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
Expand Down Expand Up @@ -879,15 +880,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
Expand Down Expand Up @@ -824,15 +825,15 @@ public File prepareDownloadFile(Response response) throws IOException {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
// Files.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
return Files.createTempFile(prefix, suffix).toFile();
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
}

/**
Expand Down
Loading

0 comments on commit 1b6cabb

Please sign in to comment.