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. * This class encapsulates a listing of files from an FTP server. It is
  25. * initialized with an input stream which is read and the input split into
  26. * lines, each of which (after some possible initial verbiage) represents
  27. * a file on the FTP server. A parser is also supplied, which is used to
  28. * iterate through the internal list of lines parsing each into an FTPFile
  29. * object which is returned to the caller of the iteration methods. This
  30. * parser may be replaced with another, allowing the same list to be parsed
  31. * with different parsers.
  32. * Parsing takes place on an as-needed basis, basically, the first time a
  33. * position is iterated over. This happens at the time of iteration, not
  34. * prior to it as the older <code>(FTPClient.listFiles()</code> methods did,
  35. * which required a bigger memory hit.
  36. *
  37. * @author <a href="mailto:scohen@apache.org">Steve Cohen</a>
  38. * @version $Id: FTPFileList.java,v 1.13 2004/04/21 23:30:33 scohen Exp $
  39. * @see org.apache.commons.net.ftp.FTPClient#createFileList
  40. * @see org.apache.commons.net.ftp.FTPFileIterator
  41. * @see org.apache.commons.net.ftp.FTPFileEntryParser
  42. * @see org.apache.commons.net.ftp.FTPListParseEngine
  43. * @deprecated This class is deprecated as of version 1.2 and will be
  44. * removed in version 2.0 -- use FTPFileParseEngine instead.
  45. */
  46. public class FTPFileList
  47. {
  48. /**
  49. * storage for the raw lines of input read from the FTP server
  50. */
  51. private LinkedList lines = null;
  52. /**
  53. * the FTPFileEntryParser assigned to be used with this lister
  54. */
  55. private FTPFileEntryParser parser;
  56. /**
  57. * private status code for an empty directory
  58. */
  59. private static final int EMPTY_DIR = -2;
  60. /**
  61. * The only constructor for FTPFileList, private because
  62. * construction only invoked at create()
  63. *
  64. * @param parser a <code>FTPFileEntryParser</code> value that knows
  65. * how to parse the entries returned by a particular FTP site.
  66. */
  67. private FTPFileList (FTPFileEntryParser parser)
  68. {
  69. this.parser = parser;
  70. this.lines = new LinkedList();
  71. }
  72. /**
  73. * The only way to create an <code>FTPFileList</code> object. Invokes
  74. * the private constructor and then reads the stream supplied stream to
  75. * build the intermediate array of "lines" which will later be parsed
  76. * into <code>FTPFile</code> object.
  77. *
  78. * @param stream The input stream created by reading the socket on which
  79. * the output of the LIST command was returned
  80. * @param parser the default <code>FTPFileEntryParser</code> to be used
  81. * by this object. This may later be changed using the init() method.
  82. *
  83. * @return the <code>FTPFileList</code> created, with an initialized
  84. * of unparsed lines of output. Will be null if the listing cannot
  85. * be read from the stream.
  86. * @exception IOException
  87. * Thrown on any failure to read from the socket.
  88. */
  89. public static FTPFileList create(InputStream stream,
  90. FTPFileEntryParser parser)
  91. throws IOException
  92. {
  93. FTPFileList list = new FTPFileList(parser);
  94. list.readStream(stream);
  95. parser.preParse(list.lines);
  96. return list;
  97. }
  98. /**
  99. * internal method for reading the input into the <code>lines</code> vector.
  100. *
  101. * @param stream The socket stream on which the input will be read.
  102. *
  103. * @exception IOException thrown on any failure to read the stream
  104. */
  105. public void readStream(InputStream stream) throws IOException
  106. {
  107. BufferedReader reader =
  108. new BufferedReader(new InputStreamReader(stream));
  109. String line = this.parser.readNextEntry(reader);
  110. while (line != null)
  111. {
  112. this.lines.add(line);
  113. line = this.parser.readNextEntry(reader);
  114. }
  115. reader.close();
  116. }
  117. /**
  118. * Accessor for this object's default parser.
  119. *
  120. * @return this object's default parser.
  121. */
  122. FTPFileEntryParser getParser()
  123. {
  124. return this.parser;
  125. }
  126. /**
  127. * Package private accessor for the collection of raw input lines.
  128. *
  129. * @return vector containing all the raw input lines returned from the FTP
  130. * server
  131. */
  132. List getLines()
  133. {
  134. return this.lines;
  135. }
  136. /**
  137. * create an iterator over this list using the parser with which this list
  138. * was initally created
  139. *
  140. * @return an iterator over this list using the list's default parser.
  141. */
  142. public FTPFileIterator iterator()
  143. {
  144. return new FTPFileIterator(this);
  145. }
  146. /**
  147. * create an iterator over this list using the supplied parser
  148. *
  149. * @param parser The user-supplied parser with which the list is to be
  150. * iterated, may be different from this list's default parser.
  151. *
  152. * @return an iterator over this list using the supplied parser.
  153. */
  154. public FTPFileIterator iterator(FTPFileEntryParser parser)
  155. {
  156. return new FTPFileIterator(this, parser);
  157. }
  158. /**
  159. * returns an array of FTPFile objects for all the files in the directory
  160. * listing
  161. *
  162. * @return an array of FTPFile objects for all the files in the directory
  163. * listinge
  164. */
  165. public FTPFile[] getFiles()
  166. {
  167. return iterator().getFiles();
  168. }
  169. }
  170. /* Emacs configuration
  171. * Local variables: **
  172. * mode: java **
  173. * c-basic-offset: 4 **
  174. * indent-tabs-mode: nil **
  175. * End: **
  176. */