e6ed7831ee0171a19d3eab4f85557421d4e5961f
[jFCPlib.git] / src / net / pterodactylus / fcp / AllData.java
1 /*
2  * jSite2 - AllData.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.fcp;
21
22 import java.io.InputStream;
23
24 /**
25  * The “AllData” message carries the payload of a successful {@link ClientGet}
26  * request. You will only received this message if the {@link ClientGet} request
27  * was started with a return type of {@link ReturnType#direct}. If you get this
28  * message and decide that the data is for you, call
29  * {@link #getPayloadInputStream()} to get the data. If an AllData message
30  * passes through all registered {@link FcpListener}s without the payload being
31  * consumed, the payload is discarded!
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 public class AllData extends BaseMessage {
36
37         /** The payload. */
38         private InputStream payloadInputStream;
39
40         /**
41          * Creates an “AllData” message that wraps the received message.
42          * 
43          * @param receivedMessage
44          *            The received message
45          * @param payloadInputStream
46          *            The payload
47          */
48         AllData(FcpMessage receivedMessage, InputStream payloadInputStream) {
49                 super(receivedMessage);
50                 this.payloadInputStream = payloadInputStream;
51         }
52
53         /**
54          * Returns the identifier of the request.
55          * 
56          * @return The identifier of the request
57          */
58         public String getIdentifier() {
59                 return getField("Identifier");
60         }
61
62         /**
63          * Returns the length of the data.
64          * 
65          * @return The length of the data, or <code>-1</code> if the length could
66          *         not be parsed
67          */
68         public long getDataLength() {
69                 return FcpUtils.safeParseLong(getField("DataLength"));
70         }
71
72         /**
73          * Returns the startup time of the request.
74          * 
75          * @return The startup time of the request (in milliseconds since Jan 1,
76          *         1970 UTC), or <code>-1</code> if the time could not be parsed
77          */
78         public long getStartupTime() {
79                 return FcpUtils.safeParseLong(getField("StartupTime"));
80         }
81
82         /**
83          * Returns the completion time of the request.
84          * 
85          * @return The completion time of the request (in milliseconds since Jan 1,
86          *         1970 UTC), or <code>-1</code> if the time could not be parsed
87          */
88         public long getCompletionTime() {
89                 return FcpUtils.safeParseLong(getField("CompletionTime"));
90         }
91
92         /**
93          * Returns the payload input stream. You <strong>have</strong> consume the
94          * input stream before returning from the
95          * {@link FcpListener#receivedAllData(FcpConnection, AllData)} method!
96          * 
97          * @return The payload
98          */
99         public InputStream getPayloadInputStream() {
100                 return payloadInputStream;
101         }
102
103 }