From 124aa72fd952955dc9a0fd0fd50179302a652079 Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 4 Jul 2012 10:00:11 -0400 Subject: [PATCH] Better file handling --- .../com/gmail/nossr50/spout/SpoutStuff.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/spout/SpoutStuff.java b/src/main/java/com/gmail/nossr50/spout/SpoutStuff.java index 61df98260..a59cfdd5a 100644 --- a/src/main/java/com/gmail/nossr50/spout/SpoutStuff.java +++ b/src/main/java/com/gmail/nossr50/spout/SpoutStuff.java @@ -46,26 +46,24 @@ public class SpoutStuff { * @param theFilePath The name of the file path */ private static void writeFile(String theFileName, String theFilePath) { + OutputStream os = null; + JarFile jar = null; + try { File currentFile = new File(theFilePath + theFileName); - JarFile jar = new JarFile(mcMMO.mcmmo); + jar = new JarFile(mcMMO.mcmmo); JarEntry entry = jar.getJarEntry("resources/" + theFileName); InputStream is = jar.getInputStream(entry); - byte[] buf = new byte[2048]; int nbRead; - OutputStream os = new BufferedOutputStream(new FileOutputStream(currentFile)); + os = new BufferedOutputStream(new FileOutputStream(currentFile)); while ((nbRead = is.read(buf)) != -1) { os.write(buf, 0, nbRead); } - - os.flush(); - os.close(); - jar.close(); } catch (FileNotFoundException e) { e.printStackTrace(); @@ -73,6 +71,26 @@ public class SpoutStuff { catch (IOException e) { e.printStackTrace(); } + finally { + if (os != null) { + try { + os.flush(); + os.close(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + + if (jar != null) { + try { + jar.close(); + } + catch (IOException ex) { + ex.printStackTrace(); + } + } + } } /**