From: David ‘Bombe’ Roden Date: Sun, 11 Sep 2016 14:20:23 +0000 (+0200) Subject: End messages carrying data with “Data” X-Git-Tag: v0.1.5^2~3 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=14edf617ab865e3634f243c87ac1cb636c055b6c;p=jFCPlib.git End messages carrying data with “Data” --- diff --git a/src/main/java/net/pterodactylus/fcp/FcpMessage.java b/src/main/java/net/pterodactylus/fcp/FcpMessage.java index 921d74d..58f28a7 100644 --- a/src/main/java/net/pterodactylus/fcp/FcpMessage.java +++ b/src/main/java/net/pterodactylus/fcp/FcpMessage.java @@ -170,12 +170,13 @@ public class FcpMessage implements Iterable { for (Entry fieldEntry : fields.entrySet()) { writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue()); } - writeLine(outputStream, "EndMessage"); - outputStream.flush(); if (payloadInputStream != null) { + writeLine(outputStream, "Data"); FcpUtils.copy(payloadInputStream, outputStream); - outputStream.flush(); + } else { + writeLine(outputStream, "EndMessage"); } + outputStream.flush(); } // diff --git a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java new file mode 100644 index 0000000..bc0dbd1 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java @@ -0,0 +1,35 @@ +package net.pterodactylus.fcp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +/** + * Unit test for {@link FcpMessage}. + * + * @author David Roden + */ +public class FcpMessageTest { + + private final FcpMessage fcpMessage = new FcpMessage("TestMessage"); + + @Test + public void fcpMessageWithPayloadIsTerminatedByData() throws Exception { + fcpMessage.setPayloadInputStream(new ByteArrayInputStream("Test".getBytes())); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + fcpMessage.write(outputStream); + List lines = Arrays.asList(outputStream.toString().split("\r?\n")); + assertThat(lines, contains( + "TestMessage", + "Data", + "Test" + )); + } + +}