ReplyIterator.java

  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. package org.apache.commons.net.nntp;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.util.Iterator;
  21. import java.util.NoSuchElementException;

  22. import org.apache.commons.net.io.DotTerminatedMessageReader;
  23. import org.apache.commons.net.io.Util;

  24. /**
  25.  * Wraps a {@link BufferedReader} and returns an {@code Iterable<String>} which returns the individual lines from the reader.
  26.  *
  27.  * @since 3.0
  28.  */
  29. final class ReplyIterator implements Iterator<String>, Iterable<String> {

  30.     private final BufferedReader reader;

  31.     private String line;

  32.     private Exception savedException;

  33.     ReplyIterator(final BufferedReader _reader) throws IOException {
  34.         this(_reader, true);
  35.     }

  36.     /**
  37.      *
  38.      * @param _reader      the reader to wrap
  39.      * @param addDotReader whether to additionally wrap the reader in a DotTerminatedMessageReader
  40.      * @throws IOException
  41.      */
  42.     ReplyIterator(final BufferedReader _reader, final boolean addDotReader) throws IOException {
  43.         reader = addDotReader ? new DotTerminatedMessageReader(_reader) : _reader;
  44.         line = reader.readLine(); // prime the iterator
  45.         if (line == null) {
  46.             Util.closeQuietly(reader);
  47.         }
  48.     }

  49.     @Override
  50.     public boolean hasNext() {
  51.         if (savedException != null) {
  52.             throw new NoSuchElementException(savedException.toString());
  53.         }
  54.         return line != null;
  55.     }

  56.     @Override
  57.     public Iterator<String> iterator() {
  58.         return this;
  59.     }

  60.     @Override
  61.     public String next() throws NoSuchElementException {
  62.         if (savedException != null) {
  63.             throw new NoSuchElementException(savedException.toString());
  64.         }
  65.         final String prev = line;
  66.         if (prev == null) {
  67.             throw new NoSuchElementException();
  68.         }
  69.         try {
  70.             line = reader.readLine(); // save next line
  71.             if (line == null) {
  72.                 Util.closeQuietly(reader);
  73.             }
  74.         } catch (final IOException ex) {
  75.             savedException = ex; // if it fails, save the exception, as it does not apply to this call
  76.             Util.closeQuietly(reader);
  77.         }
  78.         return prev;
  79.     }

  80.     @Override
  81.     public void remove() {
  82.         throw new UnsupportedOperationException();
  83.     }
  84. }