From 50900c1bd4398ed5e13a243adc822c54a2424d07 Mon Sep 17 00:00:00 2001 From: Khashayar Etemadi Date: Fri, 3 Dec 2021 22:07:32 +0100 Subject: [PATCH] Fix violations of Sonar rule 2095 --- .../retrofit/spring/boot/test/DownloadTest.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java index 97a6b7f..4489f52 100644 --- a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java @@ -42,13 +42,14 @@ public void download() throws Exception { if (!file.exists()) { file.createNewFile(); } - FileOutputStream fos = new FileOutputStream(file); - byte[] b = new byte[1024]; - int length; - while ((length = is.read(b)) > 0) { - fos.write(b, 0, length); + try (FileOutputStream fos = new FileOutputStream(file)) { + byte[] b = new byte[1024]; + int length; + while ((length = is.read(b)) > 0) { + fos.write(b, 0, length); + } + is.close(); + fos.close(); } - is.close(); - fos.close(); } }