current state
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpMessage.java
1 /*
2  * jSite2 - FcpMessage.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.io.OutputStream;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import net.pterodactylus.util.io.StreamCopier;
32
33 /**
34  * TODO
35  * 
36  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
37  * @version $Id$
38  */
39 public class FcpMessage implements Iterable<String> {
40
41         private static final String LINEFEED = "\r\n";
42
43         private final String name;
44         private final Map<String, String> fields = new HashMap<String, String>();
45         private InputStream dataInputStream;
46
47         public FcpMessage(String name) {
48                 this(name, null);
49         }
50
51         public FcpMessage(String name, InputStream dataInputStream) {
52                 this.name = name;
53                 this.dataInputStream = dataInputStream;
54         }
55
56         public String getName() {
57                 return name;
58         }
59         
60         public void setField(String field, String value) {
61                 fields.put(field, value);
62         }
63         
64         public String getField(String field) {
65                 return fields.get(field);
66         }
67         
68         public Map<String, String> getFields() {
69                 return Collections.unmodifiableMap(fields);
70         }
71         
72         /**
73          * {@inheritDoc}
74          */
75         public Iterator<String> iterator() {
76                 return fields.keySet().iterator();
77         }
78         
79         public void setDataInputStream(InputStream dataInputStream) {
80                 this.dataInputStream = dataInputStream;
81         }
82
83         public void write(OutputStream outputStream) throws IOException {
84                 writeLine(outputStream, name);
85                 for (Entry<String, String> fieldEntry: fields.entrySet()) {
86                         writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue());
87                 }
88                 writeLine(outputStream, "EndMessage");
89                 outputStream.flush();
90                 if (dataInputStream != null) {
91                         StreamCopier.copy(dataInputStream, outputStream);
92                 }
93                 outputStream.flush();
94         }
95
96         private void writeLine(OutputStream outputStream, String line) throws IOException {
97                 outputStream.write((line + LINEFEED).getBytes("UTF-8"));
98         }
99
100 }