rdicroce commented on PR #364:
URL: https://github.com/apache/maven-resolver/pull/364#issuecomment-1815195848

   So for shits and giggles, I decided to paste the 1.6.0 file copy code into 
the 1.9.x branch. Here's what it looks like:
   
   ```
       public static CollocatedTempFile newTempFile(Path file) throws 
IOException {
           Path parent = requireNonNull(file.getParent(), "file must have 
parent");
           Files.createDirectories(parent);
           Path tempFile = parent.resolve(file.getFileName() + "."
                   + 
Long.toUnsignedString(ThreadLocalRandom.current().nextLong()) + ".tmp");
           return new CollocatedTempFile() {
               @Override
               public Path getPath() {
                   return tempFile;
               }
   
               @Override
               public void move() throws IOException {
                try (InputStream in = Files.newInputStream(tempFile); 
OutputStream out = Files.newOutputStream(file)) {
                        copy(out, in);
                }
               }
   
               @Override
               public void close() throws IOException {
                   Files.deleteIfExists(tempFile);
               }
           };
       }
       
       private static long copy( OutputStream os, InputStream is )
               throws IOException
           {
               long total = 0L;
   
               ByteBuffer buffer = ByteBuffer.allocate( 1024 * 32 );
               byte[] array = buffer.array();
   
               while ( true )
               {
                   int bytes = is.read( array );
                   if ( bytes < 0 )
                   {
                       break;
                   }
   
                   os.write( array, 0, bytes );
   
                   total += bytes;
               }
   
               return total;
           }
   ```
   
   Then I tried the same test as before. This code did NOT throw an exception. 
And it appears to have actually worked. The file on disk has different 
timestamps inside the JAR. Eclipse even seems to have noticed that it changed, 
although that's difficult to say for sure.
   
   Why does this work and the other code doesn't? I have no idea. Probably has 
something to do with the fact that this code actually copies the content of the 
file, byte by byte. Whereas the MoveFile approach probably only tries to change 
the actual location on disk that the file points to.


-- 
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: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to