From: Niki Roo Date: Mon, 15 Apr 2019 21:28:35 +0000 (+0200) Subject: fix forceResetableStream X-Git-Tag: fanfix-3.0.1~29^2~150 X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=75712fb3f9e0b2605ddb50a156b0ff5a23bacef2;p=fanfix.git fix forceResetableStream --- 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; } }