1 16 package org.apache.commons.net.ftp; 17 import java.util.List ; 18 19 33 public class FTPFileIterator 34 { 35 39 private List rawlines; 40 41 44 private FTPFileEntryParser parser; 45 46 50 private static final int UNINIT = -1; 51 52 56 private static final int DIREMPTY = -2; 57 58 61 private int itemptr = 0; 62 63 66 private int firstGoodEntry = UNINIT; 67 68 75 FTPFileIterator (FTPFileList rawlist) 76 { 77 this(rawlist, rawlist.getParser()); 78 } 79 80 88 FTPFileIterator (FTPFileList rawlist, 89 FTPFileEntryParser parser) 90 { 91 this.rawlines = rawlist.getLines(); 92 this.parser = parser; 93 } 94 95 105 private FTPFile parseFTPEntry(String entry) 106 { 107 return this.parser.parseFTPEntry(entry); 108 } 109 110 118 private int getFirstGoodEntry() 119 { 120 FTPFile entry = null; 121 for (int iter = 0; iter < this.rawlines.size(); iter++) 122 { 123 String line = (String ) this.rawlines.get(iter); 124 entry = parseFTPEntry(line); 125 if (null != entry) 126 { 127 return iter; 128 } 129 } 130 return DIREMPTY; 131 } 132 133 136 private void init() 137 { 138 this.itemptr = 0; 139 this.firstGoodEntry = UNINIT; 140 } 141 142 145 private static final FTPFile[] EMPTY = new FTPFile[0]; 146 147 154 public FTPFile[] getFiles() 155 { 156 if (this.itemptr != DIREMPTY) 157 { 158 init(); 159 } 160 return getNext(0); 161 } 162 163 184 public FTPFile[] getNext(int quantityRequested) 185 { 186 187 if (this.firstGoodEntry == UNINIT) 189 { 190 this.firstGoodEntry = getFirstGoodEntry(); 191 } 192 if (this.firstGoodEntry == DIREMPTY) 193 { 194 return EMPTY; 195 } 196 197 int max = this.rawlines.size() - this.firstGoodEntry; 198 199 202 int howMany = (quantityRequested == 0) ? max : quantityRequested; 203 howMany = (howMany + this.itemptr < this.rawlines.size()) 204 ? howMany 205 : this.rawlines.size() - this.itemptr; 206 207 FTPFile[] output = new FTPFile[howMany]; 208 209 for (int i = 0, e = this.firstGoodEntry + this.itemptr ; 210 i < howMany; i++, e++) 211 { 212 output[i] = parseFTPEntry((String ) this.rawlines.get(e)); 213 this.itemptr++; 214 } 215 return output; 216 } 217 218 225 public boolean hasNext() 226 { 227 int fge = this.firstGoodEntry; 228 if (fge == DIREMPTY) 229 { 230 return false; 232 } 233 else if (fge < 0) 234 { 235 fge = getFirstGoodEntry(); 237 } 238 return fge + this.itemptr < this.rawlines.size(); 239 } 240 241 252 public FTPFile next() 253 { 254 FTPFile[] file = getNext(1); 255 if (file.length > 0) 256 { 257 return file[0]; 258 } 259 else 260 { 261 return null; 262 } 263 } 264 265 285 public FTPFile[] getPrevious(int quantityRequested) 286 { 287 int howMany = quantityRequested; 288 if (howMany > this.itemptr) 290 { 291 howMany = this.itemptr; 292 } 293 FTPFile[] output = new FTPFile[howMany]; 294 for (int i = howMany, e = this.firstGoodEntry + this.itemptr; i > 0;) 295 { 296 output[--i] = parseFTPEntry((String ) this.rawlines.get(--e)); 297 this.itemptr--; 298 } 299 return output; 300 } 301 302 309 public boolean hasPrevious() 310 { 311 int fge = this.firstGoodEntry; 312 if (fge == DIREMPTY) 313 { 314 return false; 316 } 317 else if (fge < 0) 318 { 319 fge = getFirstGoodEntry(); 321 } 322 323 return this.itemptr > fge; 324 } 325 326 337 public FTPFile previous() 338 { 339 FTPFile[] file = getPrevious(1); 340 if (file.length > 0) 341 { 342 return file[0]; 343 } 344 else 345 { 346 return null; 347 } 348 } 349 } 350 351 358 | Popular Tags |