mbien commented on code in PR #9762:
URL: https://github.com/apache/netbeans/pull/9762#discussion_r4416252028


##########
platform/openide.filesystems/src/org/openide/filesystems/JarFileSystem.java:
##########
@@ -465,50 +467,32 @@ private InputStream getMemInputStream(JarFile jf, 
JarEntry je) throws IOExceptio
         return new ByteArrayInputStream(is.readAllBytes());
     }
 
-    private InputStream getTemporaryInputStream(JarFile jf, JarEntry je, 
boolean forceRecreate)
-    throws IOException {
-        String filePath = jf.getName();
-        String entryPath = je.getName();
+    private InputStream getTemporaryInputStream(JarFile jf, JarEntry je) 
throws IOException {
         StringBuffer jarCacheFolder = new StringBuffer("jarfscache"); //NOI18N
-        jarCacheFolder.append(System.getProperty("user.name")).append("/"); 
//NOI18N        
+        jarCacheFolder.append(System.getProperty("user.name")).append("/"); 
//NOI18N
 
         File jarfscache = new File(System.getProperty("java.io.tmpdir"), 
jarCacheFolder.toString()); //NOI18N
 
         if (!jarfscache.exists()) {
             jarfscache.mkdirs();
         }
 
-        File f = new File(jarfscache, temporaryName(filePath, entryPath));
-
-        boolean createContent = !f.exists();
-
-        if (createContent) {
-            f.createNewFile();
-        } else {
-            forceRecreate |= (Math.abs((System.currentTimeMillis() - 
f.lastModified())) > 10000);
-        }
-
-        if (createContent || forceRecreate) {
+        // Every call copies the entry to a new file of its own. A file shared 
between calls (or between
+        // JarFileSystems, or between JVMs) would be truncated and rewritten 
by one caller while another
+        // is still reading it. DELETE_ON_CLOSE removes the file once the 
stream is closed (on Unix, as
+        // soon as it is opened), so a stream that is never closed leaves no 
file behind either.
+        Path f = Files.createTempFile(jarfscache.toPath(), "jarfs", null); 
//NOI18N
+        try {
             // JDK 1.3 contains bug #4336753
             //is = j.getInputStream (je);
-            try (InputStream is = getInputStream4336753(jf, je); OutputStream 
os = new FileOutputStream(f)) {
-                is.transferTo(os);
+            try (InputStream is = getInputStream4336753(jf, je)) {
+                Files.copy(is, f, StandardCopyOption.REPLACE_EXISTING);
             }
+            return Files.newInputStream(f, StandardOpenOption.DELETE_ON_CLOSE);

Review Comment:
   i didn't know this option existed in java. It is a OS hook to remove the 
file as soon the (last) file descriptor is closed - cool! Have a few usages for 
that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to