Просмотр исходного кода

BufferedInputStream enhancement android (#1627)

* Wrapped InputStream to BufferedInputStream

* Copyright update

* revert back to an input stream
Scrappers-glitch 3 лет назад
Родитель
Сommit
1ee04e13ac

+ 6 - 10
jme3-android/src/main/java/com/jme3/texture/plugins/AndroidBufferImageLoader.java

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009-2020 jMonkeyEngine
+ * Copyright (c) 2009-2021 jMonkeyEngine
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -42,6 +42,7 @@ import com.jme3.texture.image.ColorSpace;
 import com.jme3.util.BufferUtils;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.BufferedInputStream;
 import java.nio.ByteBuffer;
 
 /**
@@ -69,9 +70,8 @@ public class AndroidBufferImageLoader implements AssetLoader {
     
     @Override
     public Object load(AssetInfo assetInfo) throws IOException {
-        Bitmap bitmap = null;
+        Bitmap bitmap;
         Image.Format format;
-        InputStream in = null;
         int bpp;
         
         BitmapFactory.Options options = new BitmapFactory.Options();
@@ -84,16 +84,11 @@ public class AndroidBufferImageLoader implements AssetLoader {
         options.inPurgeable = true;
         options.inSampleSize = 1;
         
-        try {
-            in = assetInfo.openStream();
-            bitmap = BitmapFactory.decodeStream(in, null, options);
+        try (final BufferedInputStream bin = new BufferedInputStream(assetInfo.openStream())) {
+            bitmap = BitmapFactory.decodeStream(bin, null, options);
             if (bitmap == null) {
                 throw new IOException("Failed to load image: " + assetInfo.getKey().getName());
             }
-        } finally {
-            if (in != null) {
-                in.close();
-            }
         }
 
         switch (bitmap.getConfig()) {
@@ -160,6 +155,7 @@ public class AndroidBufferImageLoader implements AssetLoader {
         bitmap.recycle();
         
         Image image = new Image(format, width, height, data, ColorSpace.sRGB);
+        
         return image;
     }
 }

+ 32 - 7
jme3-android/src/main/java/com/jme3/texture/plugins/AndroidNativeImageLoader.java

@@ -1,3 +1,34 @@
+/*
+ * Copyright (c) 2009-2021 jMonkeyEngine
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
+ *   may be used to endorse or promote products derived from this software
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package com.jme3.texture.plugins;
 
 import com.jme3.asset.AssetInfo;
@@ -28,14 +59,8 @@ public class AndroidNativeImageLoader  implements AssetLoader {
     @Override
     public Image load(AssetInfo info) throws IOException {
         boolean flip = ((TextureKey) info.getKey()).isFlipY();
-        InputStream in = null;
-        try {
-            in = info.openStream();
+        try (final InputStream in = info.openStream()) {
             return load(in, flip, tmpArray);
-        } finally {
-            if (in != null){
-                in.close();
-            }
         }
     }
 }