From 258d447736456d393d50f1b40e7c7744f13b077a Mon Sep 17 00:00:00 2001 From: kappa1 Date: Mon, 7 Mar 2011 22:22:08 +0000 Subject: [PATCH] AppletLoader: Add better checks/protection to reading cache/version files in case of corruption. Close resources properly when writing cache/version files. Add preliminary list of contributors. Thx to Arielsan for finding issue and providing patch. --- .../org/lwjgl/util/applet/AppletLoader.java | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/src/java/org/lwjgl/util/applet/AppletLoader.java b/src/java/org/lwjgl/util/applet/AppletLoader.java index 388ad1d4..662e7ee6 100644 --- a/src/java/org/lwjgl/util/applet/AppletLoader.java +++ b/src/java/org/lwjgl/util/applet/AppletLoader.java @@ -129,10 +129,23 @@ import sun.security.util.SecurityConstants; *
  • lwjgl_arguments -
  • [String] used to pass the hidden LWJGL parameters to LWJGL e.g. ("-Dorg.lwjgl.input.Mouse.allowNegativeMouseCoords=true -Dorg.lwjgl.util.Debug=true"). * *

    - * @author kappaOne + * @author kappaOne * @author Brian Matzon * @version $Revision$ * $Id$ + * + * Contributors: + *
      + *
    • Arielsan
    • + *
    • Bobjob
    • + *
    • Dashiva
    • + *
    • Kevglass
    • + *
    • MatthiasM
    • + *
    • Mickelukas
    • + *
    • NateS
    • + *
    • Ruben01
    • + *
    + * */ public class AppletLoader extends Applet implements Runnable, AppletStub { @@ -844,10 +857,22 @@ public class AppletLoader extends Applet implements Runnable, AppletStub { * @throws Exception if it fails to read value */ protected float readVersionFile(File file) throws Exception { - DataInputStream dis = new DataInputStream(new FileInputStream(file)); - float version = dis.readFloat(); - dis.close(); - return version; + FileInputStream fis = new FileInputStream(file); + try { + DataInputStream dis = new DataInputStream(fis); + float version = dis.readFloat(); + dis.close(); + return version; + } catch (Exception e) { + // failed to read version file + e.printStackTrace(); + } + finally { + fis.close(); + } + + // return 0 if failed to read file + return 0; } /** @@ -858,9 +883,11 @@ public class AppletLoader extends Applet implements Runnable, AppletStub { * @throws Exception if it fails to write file */ protected void writeVersionFile(File file, float version) throws Exception { - DataOutputStream dos = new DataOutputStream(new FileOutputStream(file)); + FileOutputStream fos = new FileOutputStream(file); + DataOutputStream dos = new DataOutputStream(fos); dos.writeFloat(version); dos.close(); + fos.close(); } /** @@ -872,10 +899,21 @@ public class AppletLoader extends Applet implements Runnable, AppletStub { */ @SuppressWarnings("unchecked") protected HashMap readCacheFile(File file) throws Exception { - ObjectInputStream dis = new ObjectInputStream(new FileInputStream(file)); - HashMap hashMap = (HashMap)dis.readObject(); - dis.close(); - return hashMap; + FileInputStream fis = new FileInputStream(file); + try { + ObjectInputStream dis = new ObjectInputStream(fis); + HashMap hashMap = (HashMap) dis.readObject(); + dis.close(); + return hashMap; + } catch (Exception e) { + // failed to read cache file + e.printStackTrace(); + } finally { + fis.close(); + } + + // return an empty map if failed to read file + return new HashMap(); } /** @@ -886,9 +924,11 @@ public class AppletLoader extends Applet implements Runnable, AppletStub { * @throws Exception if it fails to write file */ protected void writeCacheFile(File file, HashMap filesLastModified) throws Exception { - ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream(file)); + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream dos = new ObjectOutputStream(fos); dos.writeObject(filesLastModified); dos.close(); + fos.close(); } /**