if(source==null || source.getIsDirectory()){
throw new IllegalStateException("Source="+source);
}
OutputStream os=targetStream!=null?targetStream:null;
FileTransferClient ftc=null;
try{
String path=source.getPath();
if(targetStream==null){
if(localFile.isDirectory()){
localFile=new File(localFile,getName(source.getPath()));
}
if(mode.equals(Mode.nooverwrite) && localFile.exists()){
System.out.println("File exists and creation mode was set to 'nooverwrite'.");
return;
}
System.out.println("Downloading remote file '"+sms.getUrl()+"#/"+path+"' -> "+localFile.getAbsolutePath());
os=new FileOutputStream(localFile.getAbsolutePath(), mode.equals(Mode.append));
}
chosenProtocol=sms.findSupportedProtocol(preferredProtocols.toArray(new ProtocolType.Enum[preferredProtocols.size()]));
Map<String,String>extraParameters=makeExtraParameters(chosenProtocol);
ftc=sms.getExport(path,extraParameters,chosenProtocol);
configure(ftc, extraParameters);
System.out.println("DEB:File transfer URL : "+ftc.getUrl());
// ProgressBar p=null;
if(ftc instanceof IMonitorable && showProgress){
long size=ftc.getSourceFileSize();
if(isRange()){
size=getRangeSize();
}
// p=new ProgressBar(localFile.getName(),size,msg);
// ((IMonitorable) ftc).setProgressListener(p);
}
long startTime=System.currentTimeMillis();
if(isRange()){
if(!(ftc instanceof SupportsPartialRead)){
throw new Exception("Byte range is defined but protocol does not allow " +
"partial read! Please choose a different protocol!");
}
System.out.println("Byte range: "+startByte+" - "+(getRangeSize()>0?endByte:""));
SupportsPartialRead pReader=(SupportsPartialRead)ftc;
pReader.readPartial(startByte, endByte-startByte+1, os);
}
else{
ftc.readAllData(os);
}
// if(p!=null){
// p.finish();
// }
if(timing){
long duration=System.currentTimeMillis()-startTime;
double rate=(double)localFile.length()/(double)duration;
System.out.println("Rate: " +rate+ " kB/sec.");
}
if(targetStream==null)copyProperties(source, localFile);
}
finally{
try{
if(targetStream==null && os!=null){
os.close();
}
}catch(Exception ignored){}
if(ftc!=null){
try{
ftc.destroy();
}catch(Exception e1){
// System.out.println("Could not destroy the filetransfer client",e1);
}
}
}