Skip to content

Commit 438693b

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** (cherry picked from commit 764509d)
1 parent 6cabfe5 commit 438693b

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
@@ -202,16 +202,17 @@ public void close() {
202202
private void deserializationException(Headers headers, byte[] data, Exception e) {
203203
ByteArrayOutputStream stream = new ByteArrayOutputStream();
204204
DeserializationException exception = new DeserializationException("failed to deserialize", data, this.isKey, e);
205-
try {
206-
new ObjectOutputStream(stream).writeObject(exception);
205+
try (ObjectOutputStream oos = new ObjectOutputStream(stream)) {
206+
oos.writeObject(exception);
207207
}
208208
catch (IOException ex) {
209-
try {
209+
stream = new ByteArrayOutputStream();
210+
try (ObjectOutputStream oos = new ObjectOutputStream(stream)) {
210211
exception = new DeserializationException("failed to deserialize",
211212
data, this.isKey, new RuntimeException("Could not deserialize type "
212213
+ e.getClass().getName() + " with message " + e.getMessage()
213214
+ " failure: " + ex.getMessage()));
214-
new ObjectOutputStream(stream).writeObject(exception);
215+
oos.writeObject(exception);
215216
}
216217
catch (IOException ex2) {
217218
throw new IllegalStateException("Could not serialize a DeserializationException", ex2); // NOSONAR

0 commit comments

Comments
 (0)