INT-4374: TCP Fix elastic raw deserializer

JIRA: https://jira.spring.io/browse/INT-4374

Detect end of stream to prevent emitting an empty byte[] payload.
This commit is contained in:
Gary Russell
2018-01-04 09:09:01 -05:00
committed by Artem Bilan
parent f21f81092d
commit 5ad304a2a2
2 changed files with 12 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -56,7 +56,9 @@ public class ByteArrayElasticRawDeserializer implements Deserializer<byte[]> {
@Override
public byte[] deserialize(InputStream inputStream) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(this.initialBufferSize);
StreamUtils.copy(inputStream, out);
if (StreamUtils.copy(inputStream, out) == 0) {
throw new SoftEndOfStreamException("Stream closed with no data");
}
out.close();
return out.toByteArray();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -153,6 +153,13 @@ public class DeserializationTests {
byte[] out = serializer.deserialize(socket.getInputStream());
assertEquals("Data", SocketTestUtils.TEST_STRING + SocketTestUtils.TEST_STRING,
new String(out));
try {
serializer.deserialize(socket.getInputStream());
fail("Expected end of Stream");
}
catch (SoftEndOfStreamException e) {
// NOSONAR
}
server.close();
}