1 16 package org.apache.commons.net.ftp; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 import java.util.ListIterator ; 26 27 28 73 public class FTPListParseEngine { 74 private List entries = new LinkedList (); 75 private ListIterator _internalIterator = entries.listIterator(); 76 77 FTPFileEntryParser parser = null; 78 79 public FTPListParseEngine(FTPFileEntryParser parser) { 80 this.parser = parser; 81 } 82 83 94 public void readServerList(InputStream stream, String encoding) 95 throws IOException 96 { 97 this.entries = new LinkedList (); 98 readStream(stream, encoding); 99 this.parser.preParse(this.entries); 100 resetIterator(); 101 } 102 103 116 public void readServerList(InputStream stream) 117 throws IOException 118 { 119 readServerList(stream, null); 120 } 121 122 123 124 138 private void readStream(InputStream stream, String encoding) throws IOException 139 { 140 BufferedReader reader; 141 if (encoding == null) 142 { 143 reader = new BufferedReader (new InputStreamReader (stream)); 144 } 145 else 146 { 147 reader = new BufferedReader (new InputStreamReader (stream, encoding)); 148 } 149 150 String line = this.parser.readNextEntry(reader); 151 152 while (line != null) 153 { 154 this.entries.add(line); 155 line = this.parser.readNextEntry(reader); 156 } 157 reader.close(); 158 } 159 160 183 public FTPFile[] getNext(int quantityRequested) { 184 List tmpResults = new LinkedList (); 185 int count = quantityRequested; 186 while (count > 0 && this._internalIterator.hasNext()) { 187 String entry = (String ) this._internalIterator.next(); 188 FTPFile temp = this.parser.parseFTPEntry(entry); 189 tmpResults.add(temp); 190 count--; 191 } 192 return (FTPFile[]) tmpResults.toArray(new FTPFile[0]); 193 194 } 195 196 222 public FTPFile[] getPrevious(int quantityRequested) { 223 List tmpResults = new LinkedList (); 224 int count = quantityRequested; 225 while (count > 0 && this._internalIterator.hasPrevious()) { 226 String entry = (String ) this._internalIterator.previous(); 227 FTPFile temp = this.parser.parseFTPEntry(entry); 228 tmpResults.add(0,temp); 229 count--; 230 } 231 return (FTPFile[]) tmpResults.toArray(new FTPFile[0]); 232 } 233 234 246 public FTPFile[] getFiles() 247 throws IOException 248 { 249 List tmpResults = new LinkedList (); 250 Iterator iter = this.entries.iterator(); 251 while (iter.hasNext()) { 252 String entry = (String ) iter.next(); 253 FTPFile temp = this.parser.parseFTPEntry(entry); 254 tmpResults.add(temp); 255 } 256 return (FTPFile[]) tmpResults.toArray(new FTPFile[0]); 257 258 } 259 260 267 public boolean hasNext() { 268 return _internalIterator.hasNext(); 269 } 270 271 278 public boolean hasPrevious() { 279 return _internalIterator.hasPrevious(); 280 } 281 282 285 public void resetIterator() { 286 this._internalIterator = this.entries.listIterator(); 287 } 288 } 289 | Popular Tags |