org/sonews/util/io/ArticleReader.java
changeset 2 1090e2141798
child 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/util/io/ArticleReader.java	Wed Jul 01 10:48:22 2009 +0200
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.util.io;
    1.23 +
    1.24 +import java.io.BufferedInputStream;
    1.25 +import java.io.BufferedOutputStream;
    1.26 +import java.io.ByteArrayOutputStream;
    1.27 +import java.io.IOException;
    1.28 +import java.io.InputStream;
    1.29 +import java.io.UnsupportedEncodingException;
    1.30 +import java.net.Socket;
    1.31 +import java.net.UnknownHostException;
    1.32 +import org.sonews.util.Log;
    1.33 +
    1.34 +/**
    1.35 + * Reads an news article from a NNTP server.
    1.36 + * @author Christian Lins
    1.37 + * @since sonews/0.5.0
    1.38 + */
    1.39 +public class ArticleReader 
    1.40 +{
    1.41 +
    1.42 +  private BufferedOutputStream out;
    1.43 +  private BufferedInputStream  in;
    1.44 +  private String               messageID;
    1.45 +  
    1.46 +  public ArticleReader(String host, int port, String messageID)
    1.47 +    throws IOException, UnknownHostException
    1.48 +  {
    1.49 +    this.messageID = messageID;
    1.50 +
    1.51 +    // Connect to NNTP server
    1.52 +    Socket socket = new Socket(host, port);
    1.53 +    this.out = new BufferedOutputStream(socket.getOutputStream());
    1.54 +    this.in  = new BufferedInputStream(socket.getInputStream());
    1.55 +    String line = readln(this.in);
    1.56 +    if(!line.startsWith("200 "))
    1.57 +    {
    1.58 +      throw new IOException("Invalid hello from server: " + line);
    1.59 +    }
    1.60 +  }
    1.61 +  
    1.62 +  private boolean eofArticle(byte[] buf)
    1.63 +  {
    1.64 +    if(buf.length < 4)
    1.65 +    {
    1.66 +      return false;
    1.67 +    }
    1.68 +    
    1.69 +    int l = buf.length - 1;
    1.70 +    return buf[l-3] == 10 // '*\n'
    1.71 +        && buf[l-2] == '.'                   // '.'
    1.72 +        && buf[l-1] == 13 && buf[l] == 10;  // '\r\n'
    1.73 +  }
    1.74 +  
    1.75 +  public byte[] getArticleData()
    1.76 +    throws IOException, UnsupportedEncodingException
    1.77 +  {
    1.78 +    try
    1.79 +    {
    1.80 +      this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
    1.81 +      this.out.flush();
    1.82 +
    1.83 +      String line = readln(this.in);
    1.84 +      if(line.startsWith("220 "))
    1.85 +      {
    1.86 +        ByteArrayOutputStream buf = new ByteArrayOutputStream();
    1.87 +        
    1.88 +        while(!eofArticle(buf.toByteArray()))
    1.89 +        {
    1.90 +          for(int b = in.read(); b != 10; b = in.read())
    1.91 +          {
    1.92 +            buf.write(b);
    1.93 +          }
    1.94 +
    1.95 +          buf.write(10);
    1.96 +        }
    1.97 +        
    1.98 +        return buf.toByteArray();
    1.99 +      }
   1.100 +      else
   1.101 +      {
   1.102 +        Log.msg("ArticleReader: " + line, false);
   1.103 +        return null;
   1.104 +      }
   1.105 +    }
   1.106 +    catch(IOException ex)
   1.107 +    {
   1.108 +      throw ex;
   1.109 +    }
   1.110 +    finally
   1.111 +    {
   1.112 +      this.out.write("QUIT\r\n".getBytes("UTF-8"));
   1.113 +      this.out.flush();
   1.114 +      this.out.close();
   1.115 +    }
   1.116 +  }
   1.117 +  
   1.118 +  private String readln(InputStream in)
   1.119 +    throws IOException
   1.120 +  {
   1.121 +    ByteArrayOutputStream buf = new ByteArrayOutputStream();
   1.122 +    for(int b = in.read(); b != 10 /* \n */; b = in.read())
   1.123 +    {
   1.124 +      buf.write(b);
   1.125 +    }
   1.126 +    
   1.127 +    return new String(buf.toByteArray());
   1.128 +  }
   1.129 +
   1.130 +}