1 |
/* |
/* |
2 |
* $Id$ |
* LineIterator.java |
3 |
* Copyright (C) 2002 The Free Software Foundation |
* Copyright (C) 2002 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
46 |
* An iterator over an NNTP multi-line response. |
* An iterator over an NNTP multi-line response. |
47 |
* |
* |
48 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
49 |
*/ |
*/ |
50 |
public class LineIterator implements Iterator, PendingData |
public class LineIterator |
51 |
|
implements Iterator, PendingData |
52 |
{ |
{ |
53 |
|
|
54 |
static final String DOT = "."; |
static final String DOT = "."; |
57 |
NNTPConnection connection; |
NNTPConnection connection; |
58 |
String current; |
String current; |
59 |
|
|
60 |
LineIterator (NNTPConnection connection) |
LineIterator(NNTPConnection connection) |
61 |
{ |
{ |
62 |
this.connection = connection; |
this.connection = connection; |
63 |
} |
} |
64 |
|
|
65 |
void doRead () throws IOException |
void doRead() |
66 |
|
throws IOException |
67 |
{ |
{ |
68 |
if (doneRead) |
if (doneRead) |
69 |
{ |
{ |
70 |
return; |
return; |
71 |
} |
} |
72 |
String line = connection.read (); |
String line = connection.read(); |
73 |
if (DOT.equals (line)) |
if (DOT.equals(line)) |
74 |
{ |
{ |
75 |
current = null; |
current = null; |
76 |
} |
} |
84 |
/** |
/** |
85 |
* Indicates whether there are more lines to be read. |
* Indicates whether there are more lines to be read. |
86 |
*/ |
*/ |
87 |
public boolean hasNext () |
public boolean hasNext() |
88 |
{ |
{ |
89 |
try |
try |
90 |
{ |
{ |
91 |
doRead (); |
doRead(); |
92 |
} |
} |
93 |
catch (IOException e) |
catch (IOException e) |
94 |
{ |
{ |
95 |
return false; |
return false; |
96 |
} |
} |
97 |
return (current != null); |
return(current != null); |
98 |
} |
} |
99 |
|
|
100 |
/** |
/** |
101 |
* Returns the next line. |
* Returns the next line. |
102 |
*/ |
*/ |
103 |
public Object next () |
public Object next() |
104 |
{ |
{ |
105 |
try |
try |
106 |
{ |
{ |
107 |
return nextLine (); |
return nextLine(); |
108 |
} |
} |
109 |
catch (IOException e) |
catch (IOException e) |
110 |
{ |
{ |
111 |
throw new NoSuchElementException ("I/O error: " + e.getMessage ()); |
throw new NoSuchElementException("I/O error: " + e.getMessage()); |
112 |
} |
} |
113 |
} |
} |
114 |
|
|
115 |
/** |
/** |
116 |
* Returns the next line. |
* Returns the next line. |
117 |
*/ |
*/ |
118 |
public String nextLine () throws IOException |
public String nextLine() |
119 |
|
throws IOException |
120 |
{ |
{ |
121 |
doRead (); |
doRead(); |
122 |
if (current == null) |
if (current == null) |
123 |
{ |
{ |
124 |
throw new NoSuchElementException (); |
throw new NoSuchElementException(); |
125 |
} |
} |
126 |
doneRead = false; |
doneRead = false; |
127 |
return current; |
return current; |
130 |
/** |
/** |
131 |
* This iterator is read-only. |
* This iterator is read-only. |
132 |
*/ |
*/ |
133 |
public void remove () |
public void remove() |
134 |
{ |
{ |
135 |
throw new UnsupportedOperationException (); |
throw new UnsupportedOperationException(); |
136 |
} |
} |
137 |
|
|
138 |
/** |
/** |
139 |
* Read to the end of this iterator. |
* Read to the end of this iterator. |
140 |
*/ |
*/ |
141 |
public void readToEOF () throws IOException |
public void readToEOF() |
142 |
|
throws IOException |
143 |
{ |
{ |
144 |
do |
do |
145 |
{ |
{ |
146 |
doRead (); |
doRead(); |
147 |
} |
} |
148 |
while (current != null); |
while (current != null); |
149 |
} |
} |
150 |
|
|
151 |
} |
} |
152 |
|
|