Proposed fix for issue #637

I wouldn’t have figured this one out without lots of help from @pspeed.

The issue is described here.

The affected files are
/trunk/engine/src/desktop/com/jme3/cursors/plugins/CursorLoader.java
and
/branches/gradle-restructure/jme3-desktop/src/main/java/com/jme3/cursors/plugins/CursorLoader.java

Here are the diffs:

--- Base (BASE)
+++ Locally Modified (Based On LOCAL)
@@ -41,6 +41,7 @@
 import java.awt.image.BufferedImage;
 import java.awt.image.DataBufferInt;
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.DataInput;
 import java.io.DataInputStream;
 import java.io.IOException;
@@ -210,13 +211,13 @@
             }
         } else if (isIco) {
             DataInputStream in = new DataInputStream(inStream);
-            int bytesToRead;
-            while ((bytesToRead = in.available()) != 0) {
-                byte[] icoimage2 = new byte[icoimages.length + bytesToRead];
-                System.arraycopy(icoimages, 0, icoimage2, 0, icoimages.length);
-                in.read(icoimage2, icoimages.length, bytesToRead);
-                icoimages = icoimage2;
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            byte[] buffer = new byte[16384];
+            int bytesRead;
+            while ((bytesRead = in.read(buffer)) >= 0) {
+                out.write(buffer, 0, bytesRead);
             }
+            icoimages = out.toByteArray();
         }
 
         BufferedImage bi[] = parseICOImage(icoimages);

I’ve tested the fix, and it seems to work. Do I have permission to commit this?

For future travelers, if ever you find yourself calling InputStream.available() then you are probably doing it wrong. It’s a very common mistake, though.

And yes, you have my permission to commit it. :slight_smile:

1 Like

Hey, if you are at the cursorloader, how about fixing the hotspot reading from cur files?

http://hub.jmonkeyengine.org/forum/topic/fix-for-cursorloader-hotspot-loading-for-cur-files/ (see at line ~233)

As my dif got forgotten for no apparent reason, and setting them manually is stupid if the format does provide them already.

2 Likes

fixed in trunk: https://code.google.com/p/jmonkeyengine/source/detail?r=11107

fixed in gradle-restructure: https://code.google.com/p/jmonkeyengine/source/detail?r=11106

@Empire Phoenix said: Hey, if you are at the cursorloader, how about fixing the hotspot reading from cur files?

http://hub.jmonkeyengine.org/forum/topic/fix-for-cursorloader-hotspot-loading-for-cur-files/ (see at line ~233)

As my dif got forgotten for no apparent reason, and setting them manually is stupid if the format does provide them already.

I’ll take another look at that tomorrow. Is there an issue filed at Google Code?

It does now
http://code.google.com/p/jmonkeyengine/issues/detail?id=638

1 Like
@pspeed said: For future travelers, if ever you find yourself calling InputStream.available() then you are probably doing it wrong. It's a very common mistake, though.

That hint pointed me to the following probable bugs in jME3:

BlenderInputStream.java line 77: size = inputStream.available();
GZIPSerializer line 67: while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
ZIPSerializer line 69: while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {

I’d appreciate it if someone else would look at these and let me know if they’re real issues.

@Empire Phoenix said: It does now http://code.google.com/p/jmonkeyengine/issues/detail?id=638

I’ve opened a new topic for this:
http://hub.jmonkeyengine.org/forum/topic/proposed-fix-for-issue-638/