--- /dev/null
+/**
+ * © 2008 INA Service GmbH
+ */
+package net.pterodactylus.util.fcp;
+
+import java.io.InputStream;
+
+/**
+ * The “AllData” message carries the payload of a successful {@link ClientGet}
+ * request. You will only received this message if the {@link ClientGet} request
+ * was started with a return type of {@link ReturnType#direct}. If you get this
+ * message and decide that the data is for you, call
+ * {@link #getPayloadInputStream()} to get the data. If an AllData message
+ * passes through all registered {@link FcpListener}s without the payload being
+ * consumed, the payload is discarded!
+ *
+ * @author <a href="mailto:dr@ina-germany.de">David Roden</a>
+ * @version $Id$
+ */
+public class AllData extends BaseMessage {
+
+ /** The payload. */
+ private InputStream payloadInputStream;
+
+ /**
+ * Creates an “AllData” message that wraps the received message.
+ *
+ * @param receivedMessage
+ * The received message
+ * @param payloadInputStream
+ * The payload
+ */
+ public AllData(FcpMessage receivedMessage, InputStream payloadInputStream) {
+ super(receivedMessage);
+ this.payloadInputStream = payloadInputStream;
+ }
+
+ /**
+ * Returns the identifier of the request.
+ *
+ * @return The identifier of the request
+ */
+ public String getIdentifier() {
+ return getField("Identifier");
+ }
+
+ /**
+ * Returns the length of the data.
+ *
+ * @return The length of the data, or <code>-1</code> if the length could
+ * not be parsed
+ */
+ public long getDataLength() {
+ try {
+ return Long.valueOf(getField("DataLength"));
+ } catch (NumberFormatException nfe1) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the startup time of the request.
+ *
+ * @return The startup time of the request (in milliseconds since Jan 1,
+ * 1970 UTC), or <code>-1</code> if the time could not be parsed
+ */
+ public long getStartupTime() {
+ try {
+ return Long.valueOf(getField("StartupTime"));
+ } catch (NumberFormatException nfe1) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the completion time of the request.
+ *
+ * @return The completion time of the request (in milliseconds since Jan 1,
+ * 1970 UTC), or <code>-1</code> if the time could not be parsed
+ */
+ public long getCompletionTime() {
+ try {
+ return Long.valueOf(getField("CompletionTime"));
+ } catch (NumberFormatException nfe1) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the payload input stream.
+ *
+ * @return The payload
+ */
+ public InputStream getPayloadInputStream() {
+ return payloadInputStream;
+ }
+
+}
import java.util.List;
import net.pterodactylus.util.io.Closer;
+import net.pterodactylus.util.io.LimitedInputStream;
/**
* An FCP connection to a Freenet node.
}
/**
+ * Notifies all listeners that an “AllData” message was received.
+ *
+ * @param allData
+ * The “AllData” message
+ */
+ private void fireReceivedAllData(AllData allData) {
+ for (FcpListener fcpListener: fcpListeners) {
+ fcpListener.receivedAllData(this, allData);
+ }
+ }
+
+ /**
* Notifies all listeners that a “ProtocolError” message was received.
*
* @param protocolError
fireReceivedPeer(new Peer(fcpMessage));
} else if ("PeerNote".equals(messageName)) {
fireReceivedPeerNote(new PeerNote(fcpMessage));
+ } else if ("AllData".equals(messageName)) {
+ long dataLength;
+ try {
+ dataLength = Long.valueOf(fcpMessage.getField("DataLength"));
+ } catch (NumberFormatException nfe1) {
+ dataLength = -1;
+ }
+ LimitedInputStream payloadInputStream = new LimitedInputStream(remoteInputStream, dataLength);
+ fireReceivedAllData(new AllData(fcpMessage, payloadInputStream));
+ try {
+ payloadInputStream.consume();
+ } catch (IOException ioe1) {
+ /* FIXME - what now? */
+ /* well, ignore. when the connection handler fails, all fails. */
+ }
} else if ("EndListPeerNotes".equals(messageName)) {
fireReceivedEndListPeerNotes(new EndListPeerNotes(fcpMessage));
} else if ("EndListPeers".equals(messageName)) {