current state
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpConnectionHandler.java
1 /*
2  * jSite2 - FcpConnectionHandler.java -
3  * Copyright © 2008 David Roden
4  *
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 2 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.fcp;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.Charset;
26
27 /**
28  * TODO
29  * 
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  * @version $Id$
32  */
33 public class FcpConnectionHandler implements Runnable {
34
35         private final FcpConnection fcpConnection;
36         private final InputStream remoteInputStream;
37         
38         private boolean ignoreNextLinefeed;
39
40         public FcpConnectionHandler(FcpConnection fcpConnection, InputStream remoteInputStream) {
41                 this.fcpConnection = fcpConnection;
42                 this.remoteInputStream = remoteInputStream;
43         }
44
45         public void run() {
46                 FcpMessage fcpMessage = null;
47                 while (true) {
48                         try {
49                                 String line = readLine();
50                                 System.out.println("read line: " + line);
51                                 if (line == null) {
52                                         break;
53                                 }
54                                 if (line.length() == 0) {
55                                         continue;
56                                 }
57                                 line = line.trim();
58                                 if (fcpMessage == null) {
59                                         fcpMessage = new FcpMessage(line);
60                                         continue;
61                                 }
62                                 if ("EndMessage".equals(line)) {
63                                         fcpConnection.handleMessage(fcpMessage);
64                                         fcpMessage = null;
65                                 }
66                                 int equalSign = line.indexOf('=');
67                                 if (equalSign == -1) {
68                                         /* something's fishy! */
69                                         continue;
70                                 }
71                                 String field = line.substring(0, equalSign);
72                                 String value = line.substring(equalSign + 1);
73                                 fcpMessage.setField(field, value);
74                         } catch (IOException e) {
75                         }
76                 }
77         }
78
79         /**
80          * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are
81          * encountered and decodes the read bytes using UTF-8.
82          * 
83          * @return The decoded line
84          * @throws IOException
85          *             if an I/O error occurs
86          */
87         private String readLine() throws IOException {
88                 byte[] readBytes = new byte[512];
89                 int readIndex = 0;
90                 while (true) {
91                         int nextByte = remoteInputStream.read();
92                         if (nextByte == -1) {
93                                 if (readIndex == 0) {
94                                         return null;
95                                 }
96                                 break;
97                         }
98                         if (nextByte == 10) {
99                                 if (!ignoreNextLinefeed) {
100                                         break;
101                                 }
102                         }
103                         ignoreNextLinefeed = false;
104                         if (nextByte == 13) {
105                                 ignoreNextLinefeed = true;
106                                 break;
107                         }
108                         if (readIndex == readBytes.length) {
109                                 /* recopy & enlarge array */
110                                 byte[] newReadBytes = new byte[readBytes.length * 2];
111                                 System.arraycopy(readBytes, 0, newReadBytes, 0, readBytes.length);
112                                 readBytes = newReadBytes;
113                         }
114                         readBytes[readIndex++] = (byte) nextByte;
115                 }
116                 ByteBuffer byteBuffer = ByteBuffer.wrap(readBytes, 0, readIndex);
117                 return Charset.forName("UTF-8").decode(byteBuffer).toString();
118         }
119
120 }