Skip to content

Commit

Permalink
temp folder write bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanmumin committed Oct 21, 2015
1 parent 0c714f0 commit a905d39
Showing 1 changed file with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class FileOperations {

public static final String JAVA_IO_TMP_DIR = "java.io.tmpdir";
public static final String TEMP_DIR = System.getProperty(JAVA_IO_TMP_DIR).toString();
public static final String TEMP_DIR = System.getProperty(JAVA_IO_TMP_DIR);

private FileOperations() {

Expand All @@ -32,26 +32,22 @@ public static File writeToTemp(InputStream in) throws IOException {
String tempName = UUID.randomUUID().toString().replaceAll("-", "");

//Create new temp file under OS's temp folder with a random name
File tempFile = new File(System.getProperty(JAVA_IO_TMP_DIR) + tempName + ".tmp");
File tempFile = new File(TEMP_DIR, tempName + ".tmp");
if (!tempFile.createNewFile()) {
throw new IOException("Can not create temp file :" + tempFile.getParent());
} else if (in == null) {
return tempFile;
}
//Write file with a 1024 byte buffer.
FileOutputStream tempOS = new FileOutputStream(tempFile);
try {
try (FileOutputStream tempOS = new FileOutputStream(tempFile)) {
byte[] buf = new byte[1024];
int i = in.read(buf);
while (i != -1) {
tempOS.write(buf, 0, i);
i = in.read(buf);
}
} finally {
if (in != null) {
in.close();
}
tempOS.close();
in.close();
}
return tempFile;
}
Expand Down

0 comments on commit a905d39

Please sign in to comment.