2 * jFCPlib - FcpMessage.java - Copyright © 2008 David Roden
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.
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.
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.
19 package net.pterodactylus.fcp;
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;
28 import java.util.Map.Entry;
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.
35 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37 public class FcpMessage implements Iterable<String> {
39 /** Constant for the linefeed. */
40 private static final String LINEFEED = "\r\n";
42 /** The name of the message. */
43 private final String name;
45 /** The fields of the message. */
46 private final Map<String, String> fields = new HashMap<String, String>();
48 /** The optional payload input stream. */
49 private InputStream payloadInputStream;
52 * Creates a new FCP message with the given name.
55 * The name of the FCP message
57 public FcpMessage(String name) {
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)}.
67 * The name of the message
68 * @param payloadInputStream
69 * The payload of the message
71 public FcpMessage(String name, InputStream payloadInputStream) {
73 this.payloadInputStream = payloadInputStream;
77 * Returns the name of the message.
79 * @return The name of the message
81 public String getName() {
86 * Checks whether this message has a field with the given name.
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
93 public boolean hasField(String field) {
94 return fields.containsKey(field);
98 * Sets the field with the given name to the given value. If the field
99 * already exists in this message it is overwritten.
102 * The name of the field
104 * The value of the field
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");
110 fields.put(field, value);
113 public FcpMessage put(String field, String value) {
114 setField(field, value);
119 * Returns the value of the given field.
122 * The name of the field
123 * @return The value of the field, or <code>null</code> if there is no such
126 public String getField(String field) {
127 return fields.get(field);
131 * Returns all fields of this message.
133 * @return All fields of this message
135 public Map<String, String> getFields() {
136 return Collections.unmodifiableMap(fields);
143 public Iterator<String> iterator() {
144 return fields.keySet().iterator();
148 * Sets the payload input stream of the message.
150 * @param payloadInputStream
151 * The payload input stream
153 public void setPayloadInputStream(InputStream payloadInputStream) {
154 this.payloadInputStream = payloadInputStream;
158 * Writes this message to the given output stream. If the message has a
159 * payload (i.e. {@link #payloadInputStream} is not <code>null</code>) the
160 * payload is written to the given output stream after the message as well.
161 * That means that this method can only be called once because on the
162 * second invocation the payload input stream could not be read (again).
164 * @param outputStream
165 * The output stream to write the message to
166 * @throws IOException
167 * if an I/O error occurs
169 public void write(OutputStream outputStream) throws IOException {
170 writeLine(outputStream, name);
171 for (Entry<String, String> fieldEntry : fields.entrySet()) {
172 writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue());
174 writeLine(outputStream, "EndMessage");
175 outputStream.flush();
176 if (payloadInputStream != null) {
177 FcpUtils.copy(payloadInputStream, outputStream);
178 outputStream.flush();
187 * Writes the given line (followed by {@link #LINEFEED} to the given output
188 * stream, using UTF-8 as encoding.
190 * @param outputStream
191 * The output stream to write to
194 * @throws IOException
195 * if an I/O error occurs
197 private void writeLine(OutputStream outputStream, String line) throws IOException {
198 outputStream.write((line + LINEFEED).getBytes("UTF-8"));