#544 fixed.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.util.io;
21 import java.io.BufferedOutputStream;
22 import java.io.BufferedReader;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.UnsupportedEncodingException;
26 import java.net.Socket;
27 import java.net.UnknownHostException;
28 import org.sonews.storage.Article;
31 * Posts an Article to a NNTP server using the POST command.
32 * @author Christian Lins
35 public class ArticleWriter
38 private BufferedOutputStream out;
39 private BufferedReader inr;
41 public ArticleWriter(String host, int port)
42 throws IOException, UnknownHostException
44 // Connect to NNTP server
45 Socket socket = new Socket(host, port);
46 this.out = new BufferedOutputStream(socket.getOutputStream());
47 this.inr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
48 String line = inr.readLine();
49 if(line == null || !line.startsWith("200 "))
51 throw new IOException("Invalid hello from server: " + line);
56 throws IOException, UnsupportedEncodingException
58 this.out.write("QUIT\r\n".getBytes("UTF-8"));
62 protected void finishPOST()
65 this.out.write("\r\n.\r\n".getBytes());
67 String line = inr.readLine();
68 if(line == null || !line.startsWith("240 "))
70 throw new IOException(line);
74 protected void preparePOST()
77 this.out.write("POST\r\n".getBytes("UTF-8"));
80 String line = this.inr.readLine();
81 if(line == null || !line.startsWith("340 "))
83 throw new IOException(line);
87 public void writeArticle(Article article)
88 throws IOException, UnsupportedEncodingException
90 byte[] buf = new byte[512];
91 ArticleInputStream in = new ArticleInputStream(article);
95 int len = in.read(buf);
106 * Writes the raw content of an article to the remote server. This method
107 * does no charset conversion/handling of any kind so its the preferred
108 * method for sending an article to remote peers.
110 * @throws IOException
112 public void writeArticle(byte[] rawArticle)
116 writeLine(rawArticle, rawArticle.length);
121 * Writes the given buffer to the connect remote server.
124 * @throws IOException
126 protected void writeLine(byte[] buffer, int len)
129 this.out.write(buffer, 0, len);