[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[cp-patches] RFC: java.io.File.createTempFile
From: |
Meskauskas Audrius |
Subject: |
[cp-patches] RFC: java.io.File.createTempFile |
Date: |
Sun, 23 Oct 2005 19:53:09 +0200 |
User-agent: |
Mozilla Thunderbird 1.0.2 (Windows/20050317) |
This path should fix the bug #22972 (createTempFile returns the same
value for parallel threads).
There is a Mauve test (createFile) now that demonstrates the problem.
The first idea was just to synchronize the method. However even being
synchronized, it was failing if the previously returned file was already
deleted from the file system. Specification states that the same name
should not be reused.
To solve the problem, I introduced the counter for the temporary files,
created during the current millisecond. By including the counter value
into the name of the temporary file, it is possible to create multiple
files during the same millisecond without any need to wait. We still
need to synchronize because of the counter field. The counter is dropped
to zero if the new millisecond comes. To make file names not longer as
before, I switched into hexadecimal system.
Unfortunately, the "DOS name" version seems not providing enough space
for the counter. However, as much as I was able to test, synchronization
also improves the work of this part, and the same file is not returned
at least while it still exists.
The great thing would be to add the process Id to the name also. Now the
same name can be still grabbed by the two (and especially more) virtual
machines that are running together. But it seems that this would require
native coding.
java.io is one of the central packages, so I post this as RFC first. For
me, it works. Under no contradictions, I would apply it after a few days.
2005-10-23 Audrius Meskauskas <address@hidden>
* java/io/File (createTempFile): Rewritten.
Index: java/io/File.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/io/File.java,v
retrieving revision 1.57
diff -u -r1.57 File.java
--- java/io/File.java 2 Jul 2005 20:32:37 -0000 1.57
+++ java/io/File.java 23 Oct 2005 17:02:30 -0000
@@ -100,6 +100,17 @@
* may be an absolute or relative path name.
*/
private String path;
+
+
+ /**
+ * The time (millisecond), when the last temporary file was created.
+ */
+ private static long last_tmp;
+
+ /**
+ * The number of files, created during the current millisecond.
+ */
+ private static int n_created;
/**
* This method tests whether or not the current thread is allowed to
@@ -1059,7 +1070,7 @@
*
* @since 1.2
*/
- public static File createTempFile(String prefix, String suffix,
+ public static synchronized File createTempFile(String prefix, String suffix,
File directory)
throws IOException
{
@@ -1091,10 +1102,23 @@
// Now identify a file name and make sure it doesn't exist.
File file;
if (!VMFile.IS_DOS_8_3)
- {
+ {
do
{
- String filename = prefix + System.currentTimeMillis() + suffix;
+ long now = System.currentTimeMillis();
+ if (now > last_tmp)
+ {
+ // The last temporary file was created more than 1 ms ago.
+ last_tmp = now;
+ n_created = 0;
+ }
+ else
+ n_created++;
+
+ String name = Long.toHexString(now);
+ if (n_created > 0)
+ name += '_'+Integer.toHexString(n_created);
+ String filename = prefix + name + suffix;
file = new File(directory, filename);
}
while (VMFile.exists(file.path));
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [cp-patches] RFC: java.io.File.createTempFile,
Meskauskas Audrius <=