1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.net.nntp;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.NoSuchElementException;
25
26 import org.apache.commons.net.io.DotTerminatedMessageReader;
27 import org.apache.commons.net.io.Util;
28
29
30
31
32
33
34 class ReplyIterator implements Iterator<String>, Iterable<String> {
35
36 private final BufferedReader reader;
37
38 private String line;
39
40 private Exception savedException;
41
42
43
44
45
46
47
48 ReplyIterator(BufferedReader _reader, boolean addDotReader) throws IOException {
49 reader = addDotReader ? new DotTerminatedMessageReader(_reader) : _reader;
50 line = reader.readLine();
51 if (line == null) {
52 Util.closeQuietly(reader);
53 }
54 }
55
56 ReplyIterator(BufferedReader _reader) throws IOException {
57 this(_reader, true);
58 }
59
60
61 public boolean hasNext() {
62 if (savedException != null){
63 throw new NoSuchElementException(savedException.toString());
64 }
65 return line != null;
66 }
67
68
69 public String next() throws NoSuchElementException {
70 if (savedException != null){
71 throw new NoSuchElementException(savedException.toString());
72 }
73 String prev = line;
74 if (prev == null) {
75 throw new NoSuchElementException();
76 }
77 try {
78 line = reader.readLine();
79 if (line == null) {
80 Util.closeQuietly(reader);
81 }
82 } catch (IOException ex) {
83 savedException = ex;
84 Util.closeQuietly(reader);
85 }
86 return prev;
87 }
88
89
90 public void remove() {
91 throw new UnsupportedOperationException();
92 }
93
94
95 public Iterator<String> iterator() {
96 return this;
97 }
98 }