1.1 --- a/src/org/sonews/util/io/ArticleReader.java Sun Aug 29 17:28:58 2010 +0200
1.2 +++ b/src/org/sonews/util/io/ArticleReader.java Sun Oct 16 22:36:46 2011 +0200
1.3 @@ -34,102 +34,87 @@
1.4 * @author Christian Lins
1.5 * @since sonews/0.5.0
1.6 */
1.7 -public class ArticleReader
1.8 +public class ArticleReader
1.9 {
1.10
1.11 - private BufferedOutputStream out;
1.12 - private BufferedInputStream in;
1.13 - private String messageID;
1.14 -
1.15 - public ArticleReader(String host, int port, String messageID)
1.16 - throws IOException, UnknownHostException
1.17 - {
1.18 - this.messageID = messageID;
1.19 + private BufferedOutputStream out;
1.20 + private BufferedInputStream in;
1.21 + private String messageID;
1.22
1.23 - // Connect to NNTP server
1.24 - Socket socket = new Socket(host, port);
1.25 - this.out = new BufferedOutputStream(socket.getOutputStream());
1.26 - this.in = new BufferedInputStream(socket.getInputStream());
1.27 - String line = readln(this.in);
1.28 - if(!line.startsWith("200 "))
1.29 - {
1.30 - throw new IOException("Invalid hello from server: " + line);
1.31 - }
1.32 - }
1.33 -
1.34 - private boolean eofArticle(byte[] buf)
1.35 - {
1.36 - if(buf.length < 4)
1.37 - {
1.38 - return false;
1.39 - }
1.40 -
1.41 - int l = buf.length - 1;
1.42 - return buf[l-3] == 10 // '*\n'
1.43 - && buf[l-2] == '.' // '.'
1.44 - && buf[l-1] == 13 && buf[l] == 10; // '\r\n'
1.45 - }
1.46 -
1.47 - public byte[] getArticleData()
1.48 - throws IOException, UnsupportedEncodingException
1.49 - {
1.50 - long maxSize = Config.inst().get(Config.ARTICLE_MAXSIZE, 1024) * 1024L;
1.51 + public ArticleReader(String host, int port, String messageID)
1.52 + throws IOException, UnknownHostException
1.53 + {
1.54 + this.messageID = messageID;
1.55
1.56 - try
1.57 - {
1.58 - this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
1.59 - this.out.flush();
1.60 + // Connect to NNTP server
1.61 + Socket socket = new Socket(host, port);
1.62 + this.out = new BufferedOutputStream(socket.getOutputStream());
1.63 + this.in = new BufferedInputStream(socket.getInputStream());
1.64 + String line = readln(this.in);
1.65 + if (!line.startsWith("200 ")) {
1.66 + throw new IOException("Invalid hello from server: " + line);
1.67 + }
1.68 + }
1.69
1.70 - String line = readln(this.in);
1.71 - if(line.startsWith("220 "))
1.72 - {
1.73 - ByteArrayOutputStream buf = new ByteArrayOutputStream();
1.74 -
1.75 - while(!eofArticle(buf.toByteArray()))
1.76 - {
1.77 - for(int b = in.read(); b != 10; b = in.read())
1.78 - {
1.79 - buf.write(b);
1.80 - }
1.81 + private boolean eofArticle(byte[] buf)
1.82 + {
1.83 + if (buf.length < 4) {
1.84 + return false;
1.85 + }
1.86
1.87 - buf.write(10);
1.88 - if(buf.size() > maxSize)
1.89 - {
1.90 - Log.get().warning("Skipping message that is too large: " + buf.size());
1.91 - return null;
1.92 - }
1.93 - }
1.94 -
1.95 - return buf.toByteArray();
1.96 - }
1.97 - else
1.98 - {
1.99 - Log.get().warning("ArticleReader: " + line);
1.100 - return null;
1.101 - }
1.102 - }
1.103 - catch(IOException ex)
1.104 - {
1.105 - throw ex;
1.106 - }
1.107 - finally
1.108 - {
1.109 - this.out.write("QUIT\r\n".getBytes("UTF-8"));
1.110 - this.out.flush();
1.111 - this.out.close();
1.112 - }
1.113 - }
1.114 -
1.115 - private String readln(InputStream in)
1.116 - throws IOException
1.117 - {
1.118 - ByteArrayOutputStream buf = new ByteArrayOutputStream();
1.119 - for(int b = in.read(); b != 10 /* \n */; b = in.read())
1.120 - {
1.121 - buf.write(b);
1.122 - }
1.123 -
1.124 - return new String(buf.toByteArray());
1.125 - }
1.126 + int l = buf.length - 1;
1.127 + return buf[l - 3] == 10 // '*\n'
1.128 + && buf[l - 2] == '.' // '.'
1.129 + && buf[l - 1] == 13 && buf[l] == 10; // '\r\n'
1.130 + }
1.131
1.132 + public byte[] getArticleData()
1.133 + throws IOException, UnsupportedEncodingException
1.134 + {
1.135 + long maxSize = Config.inst().get(Config.ARTICLE_MAXSIZE, 1024) * 1024L;
1.136 +
1.137 + try {
1.138 + this.out.write(("ARTICLE " + this.messageID + "\r\n").getBytes("UTF-8"));
1.139 + this.out.flush();
1.140 +
1.141 + String line = readln(this.in);
1.142 + if (line.startsWith("220 ")) {
1.143 + ByteArrayOutputStream buf = new ByteArrayOutputStream();
1.144 +
1.145 + while (!eofArticle(buf.toByteArray())) {
1.146 + for (int b = in.read(); b != 10; b = in.read()) {
1.147 + buf.write(b);
1.148 + }
1.149 +
1.150 + buf.write(10);
1.151 + if (buf.size() > maxSize) {
1.152 + Log.get().warning("Skipping message that is too large: " + buf.size());
1.153 + return null;
1.154 + }
1.155 + }
1.156 +
1.157 + return buf.toByteArray();
1.158 + } else {
1.159 + Log.get().warning("ArticleReader: " + line);
1.160 + return null;
1.161 + }
1.162 + } catch (IOException ex) {
1.163 + throw ex;
1.164 + } finally {
1.165 + this.out.write("QUIT\r\n".getBytes("UTF-8"));
1.166 + this.out.flush();
1.167 + this.out.close();
1.168 + }
1.169 + }
1.170 +
1.171 + private String readln(InputStream in)
1.172 + throws IOException
1.173 + {
1.174 + ByteArrayOutputStream buf = new ByteArrayOutputStream();
1.175 + for (int b = in.read(); b != 10 /* \n */; b = in.read()) {
1.176 + buf.write(b);
1.177 + }
1.178 +
1.179 + return new String(buf.toByteArray());
1.180 + }
1.181 }