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 ")) {
50 throw new IOException("Invalid hello from server: " + line);
55 throws IOException, UnsupportedEncodingException
57 this.out.write("QUIT\r\n".getBytes("UTF-8"));
61 protected void finishPOST()
64 this.out.write("\r\n.\r\n".getBytes());
66 String line = inr.readLine();
67 if (line == null || !line.startsWith("240 ") || !line.startsWith("441 ")) {
68 throw new IOException(line);
72 protected void preparePOST()
75 this.out.write("POST\r\n".getBytes("UTF-8"));
78 String line = this.inr.readLine();
79 if (line == null || !line.startsWith("340 ")) {
80 throw new IOException(line);
84 public void writeArticle(Article article)
85 throws IOException, UnsupportedEncodingException
87 byte[] buf = new byte[512];
88 ArticleInputStream in = new ArticleInputStream(article);
92 int len = in.read(buf);
102 * Writes the raw content of an article to the remote server. This method
103 * does no charset conversion/handling of any kind so its the preferred
104 * method for sending an article to remote peers.
106 * @throws IOException
108 public void writeArticle(byte[] rawArticle)
112 writeLine(rawArticle, rawArticle.length);
117 * Writes the given buffer to the connect remote server.
120 * @throws IOException
122 protected void writeLine(byte[] buffer, int len)
125 this.out.write(buffer, 0, len);