2
* Copyright 2001-2004 The Apache Software Foundation
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
16
package org.apache.commons.net.ftp;
17
import java.io.BufferedReader;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.InputStreamReader;
21
import java.util.LinkedList;
22
import java.util.List;
25
* This class encapsulates a listing of files from an FTP server. It is
26
* initialized with an input stream which is read and the input split into
27
* lines, each of which (after some possible initial verbiage) represents
28
* a file on the FTP server. A parser is also supplied, which is used to
29
* iterate through the internal list of lines parsing each into an FTPFile
30
* object which is returned to the caller of the iteration methods. This
31
* parser may be replaced with another, allowing the same list to be parsed
32
* with different parsers.
33
* Parsing takes place on an as-needed basis, basically, the first time a
34
* position is iterated over. This happens at the time of iteration, not
35
* prior to it as the older <code>(FTPClient.listFiles()</code> methods did,
36
* which required a bigger memory hit.
38
* @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
39
* @version $Id: FTPFileList.java,v 1.13 2004/04/21 23:30:33 scohen Exp $
40
* @see org.apache.commons.net.ftp.FTPClient#createFileList
41
* @see org.apache.commons.net.ftp.FTPFileIterator
42
* @see org.apache.commons.net.ftp.FTPFileEntryParser
43
* @see org.apache.commons.net.ftp.FTPListParseEngine
44
* @deprecated This class is deprecated as of version 1.2 and will be
45
* removed in version 2.0 -- use FTPFileParseEngine instead.
47
public class FTPFileList
50
* storage for the raw lines of input read from the FTP server
52
private LinkedList lines = null;
54
* the FTPFileEntryParser assigned to be used with this lister
56
private FTPFileEntryParser parser;
58
* private status code for an empty directory
60
private static final int EMPTY_DIR = -2;
63
* The only constructor for FTPFileList, private because
64
* construction only invoked at create()
66
* @param parser a <code>FTPFileEntryParser</code> value that knows
67
* how to parse the entries returned by a particular FTP site.
69
private FTPFileList (FTPFileEntryParser parser)
72
this.lines = new LinkedList();
76
* The only way to create an <code>FTPFileList</code> object. Invokes
77
* the private constructor and then reads the stream supplied stream to
78
* build the intermediate array of "lines" which will later be parsed
79
* into <code>FTPFile</code> object.
81
* @param stream The input stream created by reading the socket on which
82
* the output of the LIST command was returned
83
* @param parser the default <code>FTPFileEntryParser</code> to be used
84
* by this object. This may later be changed using the init() method.
86
* @return the <code>FTPFileList</code> created, with an initialized
87
* of unparsed lines of output. Will be null if the listing cannot
88
* be read from the stream.
89
* @exception IOException
90
* Thrown on any failure to read from the socket.
92
public static FTPFileList create(InputStream stream,
93
FTPFileEntryParser parser)
96
FTPFileList list = new FTPFileList(parser);
97
list.readStream(stream);
98
parser.preParse(list.lines);
103
* internal method for reading the input into the <code>lines</code> vector.
105
* @param stream The socket stream on which the input will be read.
107
* @exception IOException thrown on any failure to read the stream
109
public void readStream(InputStream stream) throws IOException
111
BufferedReader reader =
112
new BufferedReader(new InputStreamReader(stream));
114
String line = this.parser.readNextEntry(reader);
118
this.lines.add(line);
119
line = this.parser.readNextEntry(reader);
125
* Accessor for this object's default parser.
127
* @return this object's default parser.
129
FTPFileEntryParser getParser()
135
* Package private accessor for the collection of raw input lines.
137
* @return vector containing all the raw input lines returned from the FTP
146
* create an iterator over this list using the parser with which this list
147
* was initally created
149
* @return an iterator over this list using the list's default parser.
151
public FTPFileIterator iterator()
153
return new FTPFileIterator(this);
156
* create an iterator over this list using the supplied parser
158
* @param parser The user-supplied parser with which the list is to be
159
* iterated, may be different from this list's default parser.
161
* @return an iterator over this list using the supplied parser.
163
public FTPFileIterator iterator(FTPFileEntryParser parser)
165
return new FTPFileIterator(this, parser);
170
* returns an array of FTPFile objects for all the files in the directory
173
* @return an array of FTPFile objects for all the files in the directory
176
public FTPFile[] getFiles()
178
return iterator().getFiles();
183
/* Emacs configuration
184
* Local variables: **
186
* c-basic-offset: 4 **
187
* indent-tabs-mode: nil **