/** Equivalent to cp/copy command */ public void getFile( FileFragment remote, FileFragment local, final ProgressMonitor progressMonitor) throws FileResourceException { if (remote.isFragment() || local.isFragment()) { throw new UnsupportedOperationException( "The FTP provider does not support partial transfers"); } String currentDirectory = getCurrentDirectory(); File localFile = new File(local.getFile()); try { ftpClient.setPassive(); ftpClient.setLocalActive(); final long size = localFile.length(); DataSink sink; if (progressMonitor != null) { // The sink is used to follow progress sink = new DataSinkStream(new FileOutputStream(localFile)) { public void write(Buffer buffer) throws IOException { super.write(buffer); progressMonitor.progress(offset, size); } }; } else { sink = new DataSinkStream(new FileOutputStream(localFile)); } ftpClient.get(remote.getFile(), sink, null); } catch (Exception e) { throw translateException("Cannot retrieve the given file", e); } }
/** Copy a local file to a remote file. Default option 'overwrite' */ public void putFile( FileFragment local, FileFragment remote, final ProgressMonitor progressMonitor) throws FileResourceException { if (local.isFragment() || remote.isFragment()) { throw new UnsupportedOperationException( "The FTP provider does not support partial transfers"); } String currentDirectory = getCurrentDirectory(); File localFile = new File(local.getFile()); try { ftpClient.setPassive(); ftpClient.setLocalActive(); final long size = localFile.length(); DataSource source; if (progressMonitor != null) { source = new DataSourceStream(new FileInputStream(localFile)) { public Buffer read() throws IOException { progressMonitor.progress(totalRead, size); return super.read(); } }; } else { source = new DataSourceStream(new FileInputStream(localFile)); } ftpClient.put(remote.getFile(), source, null, false); } catch (Exception e) { throw translateException("Cannot transfer the given file", e); } }
/** * Equivalent to ls command in the current directory * * @throws IOException * @throws FileResourceException */ public Collection<GridFile> list() throws FileResourceException { List<GridFile> gridFileList = new ArrayList<GridFile>(); try { ftpClient.setPassive(); ftpClient.setLocalActive(); ftpClient.setType(Session.TYPE_ASCII); Enumeration<?> list = ftpClient.list().elements(); ftpClient.setType(Session.TYPE_IMAGE); while (list.hasMoreElements()) { gridFileList.add(createGridFile(list.nextElement())); } return gridFileList; } catch (Exception e) { throw translateException("Cannot list the elements of the current directory", e); } }
public OutputStream openOutputStream(String name) throws FileResourceException { OutputStreamDataSource source = null; try { ftpClient.setPassive(); ftpClient.setLocalActive(); source = new OutputStreamDataSource(16384); TransferState state = ftpClient.asynchPut(name, source, null, false); state.waitForStart(); return source.getOutputStream(); } catch (Exception e) { if (source != null) { try { source.close(); } catch (IOException ee) { logger.warn("Failed to close FTP source", ee); } } throw translateException("Failed to open FTP stream", e); } }
public InputStream openInputStream(String name) throws FileResourceException { InputStreamDataSink sink = null; try { ftpClient.setPassive(); ftpClient.setLocalActive(); sink = new InputStreamDataSink(); TransferState state = ftpClient.asynchGet(name, sink, null); state.waitForStart(); return sink.getInputStream(); } catch (Exception e) { if (sink != null) { try { sink.close(); } catch (IOException ee) { logger.warn("Failed to close FTP sink", ee); } } throw translateException("Failed to open FTP stream", e); } }