Reformat source code, new line length for comments (79), some trailing whitespace...
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / FcpMessage.java
1 /*
2  * jFCPlib - FcpMessage.java - Copyright © 2008 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.fcp;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 /**
31  * An FCP message. FCP messages consist of a name, an arbitrary amount of
32  * “fields” (i.e. key-value pairs), a message end marker, and optional payload
33  * data that follows the marker.
34  *
35  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36  */
37 public class FcpMessage implements Iterable<String> {
38
39         /** Constant for the linefeed. */
40         private static final String LINEFEED = "\r\n";
41
42         /** The name of the message. */
43         private final String name;
44
45         /** The fields of the message. */
46         private final Map<String, String> fields = new HashMap<String, String>();
47
48         /** The optional payload input stream. */
49         private InputStream payloadInputStream;
50
51         /**
52          * Creates a new FCP message with the given name.
53          *
54          * @param name
55          *            The name of the FCP message
56          */
57         public FcpMessage(String name) {
58                 this(name, null);
59         }
60
61         /**
62          * Creates a new FCP message with the given name and the given payload
63          * input stream. The payload input stream is not read until the message is
64          * sent to the node using {@link FcpConnection#sendMessage(FcpMessage)}.
65          *
66          * @param name
67          *            The name of the message
68          * @param payloadInputStream
69          *            The payload of the message
70          */
71         public FcpMessage(String name, InputStream payloadInputStream) {
72                 this.name = name;
73                 this.payloadInputStream = payloadInputStream;
74         }
75
76         /**
77          * Returns the name of the message.
78          *
79          * @return The name of the message
80          */
81         public String getName() {
82                 return name;
83         }
84
85         /**
86          * Checks whether this message has a field with the given name.
87          *
88          * @param field
89          *            The name of the field to check for
90          * @return <code>true</code> if the message has a field with the given
91          *         name, <code>false</code> otherwise
92          */
93         public boolean hasField(String field) {
94                 return fields.containsKey(field);
95         }
96
97         /**
98          * Sets the field with the given name to the given value. If the field
99          * already exists in this message it is overwritten.
100          *
101          * @param field
102          *            The name of the field
103          * @param value
104          *            The value of the field
105          */
106         public void setField(String field, String value) {
107                 if ((field == null) || (value == null)) {
108                         throw new NullPointerException(((field == null) ? "field " : "value ") + "must not be null");
109                 }
110                 fields.put(field, value);
111         }
112
113         /**
114          * Returns the value of the given field.
115          *
116          * @param field
117          *            The name of the field
118          * @return The value of the field, or <code>null</code> if there is no such
119          *         field
120          */
121         public String getField(String field) {
122                 return fields.get(field);
123         }
124
125         /**
126          * Returns all fields of this message.
127          *
128          * @return All fields of this message
129          */
130         public Map<String, String> getFields() {
131                 return Collections.unmodifiableMap(fields);
132         }
133
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         public Iterator<String> iterator() {
139                 return fields.keySet().iterator();
140         }
141
142         /**
143          * Sets the payload input stream of the message.
144          *
145          * @param payloadInputStream
146          *            The payload input stream
147          */
148         public void setPayloadInputStream(InputStream payloadInputStream) {
149                 this.payloadInputStream = payloadInputStream;
150         }
151
152         /**
153          * Writes this message to the given output stream. If the message has a
154          * payload (i.e. {@link #payloadInputStream} is not <code>null</code>) the
155          * payload is written to the given output stream after the message as well.
156          * That means that this method can only be called once because on the
157          * second invocation the payload input stream could not be read (again).
158          *
159          * @param outputStream
160          *            The output stream to write the message to
161          * @throws IOException
162          *             if an I/O error occurs
163          */
164         public void write(OutputStream outputStream) throws IOException {
165                 writeLine(outputStream, name);
166                 for (Entry<String, String> fieldEntry : fields.entrySet()) {
167                         writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue());
168                 }
169                 writeLine(outputStream, "EndMessage");
170                 outputStream.flush();
171                 if (payloadInputStream != null) {
172                         FcpUtils.copy(payloadInputStream, outputStream);
173                         outputStream.flush();
174                 }
175         }
176
177         //
178         // PRIVATE METHODS
179         //
180
181         /**
182          * Writes the given line (followed by {@link #LINEFEED} to the given output
183          * stream, using UTF-8 as encoding.
184          *
185          * @param outputStream
186          *            The output stream to write to
187          * @param line
188          *            The line to write
189          * @throws IOException
190          *             if an I/O error occurs
191          */
192         private void writeLine(OutputStream outputStream, String line) throws IOException {
193                 outputStream.write((line + LINEFEED).getBytes("UTF-8"));
194         }
195
196 }