From 75712fb3f9e0b2605ddb50a156b0ff5a23bacef2 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Mon, 15 Apr 2019 23:28:35 +0200 Subject: [PATCH] fix forceResetableStream --- src/be/nikiroo/utils/IOUtils.java | 36 ++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/be/nikiroo/utils/IOUtils.java b/src/be/nikiroo/utils/IOUtils.java index e9a378c..c7cbe25 100644 --- a/src/be/nikiroo/utils/IOUtils.java +++ b/src/be/nikiroo/utils/IOUtils.java @@ -396,9 +396,6 @@ public class IOUtils { */ public static InputStream forceResetableStream(InputStream in) throws IOException { - MarkableFileInputStream tmpIn = null; - File tmp = null; - boolean resetable = in.markSupported(); if (resetable) { try { @@ -412,19 +409,32 @@ public class IOUtils { return in; } - tmp = File.createTempFile(".tmp-stream", ".tmp"); + final File tmp = File.createTempFile(".tmp-stream.", ".tmp"); try { write(in, tmp); - tmpIn = new MarkableFileInputStream(new FileInputStream(tmp)); - return tmpIn; - } finally { - try { - if (tmpIn != null) { - tmpIn.close(); + in.close(); + + final FileInputStream fis = new FileInputStream(tmp); + return new MarkableFileInputStream(fis) { + @Override + public void close() throws IOException { + try { + try { + super.close(); + } finally { + try { + fis.close(); + } catch (IOException e) { + } + } + } finally { + tmp.delete(); + } } - } finally { - tmp.delete(); - } + }; + } catch (IOException e) { + tmp.delete(); + throw e; } } -- 2.27.0