Skip to content

Commit 764509d

Browse files
garyrussellartembilan
authored andcommitted
GH-995: Close ObjectOutputStream in EHD2
Fixes #995 Doesn't really make a difference because `writeObject()` drains all data to the underlying output stream and `flush()` and `close()` are no-ops for `ByteArrayOutputStream`. But, technically, correct. **cherry-pick to 2.2.x**
1 parent f5b6927 commit 764509d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/serializer/ErrorHandlingDeserializer2.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,17 @@ public void close() {
199199
private void deserializationException(Headers headers, byte[] data, Exception e) {
200200
ByteArrayOutputStream stream = new ByteArrayOutputStream();
201201
DeserializationException exception = new DeserializationException("failed to deserialize", data, this.isKey, e);
202-
try {
203-
new ObjectOutputStream(stream).writeObject(exception);
202+
try (ObjectOutputStream oos = new ObjectOutputStream(stream)) {
203+
oos.writeObject(exception);
204204
}
205205
catch (IOException ex) {
206-
try {
206+
stream = new ByteArrayOutputStream();
207+
try (ObjectOutputStream oos = new ObjectOutputStream(stream)) {
207208
exception = new DeserializationException("failed to deserialize",
208209
data, this.isKey, new RuntimeException("Could not deserialize type "
209210
+ e.getClass().getName() + " with message " + e.getMessage()
210211
+ " failure: " + ex.getMessage()));
211-
new ObjectOutputStream(stream).writeObject(exception);
212+
oos.writeObject(exception);
212213
}
213214
catch (IOException ex2) {
214215
throw new IllegalStateException("Could not serialize a DeserializationException", ex2); // NOSONAR

0 commit comments

Comments
 (0)