Example usage for org.apache.commons.net.io CopyStreamEvent UNKNOWN_STREAM_SIZE

List of usage examples for org.apache.commons.net.io CopyStreamEvent UNKNOWN_STREAM_SIZE

Introduction

In this page you can find the example usage for org.apache.commons.net.io CopyStreamEvent UNKNOWN_STREAM_SIZE.

Prototype

long UNKNOWN_STREAM_SIZE

To view the source code for org.apache.commons.net.io CopyStreamEvent UNKNOWN_STREAM_SIZE.

Click Source Link

Document

Constant used to indicate the stream size is unknown.

Usage

From source file:de.thischwa.pmcms.tool.connection.ftp.FtpTransfer.java

/**
 * Uploads a 'file' with the content of the InputStream and the name 'name' to the current dir of FTPClient.
 * If the 'file' exists, it will be deleted before.
 *
 * @throws ConnectionRunningException//from w w  w.  j  a v  a2  s  .co  m
 */
private void uploadToCurrentDir(final String name, final InputStream in) throws ConnectionRunningException {
    // TODO implement the server reconnect here!!
    OutputStream serverOut = null;
    try {
        // 1. check, if target file exists and delete it
        FTPFile serverFile = getByName(ftpClient.listFiles(), name);
        if (serverFile != null) {
            if (!ftpClient.deleteFile(name))
                throw new ConnectionRunningException("Couldn't delete existent file: " + name);
        }

        // 2. create the empty file 
        if (!ftpClient.storeFile(name, IOUtils.toInputStream("")))
            throw new ConnectionRunningException("Couldn't create and empty file for: " + name);

        // 3. copy stream
        serverOut = new BufferedOutputStream(ftpClient.storeFileStream(name), ftpClient.getBufferSize());
        Util.copyStream(in, serverOut, ftpClient.getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE,
                new CopyStreamAdapter() {
                    @Override
                    public void bytesTransferred(long totalBytesTransferred, int bytesTransferred,
                            long streamSize) {
                        progressAddBytes(bytesTransferred);
                    }
                });
        // not documented but necessary: flush and close the server stream !!!
        serverOut.flush();
        serverOut.close();
        if (!ftpClient.completePendingCommand()) {
            connectionManager.close();
            throw new ConnectionRunningException(
                    "[FTP] While getting/copying streams: " + ftpClient.getReplyString());
        }

    } catch (Exception e) {
        logger.error("[FTP] While getting/copying streams: " + e.getMessage(), e);
        if (!(e instanceof ConnectionException)) {
            connectionManager.close();
            throw new ConnectionRunningException("[FTP] While getting/copying streams: " + e.getMessage(), e);
        }
    } finally {
        IOUtils.closeQuietly(in);
        progressSetSubTaskMessage("");
    }
}

From source file:net.seedboxer.camel.component.file.remote.ftp2.Ftp2Operations.java

private boolean resume(String targetName, InputStream is) throws IOException {
    Long size = getSize(targetName);

    // Set the offset
    client.setRestartOffset(size);/*from   w ww  .ja  v a  2s  . c  om*/
    // Create stream and skip first SIZE bytes
    is.skip(size);

    try {
        OutputStream outs = client.storeFileStream(targetName);
        if (outs != null) {
            try {
                copyStream(is, outs, client.getBufferSize(), CopyStreamEvent.UNKNOWN_STREAM_SIZE,
                        client.getCopyStreamListener());
            } finally {
                outs.close();
            }
        }
    } finally {
        is.close();
    }

    return client.completePendingCommand();
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * @since 3.1//from www .java  2 s.  co  m
 */
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 v a2  s  .  c  om
 */
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;/*w ww .j a v  a  2 s .  c om*/
    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();
}