Example usage for org.apache.commons.net.ftp FTPFileEntryParser readNextEntry

List of usage examples for org.apache.commons.net.ftp FTPFileEntryParser readNextEntry

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPFileEntryParser readNextEntry.

Prototype

String readNextEntry(BufferedReader reader) throws IOException;

Source Link

Document

Reads the next entry using the supplied BufferedReader object up to whatever delemits one entry from the next.

Usage

From source file:org.apache.nutch.protocol.ftp.Client.java

/**
 * retrieve list reply for path/*from w  w w.ja v a 2  s  . co m*/
 * 
 * @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());
    }

}