~ubuntu-branches/ubuntu/gutsy/libcommons-net-java/gutsy

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/net/ftp/FTPFileList.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-05-29 21:05:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040529210546-lfvfw6v3qybb6w3r
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2001-2004 The Apache Software Foundation
 
3
 *
 
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
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
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.
 
15
 */
 
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;
 
23
 
 
24
/**
 
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.
 
37
 *
 
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.
 
46
 */
 
47
public class FTPFileList
 
48
{
 
49
    /**
 
50
     * storage for the raw lines of input read from the FTP server
 
51
     */
 
52
    private LinkedList lines = null;
 
53
    /**
 
54
     * the FTPFileEntryParser assigned to be used with this lister
 
55
     */
 
56
    private FTPFileEntryParser parser;
 
57
    /**
 
58
     * private status code for an empty directory
 
59
     */
 
60
    private static final int EMPTY_DIR = -2;
 
61
 
 
62
    /**
 
63
     * The only constructor for FTPFileList, private because
 
64
     * construction only invoked at create()
 
65
     *
 
66
     * @param parser a <code>FTPFileEntryParser</code> value that knows
 
67
     * how to parse the entries returned by a particular FTP site.
 
68
     */
 
69
    private FTPFileList (FTPFileEntryParser parser)
 
70
    {
 
71
        this.parser = parser;
 
72
        this.lines = new LinkedList();
 
73
    }
 
74
 
 
75
    /**
 
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.
 
80
     *
 
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.
 
85
     *
 
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.
 
91
     */
 
92
    public static FTPFileList create(InputStream stream,
 
93
                                      FTPFileEntryParser parser)
 
94
            throws IOException
 
95
    {
 
96
        FTPFileList list = new FTPFileList(parser);
 
97
        list.readStream(stream);
 
98
        parser.preParse(list.lines);
 
99
        return list;
 
100
    }
 
101
 
 
102
    /**
 
103
     * internal method for reading the input into the <code>lines</code> vector.
 
104
     *
 
105
     * @param stream The socket stream on which the input will be read.
 
106
     *
 
107
     * @exception IOException thrown on any failure to read the stream
 
108
     */
 
109
    public void readStream(InputStream stream) throws IOException
 
110
    {
 
111
        BufferedReader reader =
 
112
            new BufferedReader(new InputStreamReader(stream));
 
113
 
 
114
        String line = this.parser.readNextEntry(reader);
 
115
 
 
116
        while (line != null)
 
117
        {
 
118
            this.lines.add(line);
 
119
            line = this.parser.readNextEntry(reader);
 
120
        }
 
121
        reader.close();
 
122
    }
 
123
 
 
124
    /**
 
125
     * Accessor for this object's default parser.
 
126
     *
 
127
     * @return this object's default parser.
 
128
     */
 
129
    FTPFileEntryParser getParser()
 
130
    {
 
131
        return this.parser;
 
132
    }
 
133
 
 
134
    /**
 
135
     * Package private accessor for the collection of raw input lines.
 
136
     *
 
137
     * @return vector containing all the raw input lines returned from the FTP
 
138
     * server
 
139
     */
 
140
    List getLines()
 
141
    {
 
142
        return this.lines;
 
143
    }
 
144
 
 
145
    /**
 
146
     * create an iterator over this list using the parser with which this list
 
147
     * was initally created
 
148
     *
 
149
     * @return an iterator over this list using the list's default parser.
 
150
     */
 
151
    public FTPFileIterator iterator()
 
152
    {
 
153
        return new FTPFileIterator(this);
 
154
    }
 
155
    /**
 
156
     * create an iterator over this list using the supplied parser
 
157
     *
 
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.
 
160
     *
 
161
     * @return an iterator over this list using the supplied parser.
 
162
     */
 
163
    public FTPFileIterator iterator(FTPFileEntryParser parser)
 
164
    {
 
165
        return new FTPFileIterator(this, parser);
 
166
    }
 
167
 
 
168
 
 
169
    /**
 
170
     * returns an array of FTPFile objects for all the files in the directory
 
171
     * listing
 
172
     *
 
173
     * @return  an array of FTPFile objects for all the files in the directory
 
174
     * listinge
 
175
     */
 
176
    public FTPFile[] getFiles()
 
177
    {
 
178
        return iterator().getFiles();
 
179
    }
 
180
 
 
181
}
 
182
 
 
183
/* Emacs configuration
 
184
 * Local variables:        **
 
185
 * mode:             java  **
 
186
 * c-basic-offset:   4     **
 
187
 * indent-tabs-mode: nil   **
 
188
 * End:                    **
 
189
 */