You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ni...@apache.org on 2003/12/20 08:17:16 UTC
cvs commit: avalon/repository/util/src/java/org/apache/avalon/repository/util LoaderUtils.java
niclas 2003/12/19 23:17:16
Modified: repository/util/src/java/org/apache/avalon/repository/util
LoaderUtils.java
Log:
Changed so that the resources are downloaded to a temporary named file, and renamed on completion, to ensure non-corrupt artifacts.
Revision Changes Path
1.2 +53 -24 avalon/repository/util/src/java/org/apache/avalon/repository/util/LoaderUtils.java
Index: LoaderUtils.java
===================================================================
RCS file: /home/cvs/avalon/repository/util/src/java/org/apache/avalon/repository/util/LoaderUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- LoaderUtils.java 19 Dec 2003 16:46:15 -0000 1.1
+++ LoaderUtils.java 20 Dec 2003 07:17:16 -0000 1.2
@@ -52,22 +52,24 @@
import java.io.File ;
-import java.io.IOException ;
-import java.io.InputStream ;
+import java.io.FileInputStream ;
import java.io.FileOutputStream ;
+import java.io.InputStream ;
+import java.io.IOException ;
+import java.io.OutputStream ;
import java.util.ArrayList ;
-import java.util.Properties ;
import java.text.ParseException ;
+import java.util.Properties ;
-import java.net.URL ;
-import java.net.URLConnection ;
-import java.net.URLClassLoader ;
import java.net.HttpURLConnection ;
import java.net.MalformedURLException ;
+import java.net.URL ;
+import java.net.URLClassLoader ;
+import java.net.URLConnection ;
-import javax.naming.NamingException ;
import javax.naming.NamingEnumeration ;
+import javax.naming.NamingException ;
import javax.naming.directory.Attributes ;
import org.apache.avalon.repository.Artifact;
@@ -350,29 +352,26 @@
File parent = destination.getParentFile() ;
parent.mkdirs() ;
- FileOutputStream out = new FileOutputStream( destination ) ;
-
- byte[] buffer = new byte[100 * 1024] ;
- int length ;
-
+ File tempFile = File.createTempFile( "~avalon", ".tmp", parent );
+ tempFile.deleteOnExit(); // safety harness in case we abort abnormally
+ // like a Ctrl-C.
+
+ FileOutputStream tempOut = new FileOutputStream( tempFile );
+ String title;
if( update )
{
- System.out.print( "Update from: [" + source + "] ") ;
+ title = "Update from: [" + source + "] ";
}
else
{
- System.out.print( "Download from: [" + source + "] ") ;
- }
- while ( ( length = in.read( buffer ) ) >= 0 )
- {
- out.write( buffer, 0, length ) ;
- System.out.print( "." ) ;
+ title = "Download from: [" + source + "] ";
}
+ copyStream( in, tempOut, true, title );
- System.out.println( "" ) ;
- out.close() ;
- in.close() ;
-
+ // An atomic operation and no risk of a corrupted
+ // artifact content.
+ tempFile.renameTo( destination );
+
// if (and only if) the use file time option is set, then the
// saved file now has its timestamp set to that of the downloaded
// file
@@ -413,6 +412,36 @@
"Internal error while attempting to create a url from the file: "
+ file;
throw new RepositoryException( error, e );
+ }
+ }
+
+ private static void copyStream( InputStream src, OutputStream dest, boolean closeStreams, String title )
+ throws IOException
+ {
+ boolean progress = title != null;
+ byte[] buffer = new byte[100 * 1024] ;
+ int length ;
+ if( title != null )
+ System.out.println( title );
+ try
+ {
+ while ( ( length = src.read( buffer ) ) >= 0 )
+ {
+ dest.write( buffer, 0, length ) ;
+ if( progress )
+ System.out.print( "." ) ;
+ }
+ } finally
+ {
+ if( closeStreams )
+ {
+ if( src != null )
+ src.close();
+ if( dest != null )
+ dest.close();
+ }
+ if( progress )
+ System.out.println( "" ) ;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org