From 561d15670607ad2fae532e71dfc25b35848f0775 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 12 Apr 2008 18:21:36 +0000 Subject: [PATCH] simplify creation of payload input stream git-svn-id: http://trooper/svn/projects/jSite/trunk@733 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/util/fcp/FcpConnection.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/net/pterodactylus/util/fcp/FcpConnection.java b/src/net/pterodactylus/util/fcp/FcpConnection.java index 97d3b67..12d52af 100644 --- a/src/net/pterodactylus/util/fcp/FcpConnection.java +++ b/src/net/pterodactylus/util/fcp/FcpConnection.java @@ -656,18 +656,11 @@ public class FcpConnection { } else if ("IdentifierCollision".equals(messageName)) { fireReceivedIdentifierCollision(new IdentifierCollision(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); + LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("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)) { @@ -732,4 +725,18 @@ public class FcpConnection { incomingMessageStatistics.put(name, oldValue + 1); } + /** + * Returns a limited input stream from the node’s input stream. + * + * @param dataLength + * The length of the stream + * @return The limited input stream + */ + private LimitedInputStream getInputStream(long dataLength) { + if (dataLength <= 0) { + return new LimitedInputStream(null, 0); + } + return new LimitedInputStream(remoteInputStream, dataLength); + } + } -- 2.7.4