You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Julien Béti (Jira)" <ji...@apache.org> on 2022/11/29 16:35:00 UTC

[jira] [Created] (NET-717) FTPClient: Encapsulate completePendingCommand in returned outputstream

Julien Béti created NET-717:
-------------------------------

             Summary: FTPClient: Encapsulate completePendingCommand in returned outputstream
                 Key: NET-717
                 URL: https://issues.apache.org/jira/browse/NET-717
             Project: Commons Net
          Issue Type: Improvement
          Components: FTP
            Reporter: Julien Béti


Most store / retrieve methods in {{FTPClient}} class requires a call to {{completePendingCommand}} method in order to make sure that the operation completed successfully, and allow additional operations to go on.

This is clearly documented, but often lead to clumsy code as 99% of the time we just need to know if the file operation executed successfully or not (and in some case, the {{completePendingCommand}} is simply forgotten leading to, as documented, unexpected behavior of subsequent commands.

The idea would be to return a {{FilterOutputStream}} / {{FilterInputStream}} that would encapsulates the returned {{OutputStream}}/{{InputStream}} returned by methods that need a subsequent call to {{completePendingCommand}}. These filter implementations would call the {{completePendingCommand}} method on {{close}}, throwing an {{IOException}} if it returns false.

{code:java}
package org.apache.commons.net.ftp;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FTPOutputStream extends FilterOutputStream {
  private final FTPClient ftpClient;
  public FTPOutputStream(OutputStream out, FTPClient ftpClient) {
    super(out);
    this.ftpClient = ftpClient;
  }

  @Override
  public void close() throws IOException {
    super.close();
    if(!ftpClient.completePendingCommand()) {
      throw new IOException("FTP Client was unable to complete pending command");
    }
  }
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)