/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package examples.nntp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import org.apache.commons.net.nntp.Article;
import org.apache.commons.net.nntp.NNTPClient;

/**
 *
 * Some convenience methods for NNTP example classes.
 *
 * @author Rory Winston <rwinston@checkfree.com>
 */
public class NNTPUtils {

    /**
     * Given an {@link NNTPClient} instance, and an integer range of messages, return
     * an array of {@link Article} instances.
     * @param client
     * @param lowArticleNumber
     * @param highArticleNumber
     * @return Article[] An array of Article
     * @throws IOException
     */
    public  static List<Article> getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
    throws IOException {
        Reader reader = null;
        List<Article> articles = new ArrayList<Article>();
        reader = client.retrieveArticleInfo(
                    lowArticleNumber,
                    highArticleNumber);

        if (reader != null) {
            String theInfo = readerToString(reader);
            StringTokenizer st = new StringTokenizer(theInfo, "\n");

            // Extract the article information
            // Mandatory format (from NNTP RFC 2980) is :
            // Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count

            while (st.hasMoreTokens()) {
                String msg = st.nextToken();
                System.out.println("Message:" + msg);
                StringTokenizer stt = new StringTokenizer(msg, "\t");

                try {
                    Article article = new Article();
                    article.setArticleNumber(Integer.parseInt(stt.nextToken()));
                    article.setSubject(stt.nextToken());
                    article.setFrom(stt.nextToken());
                    article.setDate(stt.nextToken());
                    article.setArticleId(stt.nextToken());
                    article.addHeaderField("References", stt.nextToken());
                    articles.add(article);
                }
                catch (NoSuchElementException nse) {
                    // ignore this message
                }
            }
        } else {
            return null;
        }

        return articles;
    }


    /**
     * Convert a {@link Reader} instance to a String
     * @param reader The Reader instance
     * @return String
     */
    public static String readerToString(Reader reader) {
        String temp = null;
        BufferedReader bufReader = new BufferedReader(reader);
        StringBuilder sb = new StringBuilder();
        try {
            temp = bufReader.readLine();
            while (temp != null) {
                sb.append(temp);
                sb.append("\n");
                temp = bufReader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }
}

@@ -957,84 +957,8 @@
 st;%0A
-import java.util.NoSuchElementException;%0Aimport java.util.StringTokenizer;%0A%0A
 impo
@@ -1888,115 +1888,60 @@
     
-String theInfo = readerToString(reader);%0A            StringTokenizer st = new StringTokenizer(theInfo, %22%5Cn%22
+BufferedReader bufReader = new BufferedReader(reader
 );%0A%0A
@@ -2060,16 +2060,31 @@
      // 
+articleNumber%5Ct
 Subject%5C
@@ -2156,81 +2156,76 @@
     
-while (st.hasMoreTokens()) %7B%0A                String msg = st.nextToken();
+String msg;%0A            while ((msg=bufReader.readLine()) != null) %7B
 %0A   
@@ -2301,56 +2301,75 @@
 ring
-Tokenizer stt = new StringTokenizer(msg, %22%5Ct%22);%0A
+ parts%5B%5D = msg.split(%22%5Ct%22);%0A                if (parts.length %3E 6) %7B
 %0A   
@@ -2373,37 +2373,46 @@
                 
-try %7B
+    int i = 0;
 %0A               
@@ -2511,31 +2511,26 @@
 arseInt(
-stt.nextToken()
+parts%5Bi++%5D
 ));%0A    
@@ -2564,31 +2564,26 @@
 Subject(
-stt.nextToken()
+parts%5Bi++%5D
 );%0A     
@@ -2613,31 +2613,26 @@
 setFrom(
-stt.nextToken()
+parts%5Bi++%5D
 );%0A     
@@ -2662,31 +2662,26 @@
 setDate(
-stt.nextToken()
+parts%5Bi++%5D
 );%0A     
@@ -2716,31 +2716,26 @@
 ticleId(
-stt.nextToken()
+parts%5Bi++%5D
 );%0A     
@@ -2764,49 +2764,28 @@
 .add
-HeaderField(%22References%22, stt.nextToken()
+Reference(parts%5Bi++%5D
 );%0A 
@@ -2860,859 +2860,43 @@
     
-    catch (NoSuchElementException nse) %7B%0A                    // ignore this message%0A                %7D%0A            %7D%0A        %7D else %7B%0A            return null;%0A        %7D%0A%0A        return articles;%0A    %7D%0A%0A%0A    /**%0A     * Convert a %7B@link Reader%7D instance to a String%0A     * @param reader The Reader instance%0A     * @return String%0A     */%0A    public static String readerToString(Reader reader) %7B%0A        String temp = null;%0A        BufferedReader bufReader = new BufferedReader(reader);%0A        StringBuilder sb = new StringBuilder();%0A        try %7B%0A            temp = bufReader.readLine();%0A            while (temp != null) %7B%0A                sb.append(temp);%0A                sb.append(%22%5Cn%22);%0A                temp = bufReader.readLine();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return sb.toString()
+%7D%0A        %7D%0A        return articles
 ;%0A  

Fix bug - not saving references correctly Also Tokenize was not detecting missing references - adjacent tabs did not generate null token. Improve efficiency by not converting to string and back again