From 9e9fa2cf5812733ae32cc2d6db33504227222b8c Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 5 Oct 2018 12:02:21 -0400 Subject: [PATCH] RemoteFTempl: InputStream.close() in the finally If exception happens in the `callback.doWithInputStream(inputStream)`, we don't close the `inputStream = session.readRaw(remotePath)`. * Move the `InputStream.close()` to the `finally` block of the `SessionCallback` action in the `RemoteFileTemplate.get()` **Cherry-pick to 5.0.x and 4.3.x** --- .../file/remote/RemoteFileTemplate.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileTemplate.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileTemplate.java index 2d36ee788f..3267e5cb1c 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileTemplate.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/RemoteFileTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2017 the original author or authors. + * Copyright 2013-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. @@ -391,11 +391,18 @@ public class RemoteFileTemplate implements RemoteFileOperations, Initializ @Override public boolean get(final String remotePath, final InputStreamCallback callback) { Assert.notNull(remotePath, "'remotePath' cannot be null"); - return this.execute(session -> { - InputStream inputStream = session.readRaw(remotePath); - callback.doWithInputStream(inputStream); - inputStream.close(); - return session.finalizeRaw(); + return execute(session -> { + InputStream inputStream = null; + try { + inputStream = session.readRaw(remotePath); + callback.doWithInputStream(inputStream); + return session.finalizeRaw(); + } + finally { + if (inputStream != null) { + inputStream.close(); + } + } }); }