Parcourir la source

* AssetManager.loadAsset() with null key will throw exception
* Make use of try/finally paradigm for all loaders

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8183 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

sha..rd il y a 14 ans
Parent
commit
045e2a4317

+ 11 - 4
engine/src/core-plugins/com/jme3/export/binary/BinaryImporter.java

@@ -125,13 +125,20 @@ public final class BinaryImporter implements JmeImporter {
 
         assetManager = info.getManager();
 
-        try{
-            InputStream is = info.openStream();
+        InputStream is = null;
+        try {
+            is = info.openStream();
             Savable s = load(is);
-            is.close();
+            
             return s;
-        }catch (IOException ex){
+        } catch (IOException ex) {
             logger.log(Level.SEVERE, "An error occured while loading jME binary object", ex);
+        } finally {
+            if (is != null){
+                try {
+                    is.close();
+                } catch (IOException ex) {}
+            }
         }
         return null;
     }

+ 19 - 8
engine/src/core-plugins/com/jme3/font/plugins/BitmapFontLoader.java

@@ -38,27 +38,27 @@ import com.jme3.material.MaterialDef;
 import com.jme3.asset.AssetInfo;
 import com.jme3.asset.AssetKey;
 import com.jme3.asset.AssetLoader;
+import com.jme3.asset.AssetManager;
 import com.jme3.asset.TextureKey;
 import com.jme3.material.RenderState.BlendMode;
 import com.jme3.math.ColorRGBA;
 import com.jme3.texture.Texture;
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 
 public class BitmapFontLoader implements AssetLoader {
 
-    public Object load(AssetInfo info) throws IOException {
+    private BitmapFont load(AssetManager assetManager, String folder, InputStream in) throws IOException{
         MaterialDef spriteMat = 
-                (MaterialDef) info.getManager().loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
+                (MaterialDef) assetManager.loadAsset(new AssetKey("Common/MatDefs/Misc/Unshaded.j3md"));
 
         BitmapCharacterSet charSet = new BitmapCharacterSet();
         Material[] matPages = null;
         BitmapFont font = new BitmapFont();
 
-        String folder = info.getKey().getFolder();
-
-        BufferedReader reader = new BufferedReader(new InputStreamReader(info.openStream()));
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
         String regex = "[\\s=]+";
 
         font.setCharSet(charSet);
@@ -105,7 +105,7 @@ public class BitmapFontLoader implements AssetLoader {
                         }
                         TextureKey key = new TextureKey(folder + file, true);
                         key.setGenerateMips(false);
-                        tex = info.getManager().loadTexture(key);
+                        tex = assetManager.loadTexture(key);
                         tex.setMagFilter(Texture.MagFilter.Bilinear);
                         tex.setMinFilter(Texture.MinFilter.BilinearNoMipMaps);
                     }
@@ -165,9 +165,20 @@ public class BitmapFontLoader implements AssetLoader {
                 ch.addKerning(second, amount);
             }
         }
-        reader.close();
-
         return font;
     }
+    
+    public Object load(AssetInfo info) throws IOException {
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            BitmapFont font = load(info.getManager(), info.getKey().getFolder(), in);
+            return font;
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
+    }
 
 }

+ 2 - 1
engine/src/core-plugins/com/jme3/material/plugins/J3MLoader.java

@@ -525,8 +525,9 @@ public class J3MLoader implements AssetLoader {
             key = info.getKey();
             loadFromRoot(BlockLanguageParser.parse(in));
         } finally {
-            if (in != null)
+            if (in != null){
                 in.close();
+            }
         }
         
         if (material != null){

+ 17 - 12
engine/src/core-plugins/com/jme3/scene/plugins/MTLLoader.java

@@ -254,16 +254,25 @@ public class MTLLoader implements AssetLoader {
     }
 
     @SuppressWarnings("empty-statement")
-    public Object load(AssetInfo info){
+    public Object load(AssetInfo info) throws IOException{
+        reset();
+        
         this.assetManager = info.getManager();
         folderName = info.getKey().getFolder();
-
-        InputStream in = info.openStream();
-        scan = new Scanner(in);
-        scan.useLocale(Locale.US);
-
         matList = new MaterialList();
-        while (readLine());
+
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            scan = new Scanner(in);
+            scan.useLocale(Locale.US);
+            
+            while (readLine());
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
         
         if (matName != null){
             // still have a material in the vars
@@ -273,12 +282,8 @@ public class MTLLoader implements AssetLoader {
         
         MaterialList list = matList;
 
-        reset();
+        
 
-        try{
-            in.close();
-        }catch (IOException ex){
-        }
         return list;
     }
 }

+ 19 - 16
engine/src/core-plugins/com/jme3/scene/plugins/OBJLoader.java

@@ -534,17 +534,12 @@ public final class OBJLoader implements AssetLoader {
 
     @SuppressWarnings("empty-statement")
     public Object load(AssetInfo info) throws IOException{
+        reset();
+        
         key = (ModelKey) info.getKey();
         assetManager = info.getManager();
-
-        if (!(info.getKey() instanceof ModelKey))
-            throw new IllegalArgumentException("Model assets must be loaded using a ModelKey");
-
-        InputStream in = info.openStream();
-        scan = new Scanner(in);
-        scan.useLocale(Locale.US);
-
         objName    = key.getName();
+        
         String folderName = key.getFolder();
         String ext        = key.getExtension();
         objName = objName.substring(0, objName.length() - ext.length() - 1);
@@ -554,8 +549,23 @@ public final class OBJLoader implements AssetLoader {
 
         objNode = new Node(objName + "-objnode");
 
-        while (readLine());
+        if (!(info.getKey() instanceof ModelKey))
+            throw new IllegalArgumentException("Model assets must be loaded using a ModelKey");
 
+        InputStream in = null; 
+        try {
+            in = info.openStream();
+            
+            scan = new Scanner(in);
+            scan.useLocale(Locale.US);
+
+            while (readLine());
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
+        
         if (matFaces.size() > 0){
             for (Entry<String, ArrayList<Face>> entry : matFaces.entrySet()){
                 ArrayList<Face> materialFaces = entry.getValue();
@@ -570,13 +580,6 @@ public final class OBJLoader implements AssetLoader {
             objNode.attachChild(geom);
         }
 
-        reset();
-
-        try{
-            in.close();
-        }catch (IOException ex){
-        }
-        
         if (objNode.getQuantity() == 1)
             // only 1 geometry, so no need to send node
             return objNode.getChild(0); 

+ 16 - 10
engine/src/core-plugins/com/jme3/texture/plugins/DDSLoader.java

@@ -122,17 +122,23 @@ public class DDSLoader implements AssetLoader {
             throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
         }
 
-        InputStream stream = info.openStream();
-        in = new LittleEndien(stream);
-        loadHeader();
-        if (texture3D) {
-            ((TextureKey) info.getKey()).setTextureTypeHint(Type.ThreeDimensional);
-        } else if (depth > 1) {
-            ((TextureKey) info.getKey()).setTextureTypeHint(Type.CubeMap);
+        InputStream stream = null;
+        try {
+            stream = info.openStream();
+            in = new LittleEndien(stream);
+            loadHeader();
+            if (texture3D) {
+                ((TextureKey) info.getKey()).setTextureTypeHint(Type.ThreeDimensional);
+            } else if (depth > 1) {
+                ((TextureKey) info.getKey()).setTextureTypeHint(Type.CubeMap);
+            }
+            ArrayList<ByteBuffer> data = readData(((TextureKey) info.getKey()).isFlipY());
+            return new Image(pixelFormat, width, height, depth, data, sizes);
+        } finally {
+            if (stream != null){
+                stream.close();
+            }
         }
-        ArrayList<ByteBuffer> data = readData(((TextureKey) info.getKey()).isFlipY());
-        stream.close();
-        return new Image(pixelFormat, width, height, depth, data, sizes);
     }
 
     public Image load(InputStream stream) throws IOException {

+ 18 - 11
engine/src/core-plugins/com/jme3/texture/plugins/HDRLoader.java

@@ -40,6 +40,7 @@ import com.jme3.texture.Image.Format;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.ByteBuffer;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 public class HDRLoader implements AssetLoader {
@@ -58,7 +59,7 @@ public class HDRLoader implements AssetLoader {
     public HDRLoader(){
     }
     
-    public static final void convertFloatToRGBE(byte[] rgbe, float red, float green, float blue){
+    public static void convertFloatToRGBE(byte[] rgbe, float red, float green, float blue){
         double max = red;
         if (green > max) max = green;
         if (blue > max) max = blue;
@@ -74,7 +75,7 @@ public class HDRLoader implements AssetLoader {
       }
     }
 
-    public static final void convertRGBEtoFloat(byte[] rgbe, float[] rgbf){
+    public static void convertRGBEtoFloat(byte[] rgbe, float[] rgbf){
         int R = rgbe[0] & 0xFF, 
             G = rgbe[1] & 0xFF,
             B = rgbe[2] & 0xFF, 
@@ -86,7 +87,7 @@ public class HDRLoader implements AssetLoader {
         rgbf[2] = B * e;
     }
 
-    public static final void convertRGBEtoFloat2(byte[] rgbe, float[] rgbf){
+    public static void convertRGBEtoFloat2(byte[] rgbe, float[] rgbf){
         int R = rgbe[0] & 0xFF,
             G = rgbe[1] & 0xFF,
             B = rgbe[2] & 0xFF,
@@ -98,7 +99,7 @@ public class HDRLoader implements AssetLoader {
         rgbf[2] = (B / 256.0f) * e;
     }
 
-    public static final void convertRGBEtoFloat3(byte[] rgbe, float[] rgbf){
+    public static void convertRGBEtoFloat3(byte[] rgbe, float[] rgbf){
         int R = rgbe[0] & 0xFF,
             G = rgbe[1] & 0xFF,
             B = rgbe[2] & 0xFF,
@@ -126,7 +127,7 @@ public class HDRLoader implements AssetLoader {
     }
     
     private String readString(InputStream is) throws IOException{
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         while (true){
             int i = is.read();
             if (i == 0x0a || i == -1) // new line or EOF
@@ -257,7 +258,7 @@ public class HDRLoader implements AssetLoader {
                 // regular command
                 int index = ln.indexOf("=");
                 if (index < 1){
-                    logger.fine("Ignored string: "+ln);
+                    logger.log(Level.FINE, "Ignored string: {0}", ln);
                     continue;
                 }
 
@@ -272,7 +273,7 @@ public class HDRLoader implements AssetLoader {
                 }else if (var.equals("gamma")){
                     gamma = Float.parseFloat(value);
                 }else{
-                    logger.warning("HDR Command ignored: "+ln);
+                    logger.log(Level.WARNING, "HDR Command ignored: {0}", ln);
                 }
             }
         }
@@ -314,10 +315,16 @@ public class HDRLoader implements AssetLoader {
             throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
 
         boolean flip = ((TextureKey) info.getKey()).isFlipY();
-        InputStream in = info.openStream();
-        Image img = load(in, flip);
-        in.close();
-        return img;
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            Image img = load(in, flip);
+            return img;
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
     }
 
 }

+ 19 - 9
engine/src/core-plugins/com/jme3/texture/plugins/PFMLoader.java

@@ -34,7 +34,6 @@ package com.jme3.texture.plugins;
 
 import com.jme3.asset.AssetInfo;
 import com.jme3.asset.AssetLoader;
-import com.jme3.asset.AssetManager;
 import com.jme3.asset.TextureKey;
 import com.jme3.texture.Image;
 import com.jme3.texture.Image.Format;
@@ -50,7 +49,7 @@ public class PFMLoader implements AssetLoader {
     private static final Logger logger = Logger.getLogger(PFMLoader.class.getName());
 
     private String readString(InputStream is) throws IOException{
-        StringBuffer sb = new StringBuffer();
+        StringBuilder sb = new StringBuilder();
         while (true){
             int i = is.read();
             if (i == 0x0a || i == -1) // new line or EOF
@@ -73,12 +72,8 @@ public class PFMLoader implements AssetLoader {
             scanline[i+1] = tmp;
         }
     }
-
-    public Object load(AssetInfo info) throws IOException {
-        if (!(info.getKey() instanceof TextureKey))
-            throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
-
-        InputStream in = info.openStream();
+    
+    private Image load(InputStream in, boolean needYFlip) throws IOException{
         Format format = null;
 
         String fmtStr = readString(in);
@@ -105,7 +100,6 @@ public class PFMLoader implements AssetLoader {
         float scale = Float.parseFloat(scaleStr);
         ByteOrder order = scale < 0 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
         boolean needEndienFlip = order != ByteOrder.nativeOrder();
-        boolean needYFlip = ((TextureKey)info.getKey()).isFlipY();
 
         // make sure all unneccessary stuff gets deleted from heap
         // before allocating large amount of memory
@@ -139,4 +133,20 @@ public class PFMLoader implements AssetLoader {
         return new Image(format, width, height, imageData);
     }
 
+    public Object load(AssetInfo info) throws IOException {
+        if (!(info.getKey() instanceof TextureKey))
+            throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
+
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            return load(in, ((TextureKey)info.getKey()).isFlipY());
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
+        
+    }
+
 }

+ 10 - 4
engine/src/core-plugins/com/jme3/texture/plugins/TGALoader.java

@@ -83,10 +83,16 @@ public final class TGALoader implements AssetLoader {
             throw new IllegalArgumentException("Texture assets must be loaded using a TextureKey");
 
         boolean flip = ((TextureKey)info.getKey()).isFlipY();
-        InputStream in = info.openStream();
-        Image img = load(in, flip);
-        in.close();
-        return img;
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            Image img = load(in, flip);
+            return img;
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
     }
 
     /**

+ 3 - 0
engine/src/desktop/com/jme3/asset/DesktopAssetManager.java

@@ -199,6 +199,9 @@ public class DesktopAssetManager implements AssetManager {
      * @return
      */
     public <T> T loadAsset(AssetKey<T> key){
+        if (key == null)
+            throw new IllegalArgumentException("key cannot be null");
+        
         if (eventListener != null)
             eventListener.assetRequested(key);
 

+ 24 - 11
engine/src/jogg/com/jme3/audio/plugins/OGGLoader.java

@@ -36,6 +36,7 @@ import com.jme3.asset.AssetInfo;
 import com.jme3.audio.AudioBuffer;
 import com.jme3.audio.AudioStream;
 import com.jme3.asset.AssetLoader;
+import com.jme3.audio.AudioData;
 import com.jme3.audio.AudioKey;
 import com.jme3.util.BufferUtils;
 import de.jarnbjo.ogg.EndOfOggStreamException;
@@ -196,17 +197,8 @@ public class OGGLoader implements AssetLoader {
     private InputStream readToStream(){
         return new JOggInputStream(vorbisStream);
     }
-
-    public Object load(AssetInfo info) throws IOException {
-        if (!(info.getKey() instanceof AudioKey)){
-            throw new IllegalArgumentException("Audio assets must be loaded using an AudioKey");
-        }
-        
-        AudioKey key = (AudioKey) info.getKey();
-        boolean readStream = key.isStream();
-        boolean streamCache = key.useStreamCache();
-        
-        InputStream in = info.openStream();
+    
+    private AudioData load(InputStream in, boolean readStream, boolean streamCache) throws IOException{
         if (readStream && streamCache){
             oggStream = new CachedOggStream(in);
         }else{
@@ -241,4 +233,25 @@ public class OGGLoader implements AssetLoader {
         }
     }
 
+    public Object load(AssetInfo info) throws IOException {
+        if (!(info.getKey() instanceof AudioKey)){
+            throw new IllegalArgumentException("Audio assets must be loaded using an AudioKey");
+        }
+        
+        AudioKey key = (AudioKey) info.getKey();
+        boolean readStream = key.isStream();
+        boolean streamCache = key.useStreamCache();
+        
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            return load(in, readStream, streamCache);
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
+        
+    }
+
 }

+ 20 - 12
engine/src/ogre/com/jme3/scene/plugins/ogre/MaterialLoader.java

@@ -33,6 +33,7 @@
 package com.jme3.scene.plugins.ogre;
 
 import com.jme3.asset.AssetInfo;
+import com.jme3.asset.AssetKey;
 import com.jme3.asset.AssetLoader;
 import com.jme3.asset.AssetManager;
 import com.jme3.asset.TextureKey;
@@ -56,7 +57,6 @@ import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Locale;
 import java.util.Scanner;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -425,21 +425,19 @@ public class MaterialLoader implements AssetLoader {
         separateTexCoord = false;
         return mat;
     }
-
-    public Object load(AssetInfo info) throws IOException {
-        folderName = info.getKey().getFolder();
-        assetManager = info.getManager();
-
-        MaterialList list = null;
+    
+    private MaterialList load(AssetManager assetManager, AssetKey key, InputStream in) throws IOException{
+        folderName = key.getFolder();
+        this.assetManager = assetManager;
         
-        InputStream in = info.openStream();
+        MaterialList list = null;
         List<Statement> statements = BlockLanguageParser.parse(in);
         
         for (Statement statement : statements){
             if (statement.getLine().startsWith("import")){
                 MaterialExtensionSet matExts = null;
-                if (info.getKey() instanceof OgreMaterialKey){
-                     matExts = ((OgreMaterialKey)info.getKey()).getMaterialExtensionSet();
+                if (key instanceof OgreMaterialKey){
+                     matExts = ((OgreMaterialKey)key).getMaterialExtensionSet();
                 }
 
                 if (matExts == null){
@@ -460,8 +458,18 @@ public class MaterialLoader implements AssetLoader {
                 list.put(matName, mat);
             }
         }
-        in.close();
-        return list;
+    }
+
+    public Object load(AssetInfo info) throws IOException {
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            return load(info.getManager(), info.getKey(), in);
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
     }
 
 }

+ 12 - 8
engine/src/ogre/com/jme3/scene/plugins/ogre/MeshLoader.java

@@ -34,7 +34,6 @@ package com.jme3.scene.plugins.ogre;
 import com.jme3.animation.Animation;
 import com.jme3.scene.plugins.ogre.matext.OgreMaterialKey;
 import com.jme3.animation.AnimControl;
-import com.jme3.animation.BoneAnimation;
 import com.jme3.animation.SkeletonControl;
 import com.jme3.asset.AssetInfo;
 import com.jme3.asset.AssetKey;
@@ -53,7 +52,6 @@ import com.jme3.scene.VertexBuffer;
 import com.jme3.scene.VertexBuffer.Format;
 import com.jme3.scene.VertexBuffer.Type;
 import com.jme3.scene.VertexBuffer.Usage;
-import com.jme3.system.JmeSystem;
 import com.jme3.util.BufferUtils;
 import com.jme3.util.IntMap;
 import com.jme3.util.IntMap.Entry;
@@ -78,7 +76,6 @@ import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.XMLReader;
 import org.xml.sax.helpers.DefaultHandler;
-import org.xml.sax.helpers.XMLReaderFactory;
 
 import static com.jme3.util.xml.SAXUtil.*;
 
@@ -813,14 +810,21 @@ public class MeshLoader extends DefaultHandler implements AssetLoader {
             // checking with JmeSystem.
             SAXParserFactory factory = SAXParserFactory.newInstance();
             factory.setNamespaceAware(true);
+            
             XMLReader xr = factory.newSAXParser().getXMLReader();
-
             xr.setContentHandler(this);
             xr.setErrorHandler(this);
-            InputStreamReader r = new InputStreamReader(info.openStream());
-            xr.parse(new InputSource(r));
-            r.close();
-
+            
+            InputStreamReader r = null;
+            try {
+                r = new InputStreamReader(info.openStream());
+                xr.parse(new InputSource(r));
+            } finally {
+                if (r != null){
+                    r.close();
+                }
+            }
+            
             return compileModel();
         } catch (SAXException ex) {
             IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");

+ 12 - 3
engine/src/ogre/com/jme3/scene/plugins/ogre/SceneLoader.java

@@ -380,9 +380,18 @@ public class SceneLoader extends DefaultHandler implements AssetLoader {
             
             xr.setContentHandler(this);
             xr.setErrorHandler(this);
-            InputStreamReader r = new InputStreamReader(info.openStream());
-            xr.parse(new InputSource(r));
-            r.close();
+            
+            InputStreamReader r = null;
+            
+            try {
+                r = new InputStreamReader(info.openStream());
+                xr.parse(new InputSource(r));
+            } finally {
+                if (r != null){
+                    r.close();
+                }
+            }
+            
             return root;
         }catch (SAXException ex){
             IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");

+ 9 - 4
engine/src/ogre/com/jme3/scene/plugins/ogre/SkeletonLoader.java

@@ -296,9 +296,14 @@ public class SkeletonLoader extends DefaultHandler implements AssetLoader {
 
     public Object load(AssetInfo info) throws IOException {
         assetManager = info.getManager();
-        InputStream in = info.openStream();
-        Object obj = load(in);
-        in.close();
-        return obj;
+        InputStream in = null;
+        try {
+            in = info.openStream();
+            return load(in);
+        } finally {
+            if (in != null){
+                in.close();
+            }
+        }
     }
 }