Java Code Examples for org.apache.commons.net.ftp.FTPFileEntryParser#readNextEntry()

The following examples show how to use org.apache.commons.net.ftp.FTPFileEntryParser#readNextEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From anthelion with Apache License 2.0 4 votes vote down vote up
public void retrieveList(String path, List entries, int limit,
  FTPFileEntryParser parser)
  throws IOException,
    FtpExceptionCanNotHaveDataConnection,
    FtpExceptionUnknownForcedDataClose,
    FtpExceptionControlClosedByForcedDataClose {
  Socket socket = __openPassiveDataConnection(FTPCommand.LIST, path);

  if (socket == null)
    throw new FtpExceptionCanNotHaveDataConnection("LIST "
      + ((path == null) ? "" : path));

  BufferedReader reader =
      new BufferedReader(new InputStreamReader(socket.getInputStream()));

  // force-close data channel socket, when download limit is reached
  boolean mandatory_close = false;

  //List entries = new LinkedList();
  int count = 0;
  String line = parser.readNextEntry(reader);
  while (line != null) {
    FTPFile ftpFile = parser.parseFTPEntry(line);
    // skip non-formatted lines
    if (ftpFile == null) {
      line = parser.readNextEntry(reader);
      continue;
    }
    entries.add(ftpFile);
    count += line.length();
    // impose download limit if limit >= 0, otherwise no limit
    // here, cut off is up to the line when total bytes is just over limit
    if (limit >= 0 && count > limit) {
      mandatory_close = true;
      break;
    }
    line = parser.readNextEntry(reader);
  }

  //if (mandatory_close)
  // you always close here, no matter mandatory_close or not.
  // however different ftp servers respond differently, see below.
  socket.close();

  // scenarios:
  // (1) mandatory_close is false, download limit not reached
  //     no special care here
  // (2) mandatory_close is true, download limit is reached
  //     different servers have different reply codes:

  try {
    int reply = getReply();
    if (!_notBadReply(reply))
      throw new FtpExceptionUnknownForcedDataClose(getReplyString());
  } catch (FTPConnectionClosedException e) {
    // some ftp servers will close control channel if data channel socket
    // is closed by our end before all data has been read out. Check:
    // tux414.q-tam.hp.com FTP server (hp.com version whp02)
    // so must catch FTPConnectionClosedException thrown by getReply() above
    //disconnect();
    throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
  }

}
 
Example 2
Source File: Client.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
/**
     * retrieve list reply for path
     * @param path
     * @param entries
     * @param limit
     * @param parser
     * @throws IOException
     * @throws FtpExceptionCanNotHaveDataConnection
     * @throws FtpExceptionUnknownForcedDataClose
     * @throws FtpExceptionControlClosedByForcedDataClose
     */
    public void retrieveList(String path, List<FTPFile> entries, int limit,
      FTPFileEntryParser parser)
      throws IOException,
        FtpExceptionCanNotHaveDataConnection,
        FtpExceptionUnknownForcedDataClose,
        FtpExceptionControlClosedByForcedDataClose {
      Socket socket = __openPassiveDataConnection(FTPCommand.LIST, path);

      if (socket == null)
        throw new FtpExceptionCanNotHaveDataConnection("LIST "
          + ((path == null) ? "" : path));

      BufferedReader reader =
          new BufferedReader(new InputStreamReader(socket.getInputStream()));

      // force-close data channel socket, when download limit is reached
//      boolean mandatory_close = false;

      //List entries = new LinkedList();
      int count = 0;
      String line = parser.readNextEntry(reader);
      while (line != null) {
        FTPFile ftpFile = parser.parseFTPEntry(line);
        // skip non-formatted lines
        if (ftpFile == null) {
          line = parser.readNextEntry(reader);
          continue;
        }
        entries.add(ftpFile);
        count += line.length();
        // impose download limit if limit >= 0, otherwise no limit
        // here, cut off is up to the line when total bytes is just over limit
        if (limit >= 0 && count > limit) {
//          mandatory_close = true;
          break;
        }
        line = parser.readNextEntry(reader);
      }

      //if (mandatory_close)
      // you always close here, no matter mandatory_close or not.
      // however different ftp servers respond differently, see below.
      socket.close();

      // scenarios:
      // (1) mandatory_close is false, download limit not reached
      //     no special care here
      // (2) mandatory_close is true, download limit is reached
      //     different servers have different reply codes:

      try {
        int reply = getReply();
        if (!_notBadReply(reply))
          throw new FtpExceptionUnknownForcedDataClose(getReplyString());
      } catch (FTPConnectionClosedException e) {
        // some ftp servers will close control channel if data channel socket
        // is closed by our end before all data has been read out. Check:
        // tux414.q-tam.hp.com FTP server (hp.com version whp02)
        // so must catch FTPConnectionClosedException thrown by getReply() above
        //disconnect();
        throw new FtpExceptionControlClosedByForcedDataClose(e.getMessage());
      }

    }