INT-3290 Fix TCP Deserializer for 0 Length Message
ByteArraySingleTerminatorSerializer does not support multiple subsequent terminators (zero length messages). JIRA: https://jira.springsource.org/browse/INT-3290 * Add unit test to verify bug. * Fix bug. Polishing: * Remove test for `n >= 0`; n cannot be < 0 here * Move test case to `DeserializationTests` Conflicts: spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/serializer/DeserializationTests.java Resolved.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -51,12 +51,11 @@ public class ByteArraySingleTerminatorSerializer extends AbstractByteArraySerial
|
||||
}
|
||||
while (true) {
|
||||
bite = inputStream.read();
|
||||
// logger.debug("Read:" + (char) bite);
|
||||
if (bite < 0 && n == 0) {
|
||||
throw new SoftEndOfStreamException("Stream closed between payloads");
|
||||
}
|
||||
checkClosure(bite);
|
||||
if (n > 0 && bite == terminator) {
|
||||
if (bite == terminator) {
|
||||
break;
|
||||
}
|
||||
buffer[n++] = (byte) bite;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.integration.ip.tcp.serializer;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
@@ -26,12 +27,14 @@ import java.net.Socket;
|
||||
import javax.net.ServerSocketFactory;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.serializer.DefaultDeserializer;
|
||||
import org.springframework.integration.ip.util.SocketTestUtils;
|
||||
import org.springframework.integration.test.util.SocketUtils;
|
||||
|
||||
/**
|
||||
* @author Gary Russell
|
||||
* @author Gavin Gray
|
||||
* @since 2.0
|
||||
*/
|
||||
public class DeserializationTests {
|
||||
@@ -228,4 +231,22 @@ public class DeserializationTests {
|
||||
server.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canDeserializeMultipleSubsequentTerminators() throws IOException {
|
||||
byte terminator = (byte) '\n';
|
||||
ByteArraySingleTerminatorSerializer serializer = new ByteArraySingleTerminatorSerializer(terminator);
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream("s\n\n".getBytes());
|
||||
|
||||
try {
|
||||
byte[] bytes = serializer.deserialize(inputStream);
|
||||
assertEquals(1, bytes.length);
|
||||
assertEquals("s".getBytes()[0], bytes[0]);
|
||||
bytes = serializer.deserialize(inputStream);
|
||||
assertEquals(0, bytes.length);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user