2 * jSite2 - FcpMessage.java -
3 * Copyright © 2008 David Roden
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.
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.fcp;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
29 import java.util.Map.Entry;
32 * An FCP message. FCP messages consist of a name, an arbitrary amount of
33 * “fields” (i.e. key-value pairs), a message end marker, and optional payload
34 * data that follows the marker.
36 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39 public class FcpMessage implements Iterable<String> {
41 /** Constant for the linefeed. */
42 private static final String LINEFEED = "\r\n";
44 /** The name of the message. */
45 private final String name;
47 /** The fields of the message. */
48 private final Map<String, String> fields = new HashMap<String, String>();
50 /** The optional payload input stream. */
51 private InputStream payloadInputStream;
54 * Creates a new FCP message with the given name.
57 * The name of the FCP message
59 public FcpMessage(String name) {
64 * Creates a new FCP message with the given name and the given payload input
65 * stream. The payload input stream is not read until the message is sent to
66 * the node using {@link FcpConnection#sendMessage(FcpMessage)}.
69 * The name of the message
70 * @param payloadInputStream
71 * The payload of the message
73 public FcpMessage(String name, InputStream payloadInputStream) {
75 this.payloadInputStream = payloadInputStream;
79 * Returns the name of the message.
81 * @return The name of the message
83 public String getName() {
88 * Checks whether this message has a field with the given name.
91 * The name of the field to check for
92 * @return <code>true</code> if the message has a field with the given
93 * name, <code>false</code> otherwise
95 public boolean hasField(String field) {
96 return fields.containsKey(field);
100 * Sets the field with the given name to the given value. If the field
101 * already exists in this message it is overwritten.
104 * The name of the field
106 * The value of the field
108 public void setField(String field, String value) {
109 if ((field == null) || (value == null)) {
110 throw new NullPointerException(((field == null) ? "field " : "value ") + "must not be null");
112 fields.put(field, value);
116 * Returns the value of the given field.
119 * The name of the field
120 * @return The value of the field, or <code>null</code> if there is no
123 public String getField(String field) {
124 return fields.get(field);
128 * Returns all fields of this message.
130 * @return All fields of this message
132 public Map<String, String> getFields() {
133 return Collections.unmodifiableMap(fields);
139 public Iterator<String> iterator() {
140 return fields.keySet().iterator();
144 * Sets the payload input stream of the message.
146 * @param payloadInputStream
147 * The payload input stream
149 public void setPayloadInputStream(InputStream payloadInputStream) {
150 this.payloadInputStream = payloadInputStream;
154 * Writes this message to the given output stream. If the message has a
155 * payload (i.e. {@link #payloadInputStream} is not <code>null</code>)
156 * the payload is written to the given output stream after the message as
157 * well. That means that this method can only be called once because on the
158 * second invocation the payload input stream could not be read (again).
160 * @param outputStream
161 * The output stream to write the message to
162 * @throws IOException
163 * if an I/O error occurs
165 public void write(OutputStream outputStream) throws IOException {
166 writeLine(outputStream, name);
167 for (Entry<String, String> fieldEntry: fields.entrySet()) {
168 writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue());
170 writeLine(outputStream, "EndMessage");
171 outputStream.flush();
172 if (payloadInputStream != null) {
173 FcpUtils.copy(payloadInputStream, outputStream);
175 outputStream.flush();
183 * Writes the given line (followed by {@link #LINEFEED} to the given output
184 * stream, using UTF-8 as encoding.
186 * @param outputStream
187 * The output stream to write to
190 * @throws IOException
191 * if an I/O error occurs
193 private void writeLine(OutputStream outputStream, String line) throws IOException {
194 outputStream.write((line + LINEFEED).getBytes("UTF-8"));