src/org/sonews/util/io/ArticleWriter.java
changeset 42 7f84f4de2893
parent 35 ed84c8bdd87b
     1.1 --- a/src/org/sonews/util/io/ArticleWriter.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/util/io/ArticleWriter.java	Mon Jun 06 20:12:21 2011 +0200
     1.3 @@ -32,102 +32,97 @@
     1.4   * @author Christian Lins
     1.5   * @since sonews/0.5.0
     1.6   */
     1.7 -public class ArticleWriter 
     1.8 +public class ArticleWriter
     1.9  {
    1.10 -  
    1.11 -  private BufferedOutputStream out;
    1.12 -  private BufferedReader       inr;
    1.13  
    1.14 -  public ArticleWriter(String host, int port)
    1.15 -    throws IOException, UnknownHostException
    1.16 -  {
    1.17 -    // Connect to NNTP server
    1.18 -    Socket socket = new Socket(host, port);
    1.19 -    this.out = new BufferedOutputStream(socket.getOutputStream());
    1.20 -    this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.21 -    String line = inr.readLine();
    1.22 -    if(line == null || !line.startsWith("200 "))
    1.23 -    {
    1.24 -      throw new IOException("Invalid hello from server: " + line);
    1.25 -    }
    1.26 -  }
    1.27 -  
    1.28 -  public void close()
    1.29 -    throws IOException, UnsupportedEncodingException
    1.30 -  {
    1.31 -    this.out.write("QUIT\r\n".getBytes("UTF-8"));
    1.32 -    this.out.flush();
    1.33 -  }
    1.34 +	private BufferedOutputStream out;
    1.35 +	private BufferedReader inr;
    1.36  
    1.37 -  protected void finishPOST()
    1.38 -    throws IOException
    1.39 -  {
    1.40 -    this.out.write("\r\n.\r\n".getBytes());
    1.41 -    this.out.flush();
    1.42 -    String line = inr.readLine();
    1.43 -    if(line == null || !line.startsWith("240 ") || !line.startsWith("441 "))
    1.44 -    {
    1.45 -      throw new IOException(line);
    1.46 -    }
    1.47 -  }
    1.48 +	public ArticleWriter(String host, int port)
    1.49 +		throws IOException, UnknownHostException
    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.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    1.55 +		String line = inr.readLine();
    1.56 +		if (line == null || !line.startsWith("200 ")) {
    1.57 +			throw new IOException("Invalid hello from server: " + line);
    1.58 +		}
    1.59 +	}
    1.60  
    1.61 -  protected void preparePOST()
    1.62 -    throws IOException
    1.63 -  {
    1.64 -    this.out.write("POST\r\n".getBytes("UTF-8"));
    1.65 -    this.out.flush();
    1.66 +	public void close()
    1.67 +		throws IOException, UnsupportedEncodingException
    1.68 +	{
    1.69 +		this.out.write("QUIT\r\n".getBytes("UTF-8"));
    1.70 +		this.out.flush();
    1.71 +	}
    1.72  
    1.73 -    String line = this.inr.readLine();
    1.74 -    if(line == null || !line.startsWith("340 "))
    1.75 -    {
    1.76 -      throw new IOException(line);
    1.77 -    }
    1.78 -  }
    1.79 +	protected void finishPOST()
    1.80 +		throws IOException
    1.81 +	{
    1.82 +		this.out.write("\r\n.\r\n".getBytes());
    1.83 +		this.out.flush();
    1.84 +		String line = inr.readLine();
    1.85 +		if (line == null || !line.startsWith("240 ") || !line.startsWith("441 ")) {
    1.86 +			throw new IOException(line);
    1.87 +		}
    1.88 +	}
    1.89  
    1.90 -  public void writeArticle(Article article)
    1.91 -    throws IOException, UnsupportedEncodingException
    1.92 -  {
    1.93 -    byte[] buf = new byte[512];
    1.94 -    ArticleInputStream in = new ArticleInputStream(article);
    1.95 +	protected void preparePOST()
    1.96 +		throws IOException
    1.97 +	{
    1.98 +		this.out.write("POST\r\n".getBytes("UTF-8"));
    1.99 +		this.out.flush();
   1.100  
   1.101 -    preparePOST();
   1.102 -    
   1.103 -    int len = in.read(buf);
   1.104 -    while(len != -1)
   1.105 -    {
   1.106 -      writeLine(buf, len);
   1.107 -      len = in.read(buf);
   1.108 -    }
   1.109 +		String line = this.inr.readLine();
   1.110 +		if (line == null || !line.startsWith("340 ")) {
   1.111 +			throw new IOException(line);
   1.112 +		}
   1.113 +	}
   1.114  
   1.115 -    finishPOST();
   1.116 -  }
   1.117 +	public void writeArticle(Article article)
   1.118 +		throws IOException, UnsupportedEncodingException
   1.119 +	{
   1.120 +		byte[] buf = new byte[512];
   1.121 +		ArticleInputStream in = new ArticleInputStream(article);
   1.122  
   1.123 -  /**
   1.124 -   * Writes the raw content of an article to the remote server. This method
   1.125 -   * does no charset conversion/handling of any kind so its the preferred
   1.126 -   * method for sending an article to remote peers.
   1.127 -   * @param rawArticle
   1.128 -   * @throws IOException
   1.129 -   */
   1.130 -  public void writeArticle(byte[] rawArticle)
   1.131 -    throws IOException
   1.132 -  {
   1.133 -    preparePOST();
   1.134 -    writeLine(rawArticle, rawArticle.length);
   1.135 -    finishPOST();
   1.136 -  }
   1.137 +		preparePOST();
   1.138  
   1.139 -  /**
   1.140 -   * Writes the given buffer to the connect remote server.
   1.141 -   * @param buffer
   1.142 -   * @param len
   1.143 -   * @throws IOException
   1.144 -   */
   1.145 -  protected void writeLine(byte[] buffer, int len)
   1.146 -    throws IOException
   1.147 -  {
   1.148 -    this.out.write(buffer, 0, len);
   1.149 -    this.out.flush();
   1.150 -  }
   1.151 +		int len = in.read(buf);
   1.152 +		while (len != -1) {
   1.153 +			writeLine(buf, len);
   1.154 +			len = in.read(buf);
   1.155 +		}
   1.156  
   1.157 +		finishPOST();
   1.158 +	}
   1.159 +
   1.160 +	/**
   1.161 +	 * Writes the raw content of an article to the remote server. This method
   1.162 +	 * does no charset conversion/handling of any kind so its the preferred
   1.163 +	 * method for sending an article to remote peers.
   1.164 +	 * @param rawArticle
   1.165 +	 * @throws IOException
   1.166 +	 */
   1.167 +	public void writeArticle(byte[] rawArticle)
   1.168 +		throws IOException
   1.169 +	{
   1.170 +		preparePOST();
   1.171 +		writeLine(rawArticle, rawArticle.length);
   1.172 +		finishPOST();
   1.173 +	}
   1.174 +
   1.175 +	/**
   1.176 +	 * Writes the given buffer to the connect remote server.
   1.177 +	 * @param buffer
   1.178 +	 * @param len
   1.179 +	 * @throws IOException
   1.180 +	 */
   1.181 +	protected void writeLine(byte[] buffer, int len)
   1.182 +		throws IOException
   1.183 +	{
   1.184 +		this.out.write(buffer, 0, len);
   1.185 +		this.out.flush();
   1.186 +	}
   1.187  }