List of usage examples for org.apache.commons.net.io Util copyStream
public static final long copyStream(InputStream source, OutputStream dest, int bufferSize, long streamSize, CopyStreamListener listener, boolean flush) throws CopyStreamException
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * @since 3.1/*from w ww . ja v a2s . c om*/ */ protected boolean _storeFile(String command, String remote, InputStream local) throws IOException { Socket socket = _openDataConnection_(command, remote); if (socket == null) { return false; } OutputStream output = getBufferedOutputStream(socket.getOutputStream()); if (__fileType == ASCII_FILE_TYPE) { output = new ToNetASCIIOutputStream(output); } CSL csl = null; if (__controlKeepAliveTimeout > 0) { csl = new CSL(this, __controlKeepAliveTimeout, __controlKeepAliveReplyTimeout); } // Treat everything else as binary for now try { Util.copyStream(local, output, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, __mergeListeners(csl), false); } catch (IOException e) { Util.closeQuietly(socket); // ignore close errors here if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } throw e; } output.close(); // ensure the file is fully written socket.close(); // done writing the file if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } // Get the transfer response boolean ok = completePendingCommand(); return ok; }
From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java
/** * @since 3.1// w w w . j a va 2 s. c o m */ protected boolean _retrieveFile(String command, String remote, OutputStream local) throws IOException { Socket socket = _openDataConnection_(command, remote); if (socket == null) { return false; } InputStream input = getBufferedInputStream(socket.getInputStream()); if (__fileType == ASCII_FILE_TYPE) { input = new FromNetASCIIInputStream(input); } CSL csl = null; if (__controlKeepAliveTimeout > 0) { csl = new CSL(this, __controlKeepAliveTimeout, __controlKeepAliveReplyTimeout); } // Treat everything else as binary for now try { Util.copyStream(input, local, getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE, __mergeListeners(csl), false); } finally { Util.closeQuietly(input); Util.closeQuietly(socket); if (csl != null) { csl.cleanUp(); // fetch any outstanding keepalive replies } } // Get the transfer response boolean ok = completePendingCommand(); return ok; }
From source file:org.openconcerto.ftp.IFtp.java
private boolean __storeFile(int command, String remote, InputStream local) throws IOException { OutputStream output;/* ww w. j a v a 2s .co m*/ Socket socket; if ((socket = _openDataConnection_(command, remote)) == null) return false; output = new BufferedOutputStream(socket.getOutputStream(), getBufferSize()); // __fileType is private, if we really want we could subclass setFileType() to have access // if (__fileType == ASCII_FILE_TYPE) // output = new ToNetASCIIOutputStream(output); // Treat everything else as binary for now try { final CopyStreamListener l = this.streamListeners.get(); final long size = CopyStreamEvent.UNKNOWN_STREAM_SIZE; if (l != null) { // copyStream() doesn't pass 0 l.bytesTransferred(0, 0, size); } Util.copyStream(local, output, getBufferSize(), size, l, false); } catch (IOException e) { try { socket.close(); } catch (IOException f) { } throw e; } output.close(); socket.close(); return completePendingCommand(); }