41 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
42 |
* @version $Revision$ $Date$ |
* @version $Revision$ $Date$ |
43 |
*/ |
*/ |
44 |
public final class DirectoryListing |
public final class DirectoryListing implements Iterator |
|
implements Iterator |
|
45 |
{ |
{ |
46 |
|
|
47 |
private static final String DOT = "."; |
private static final String DOT = "."; |
|
|
|
|
private LineInputStream in; |
|
|
private boolean doneRead = false; |
|
|
private DirectoryEntry current; |
|
|
|
|
|
DirectoryListing(InputStream in) |
|
|
{ |
|
|
this.in = new LineInputStream(in); |
|
|
} |
|
48 |
|
|
49 |
/** |
private LineInputStream in; |
50 |
|
private boolean doneRead = false; |
51 |
|
private DirectoryEntry current; |
52 |
|
|
53 |
|
DirectoryListing(InputStream in) |
54 |
|
{ |
55 |
|
this.in = new LineInputStream(in); |
56 |
|
} |
57 |
|
|
58 |
|
/** |
59 |
* Indicates whether this listing contains more entries. |
* Indicates whether this listing contains more entries. |
60 |
*/ |
*/ |
61 |
public boolean hasNext() |
public boolean hasNext() |
62 |
{ |
{ |
63 |
try |
try |
64 |
{ |
{ |
65 |
fetch(); |
fetch(); |
66 |
} |
} |
67 |
catch (IOException e) |
catch(IOException e) |
68 |
{ |
{ |
69 |
return false; |
return false; |
70 |
} |
} |
71 |
return (current!=null); |
return (current != null); |
72 |
} |
} |
73 |
|
|
74 |
/** |
/** |
75 |
* @see #nextEntry |
* @see #nextEntry |
76 |
*/ |
*/ |
77 |
public Object next() |
public Object next() |
78 |
{ |
{ |
79 |
try |
try |
80 |
{ |
{ |
81 |
return nextEntry(); |
return nextEntry(); |
82 |
} |
} |
83 |
catch (IOException e) |
catch(IOException e) |
84 |
{ |
{ |
85 |
throw new NoSuchElementException("I/O error: "+e.getMessage()); |
throw new NoSuchElementException("I/O error: " + e.getMessage()); |
86 |
} |
} |
87 |
} |
} |
88 |
|
|
89 |
/** |
/** |
90 |
* This iterator is read-only. |
* This iterator is read-only. |
91 |
*/ |
*/ |
92 |
public void remove() |
public void remove() |
93 |
{ |
{ |
94 |
throw new UnsupportedOperationException(); |
throw new UnsupportedOperationException(); |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
98 |
* Returns the next entry in the directory listing. |
* Returns the next entry in the directory listing. |
99 |
*/ |
*/ |
100 |
public DirectoryEntry nextEntry() |
public DirectoryEntry nextEntry() throws IOException |
101 |
throws IOException |
{ |
102 |
{ |
fetch(); |
103 |
fetch(); |
if (current == null) |
104 |
if (current==null) |
throw new NoSuchElementException(); |
105 |
throw new NoSuchElementException(); |
doneRead = false; |
106 |
doneRead = false; |
return current; |
107 |
return current; |
} |
108 |
} |
|
109 |
|
void fetch() throws IOException |
110 |
void fetch() |
{ |
111 |
throws IOException |
if (doneRead) |
112 |
{ |
return; |
113 |
if (doneRead) |
String line = in.readLine(); |
114 |
return; |
if (DOT.equals(line)) |
115 |
String line = in.readLine(); |
current = null; |
116 |
if (DOT.equals(line)) |
else |
117 |
current = null; |
{ |
118 |
else |
// Parse line |
119 |
{ |
int type = DirectoryEntry.ERROR; |
120 |
// Parse line |
switch (line.charAt(0)) |
121 |
int type = DirectoryEntry.ERROR; |
{ |
122 |
switch (line.charAt(0)) |
case '0': |
123 |
{ |
type = DirectoryEntry.FILE; |
124 |
case '0': |
break; |
125 |
type = DirectoryEntry.FILE; |
case '1':type = DirectoryEntry.DIRECTORY; |
126 |
break; |
break; |
127 |
case '1': |
case '2':type = DirectoryEntry.CSO_PHONE_BOOK; |
128 |
type = DirectoryEntry.DIRECTORY; |
break; |
129 |
break; |
case '3':type = DirectoryEntry.ERROR; |
130 |
case '2': |
break; |
131 |
type = DirectoryEntry.CSO_PHONE_BOOK; |
case '4':type = DirectoryEntry.BINHEX; |
132 |
break; |
break; |
133 |
case '3': |
case '5':type = DirectoryEntry.DOS_ARCHIVE; |
134 |
type = DirectoryEntry.ERROR; |
break; |
135 |
break; |
case '6':type = DirectoryEntry.UUENCODED; |
136 |
case '4': |
break; |
137 |
type = DirectoryEntry.BINHEX; |
case '7':type = DirectoryEntry.INDEX_SEARCH; |
138 |
break; |
break; |
139 |
case '5': |
case '8':type = DirectoryEntry.TELNET; |
140 |
type = DirectoryEntry.DOS_ARCHIVE; |
break; |
141 |
break; |
case '9':type = DirectoryEntry.BINARY; |
142 |
case '6': |
break; |
143 |
type = DirectoryEntry.UUENCODED; |
case '+':type = DirectoryEntry.REDUNDANT; |
144 |
break; |
break; |
145 |
case '7': |
case 'T':type = DirectoryEntry.TN3270; |
146 |
type = DirectoryEntry.INDEX_SEARCH; |
break; |
147 |
break; |
case 'g':type = DirectoryEntry.GIF; |
148 |
case '8': |
break; |
149 |
type = DirectoryEntry.TELNET; |
case 'I':type = DirectoryEntry.IMAGE; |
150 |
break; |
break; |
151 |
case '9': |
} |
152 |
type = DirectoryEntry.BINARY; |
int start = 1; |
153 |
break; |
int end = line.indexOf('\t', start); |
154 |
case '+': |
if (end == -1) |
155 |
type = DirectoryEntry.REDUNDANT; |
throw new ProtocolException("Invalid directory entry: " + line); |
156 |
break; |
String title = line.substring(start, end); |
157 |
case 'T': |
start = end + 1; |
158 |
type = DirectoryEntry.TN3270; |
end = line.indexOf('\t', start); |
159 |
break; |
if (end == -1) |
160 |
case 'g': |
throw new ProtocolException("Invalid directory entry: " + line); |
161 |
type = DirectoryEntry.GIF; |
String selector = line.substring(start, end); |
162 |
break; |
start = end + 1; |
163 |
case 'I': |
end = line.indexOf('\t', start); |
164 |
type = DirectoryEntry.IMAGE; |
if (end == -1) |
165 |
break; |
throw new ProtocolException("Invalid directory entry: " + line); |
166 |
} |
String hostname = line.substring(start, end); |
167 |
int start = 1; |
start = end + 1; |
168 |
int end = line.indexOf('\t', start); |
int port = Integer.parseInt(line.substring(start)); |
169 |
if (end==-1) |
current = new DirectoryEntry(type, title, selector, hostname, port); |
170 |
throw new ProtocolException("Invalid directory entry: "+line); |
} |
171 |
String title = line.substring(start, end); |
} |
|
start = end+1; |
|
|
end = line.indexOf('\t', start); |
|
|
if (end==-1) |
|
|
throw new ProtocolException("Invalid directory entry: "+line); |
|
|
String selector = line.substring(start, end); |
|
|
start = end+1; |
|
|
end = line.indexOf('\t', start); |
|
|
if (end==-1) |
|
|
throw new ProtocolException("Invalid directory entry: "+line); |
|
|
String hostname = line.substring(start, end); |
|
|
start = end+1; |
|
|
int port = Integer.parseInt(line.substring(start)); |
|
|
current = new DirectoryEntry(type, title, selector, hostname, port); |
|
|
} |
|
|
} |
|
172 |
|
|
173 |
} |
} |