FileDocCategorySizeDatePackage
FTPFileIterator.javaAPI DocApache Commons NET 1.4.1 API11865Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.ftp

FTPFileIterator

public class FTPFileIterator extends Object
This class implements a bidirectional iterator over an FTPFileList. Elements may be retrieved one at at time using the hasNext() - next() syntax familiar from Java 2 collections. Alternatively, entries may be receieved as an array of any requested number of entries or all of them.
author
Steve Cohen
version
$Id: FTPFileIterator.java 165675 2005-05-02 20:09:55Z rwinston $
see
org.apache.commons.net.ftp.FTPFileList
see
org.apache.commons.net.ftp.FTPFileEntryParser
see
org.apache.commons.net.ftp.FTPListParseEngine
deprecated
This class is deprecated as of version 1.2 and will be removed in version 2.0 - use FTPFileParseEngine instead

Fields Summary
private List
rawlines
a vector of strings, each representing a possibly valid ftp file entry
private FTPFileEntryParser
parser
the parser to which this iterator delegates its parsing duties
private static final int
UNINIT
constant shorthand for the situation where the raw listing has not yet been scanned
private static final int
DIREMPTY
constant shorthand for the situation where the raw listing has been scanned and found to have no valid entry.
private int
itemptr
this iterator's current position within rawlines.
private int
firstGoodEntry
number within rawlines of the first valid file entry.
private static final FTPFile[]
EMPTY
shorthand for an empty return value.
Constructors Summary
FTPFileIterator(FTPFileList rawlist)
"Package-private" constructor. Only the FTPFileList can create an iterator, using it's iterator() method. The list will be iterated with the list's default parser.

param
rawlist the FTPFileList to be iterated


                                         
      
    
        this(rawlist, rawlist.getParser());
    
FTPFileIterator(FTPFileList rawlist, FTPFileEntryParser parser)
"Package-private" constructor. Only the FTPFileList can create an iterator, using it's iterator() method. The list will be iterated with a supplied parser

param
rawlist the FTPFileList to be iterated
param
parser the system specific parser for raw FTP entries.

        this.rawlines = rawlist.getLines();
        this.parser = parser;
    
Methods Summary
public FTPFile[]getFiles()
Returns a list of FTPFile objects for ALL files listed in the server's LIST output.

return
a list of FTPFile objects for ALL files listed in the server's LIST output.


                                       
      
    
        if (this.itemptr != DIREMPTY)
        {
            init();
        }
        return getNext(0);
    
private intgetFirstGoodEntry()
Skips over any introductory lines and stuff in the listing that does not represent files, returning the line number of the first entry that does represent a file.

return
the line number within rawlines of the first good entry in the array or DIREMPTY if there are no good entries.

        FTPFile entry = null;
        for (int iter = 0; iter < this.rawlines.size(); iter++)
        {
            String line = (String) this.rawlines.get(iter);
            entry = parseFTPEntry(line);
            if (null != entry)
            {
                return iter;
            }
        }
        return DIREMPTY;
    
public FTPFile[]getNext(int quantityRequested)
Returns an array of at most quantityRequested FTPFile objects starting at this iterator's current position within its associated list. If fewer than quantityRequested such elements are available, the returned array will have a length equal to the number of entries at and after after the current position. If no such entries are found, this array will have a length of 0. After this method is called the current position is advanced by either quantityRequested or the number of entries available after the iterator, whichever is fewer.

param
quantityRequested the maximum number of entries we want to get. A 0 passed here is a signal to get ALL the entries.
return
an array of at most quantityRequested FTPFile objects starting at the current position of this iterator within its list and at least the number of elements which exist in the list at and after its current position.


        // if we haven't gotten past the initial junk do so.
        if (this.firstGoodEntry == UNINIT)
        {
            this.firstGoodEntry = getFirstGoodEntry();
        }
        if (this.firstGoodEntry == DIREMPTY)
        {
            return EMPTY;
        }

        int max = this.rawlines.size() - this.firstGoodEntry;

        // now that we know the maximum we can possibly get,
        // resolve a 0 request to ask for that many.

        int howMany = (quantityRequested == 0) ? max : quantityRequested;
        howMany = (howMany + this.itemptr < this.rawlines.size())
                   ? howMany
                   : this.rawlines.size() - this.itemptr;

        FTPFile[] output = new FTPFile[howMany];

        for (int i = 0, e = this.firstGoodEntry + this.itemptr ;
                i < howMany; i++, e++)
        {
            output[i] = parseFTPEntry((String) this.rawlines.get(e));
            this.itemptr++;
        }
        return output;
    
public FTPFile[]getPrevious(int quantityRequested)
Returns an array of at most quantityRequested FTPFile objects starting at the position preceding this iterator's current position within its associated list. If fewer than quantityRequested such elements are available, the returned array will have a length equal to the number of entries after the iterator. If no such entries are found, this array will have a length of 0. The entries will be ordered in the same order as the list, not reversed. After this method is called the current position is moved back by either quantityRequested or the number of entries available before the current position, whichever is fewer.

param
quantityRequested the maximum number of entries we want to get. A 0 passed here is a signal to get ALL the entries.
return
an array of at most quantityRequested FTPFile objects starting at the position preceding the current position of this iterator within its list and at least the number of elements which exist in the list prior to its current position.

        int howMany = quantityRequested;
        // can't retreat further than we've previously advanced
        if (howMany > this.itemptr)
        {
            howMany = this.itemptr;
        }
        FTPFile[] output = new FTPFile[howMany];
        for (int i = howMany, e = this.firstGoodEntry + this.itemptr; i > 0;)
        {
            output[--i] = parseFTPEntry((String) this.rawlines.get(--e));
            this.itemptr--;
        }
        return output;
    
public booleanhasNext()
Method for determining whether getNext() will successfully return a non-null value.

return
true if there exist any files after the one currently pointed to by the internal iterator, false otherwise.

        int fge = this.firstGoodEntry;
        if (fge == DIREMPTY)
        {
            //directory previously found empty - return false
            return false;
        }
        else if (fge < 0)
        {
            // we haven't scanned the list yet so do it first
            fge = getFirstGoodEntry();
        }
        return fge + this.itemptr < this.rawlines.size();
    
public booleanhasPrevious()
Method for determining whether getPrevious() will successfully return a non-null value.

return
true if there exist any files before the one currently pointed to by the internal iterator, false otherwise.

        int fge = this.firstGoodEntry;
        if (fge == DIREMPTY)
        {
            //directory previously found empty - return false
            return false;
        }
        else if (fge < 0)
        {
            // we haven't scanned the list yet so do it first
            fge = getFirstGoodEntry();
        }

        return this.itemptr > fge;
    
private voidinit()
resets iterator to the beginning of the list.

        this.itemptr = 0;
        this.firstGoodEntry = UNINIT;
    
public FTPFilenext()
Returns a single parsed FTPFile object corresponding to the raw input line at this iterator's current position. After this method is called the internal iterator is advanced by one element (unless already at end of list).

return
a single FTPFile object corresponding to the raw input line at the position of the internal iterator over the list of raw input lines maintained by this object or null if no such object exists.

        FTPFile[] file = getNext(1);
        if (file.length > 0)
        {
            return file[0];
        }
        else
        {
            return null;
        }
    
private FTPFileparseFTPEntry(java.lang.String entry)
Delegates to this object's parser member the job of parsing an entry.

param
entry A string containing one entry, as determined by the parser's getNextEntry() method.
return
an FTPFile object representing this entry or null if it can't be parsed as a file

        return this.parser.parseFTPEntry(entry);
    
public FTPFileprevious()
Returns a single parsed FTPFile object corresponding to the raw input line at the position preceding that of the internal iterator over the list of raw lines maintained by this object After this method is called the internal iterator is retreated by one element (unless it is already at beginning of list).

return
a single FTPFile object corresponding to the raw input line at the position immediately preceding that of the internal iterator over the list of raw input lines maintained by this object.

        FTPFile[] file = getPrevious(1);
        if (file.length > 0)
        {
            return file[0];
        }
        else
        {
            return null;
        }