KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > nntp > NNTPUtils


1 package examples.nntp;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.Reader JavaDoc;
6 import java.util.StringTokenizer JavaDoc;
7
8 import org.apache.commons.net.io.DotTerminatedMessageReader;
9 import org.apache.commons.net.nntp.Article;
10 import org.apache.commons.net.nntp.NNTPClient;
11
12 /**
13  *
14  * Some convenience methods for NNTP example classes.
15  *
16  * @author Rory Winston <rwinston@checkfree.com>
17  */

18 public class NNTPUtils {
19
20     /**
21      * Given an {@link NNTPClient} instance, and an integer range of messages, return
22      * an array of {@link Article} instances.
23      * @param client
24      * @param lowArticleNumber
25      * @param highArticleNumber
26      * @return Article[] An array of Article
27      * @throws IOException
28      */

29     public static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber)
30             throws IOException JavaDoc {
31             Reader JavaDoc reader = null;
32             Article[] articles = null;
33             reader =
34                 (DotTerminatedMessageReader) client.retrieveArticleInfo(
35                     lowArticleNumber,
36                     highArticleNumber);
37
38             if (reader != null) {
39                 String JavaDoc theInfo = readerToString(reader);
40                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(theInfo, "\n");
41
42                 // Extract the article information
43
// Mandatory format (from NNTP RFC 2980) is :
44
// Subject\tAuthor\tDate\tID\tReference(s)\tByte Count\tLine Count
45

46                 int count = st.countTokens();
47                 articles = new Article[count];
48                 int index = 0;
49
50                 while (st.hasMoreTokens()) {
51                     StringTokenizer JavaDoc stt = new StringTokenizer JavaDoc(st.nextToken(), "\t");
52                     Article article = new Article();
53                     article.setArticleNumber(Integer.parseInt(stt.nextToken()));
54                     article.setSubject(stt.nextToken());
55                     article.setFrom(stt.nextToken());
56                     article.setDate(stt.nextToken());
57                     article.setArticleId(stt.nextToken());
58                     article.addHeaderField("References", stt.nextToken());
59                     articles[index++] = article;
60                 }
61             } else {
62                 return null;
63             }
64
65             return articles;
66         }
67         
68     
69     /**
70      * Convert a {@link Reader} instance to a String
71      * @param reader The Reader instance
72      * @return String
73      */

74     public static String JavaDoc readerToString(Reader JavaDoc reader) {
75         String JavaDoc temp = null;
76         StringBuffer JavaDoc sb = null;
77         BufferedReader JavaDoc bufReader = new BufferedReader JavaDoc(reader);
78
79         sb = new StringBuffer JavaDoc();
80         try {
81             temp = bufReader.readLine();
82             while (temp != null) {
83                 sb.append(temp);
84                 sb.append("\n");
85                 temp = bufReader.readLine();
86             }
87         } catch (IOException JavaDoc e) {
88             e.printStackTrace();
89         }
90
91         return sb.toString();
92     }
93 }
94
Popular Tags