1 package examples.nntp; 2 3 import java.io.BufferedReader ; 4 import java.io.IOException ; 5 import java.io.Reader ; 6 import java.util.StringTokenizer ; 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 18 public class NNTPUtils { 19 20 29 public static Article[] getArticleInfo(NNTPClient client, int lowArticleNumber, int highArticleNumber) 30 throws IOException { 31 Reader reader = null; 32 Article[] articles = null; 33 reader = 34 (DotTerminatedMessageReader) client.retrieveArticleInfo( 35 lowArticleNumber, 36 highArticleNumber); 37 38 if (reader != null) { 39 String theInfo = readerToString(reader); 40 StringTokenizer st = new StringTokenizer (theInfo, "\n"); 41 42 46 int count = st.countTokens(); 47 articles = new Article[count]; 48 int index = 0; 49 50 while (st.hasMoreTokens()) { 51 StringTokenizer stt = new StringTokenizer (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 74 public static String readerToString(Reader reader) { 75 String temp = null; 76 StringBuffer sb = null; 77 BufferedReader bufReader = new BufferedReader (reader); 78 79 sb = new StringBuffer (); 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 e) { 88 e.printStackTrace(); 89 } 90 91 return sb.toString(); 92 } 93 } 94 | Popular Tags |