package com.xoetrope.deploy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import net.xoetrope.xui.build.BuildProperties;
/**
* A utility to download a file and store it in the local files system. It is
* required that the application is signed for this to work
*
* <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
* the GNU Public License (GPL), please see license.txt for more details. If
* you make commercial use of this software you must purchase a commercial
* license from Xoetrope.</p>
*/
public class XUrlResourceStorageService
{
public static boolean storeFile( URL src, File dest )
{
byte[] fileBytes = new byte[ 0 ];
try {
dest.getParentFile().mkdirs();
// Check the file date
// See http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#DateFormats
// for a description of the date formats
String lm = src.openConnection().getHeaderField( "Last-Modified" );
Date lmd = null;
if ( lm != null ) {
String[] formats = { "EEE, d MMM yyyy HH:mm:ss Z", "EEEE, d-MMM-yyyy HH:mm:ss Z", "EEE MMM d HH:mm:ss yyyy" };
for ( int i = 0; i < formats.length; i++ ) {
SimpleDateFormat sdf = new SimpleDateFormat( formats[ i ], Locale.ENGLISH );
try {
lmd = sdf.parse( lm );
break;
}
catch ( ParseException ex )
{
}
}
}
else
return true;
if (( lmd != null ) && dest.exists()) {
if ( dest.lastModified() >= lmd.getTime())
return true;
}
BufferedInputStream is = new BufferedInputStream( src.openStream());
byte[] b = new byte[ 1024 ];
if ( is != null ) {
OutputStream fos = new BufferedOutputStream( new FileOutputStream( dest ));
int i = is.read( b );
while ( i != -1 ) {
fileBytes = new byte[ i ];
System.arraycopy( b, 0, fileBytes, 0, i );
fos.write( fileBytes );
b = new byte[ 1024 ];
i = is.read( b );
}
fos.flush();
fos.close();
}
else
return false;
}
catch ( Exception ex ) {
if ( BuildProperties.DEBUG )
ex.printStackTrace();
return false;
}
return true;
}
}
|