View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.nntp;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.util.Iterator;
23  import java.util.NoSuchElementException;
24  
25  import org.apache.commons.net.io.DotTerminatedMessageReader;
26  import org.apache.commons.net.io.Util;
27  
28  /**
29   * Wraps a {@link BufferedReader} and returns an {@code Iterable<String>} which returns the individual lines from the reader.
30   *
31   * @since 3.0
32   */
33  class ReplyIterator implements Iterator<String>, Iterable<String> {
34  
35      private final BufferedReader reader;
36  
37      private String line;
38  
39      private Exception savedException;
40  
41      ReplyIterator(final BufferedReader _reader) throws IOException {
42          this(_reader, true);
43      }
44  
45      /**
46       *
47       * @param _reader      the reader to wrap
48       * @param addDotReader whether to additionally wrap the reader in a DotTerminatedMessageReader
49       * @throws IOException
50       */
51      ReplyIterator(final BufferedReader _reader, final boolean addDotReader) throws IOException {
52          reader = addDotReader ? new DotTerminatedMessageReader(_reader) : _reader;
53          line = reader.readLine(); // prime the iterator
54          if (line == null) {
55              Util.closeQuietly(reader);
56          }
57      }
58  
59      @Override
60      public boolean hasNext() {
61          if (savedException != null) {
62              throw new NoSuchElementException(savedException.toString());
63          }
64          return line != null;
65      }
66  
67      @Override
68      public Iterator<String> iterator() {
69          return this;
70      }
71  
72      @Override
73      public String next() throws NoSuchElementException {
74          if (savedException != null) {
75              throw new NoSuchElementException(savedException.toString());
76          }
77          final String prev = line;
78          if (prev == null) {
79              throw new NoSuchElementException();
80          }
81          try {
82              line = reader.readLine(); // save next line
83              if (line == null) {
84                  Util.closeQuietly(reader);
85              }
86          } catch (final IOException ex) {
87              savedException = ex; // if it fails, save the exception, as it does not apply to this call
88              Util.closeQuietly(reader);
89          }
90          return prev;
91      }
92  
93      @Override
94      public void remove() {
95          throw new UnsupportedOperationException();
96      }
97  }