[Kaffe] problem and patch for MediaTracker

Moses DeJong dejong at cs.umn.edu
Thu Feb 18 15:41:41 PST 1999


Hi all.

I have run into a number of problems with the java.awt package. The Image
and MediaTracker classes do not seem to work. I have been looking around
in the Kaffe source and I have implemented a couple of methods that were
not implemented but I have yet to find the problem. I am going to include
a small example of Java code that breaks under Kaffe along with a patch
I have created. Note that my patch fixes some problems but it does not
fix the "real" problem of the image load failing. Has anyone run into
this kind of image loading problem before?

Mo DeJong
dejong at cs.umn.edu



// File ImageLoad.java

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ImageLoad {

    public static void main(String[] argv) throws Exception {
	String image = "/Tile.gif";
	InputStream is = Class.class.getResourceAsStream(image);
	if (is == null) {
	    System.err.println("Failed to find boot image " + image);
	    System.exit(-1);
	}

	System.out.println("Image available size is " + is.available());
	byte[] imageBytes = new byte[is.available()];
	is.read(imageBytes);

	Image im = Toolkit.getDefaultToolkit().createImage(imageBytes);

	int width;
	int height;
	
	Frame f = new Frame();

	MediaTracker tracker = new MediaTracker(f);
	try {
	    tracker.addImage(im, 0);
	    tracker.waitForID(0);
	}
	catch(InterruptedException e) {
	    e.printStackTrace();
	}

	// Check to see if the image loading failed
	if ( tracker.isErrorAny() ) {
	    System.err.println("Error in MediaTracker");

	    Object[] errors = tracker.getErrorsAny();

	    if (errors == null || errors.length != 1) {
		System.err.println("getErrorsAny() did not return array of length 1");
	    }

	    errors = tracker.getErrorsID(0);
	    if (errors == null || errors.length != 1) {
		System.err.println("getErrorsId(0) did not return array of length 1");
	    }

	} else {
	    System.err.println("No Errors in MediaTracker");
	}

	int status = tracker.statusAll(false);
	
	if ((status & MediaTracker.LOADING) != 0) {
	    System.err.println("LOADING");
	}
	
	if ((status & MediaTracker.ABORTED) != 0) {
	    System.err.println("ABORTED");
	}
	
	if ((status & MediaTracker.ERRORED) != 0) {
	    System.err.println("ERRORED");
	}
	
	if ((status & MediaTracker.COMPLETE) != 0) {
	    System.err.println("COMPLETE");
	}

	
	width = im.getWidth(f);
	height = im.getHeight(f);

	System.out.println("image size is [" +
			   width +
			   "," +
			   height +
			   "]");

	System.exit(0);
    }
}



JDK

Image available size is 25435
No Errors in MediaTracker
COMPLETE
image size is [408,104]



Kaffe from the CVS

Image available size is 25435
No Errors in MediaTracker
ABORTED
ERRORED
image size is [-1,-1]



Kaffe with my patch

Image available size is 25435
Error in MediaTracker
ABORTED
ERRORED
image size is [-1,-1]

(note that the you can not see a diff here because
in the code errors are only printed if tracker.isErrorAny()
fails).






Index: MediaTracker.java
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/javalib/java/awt/MediaTracker.java,v
retrieving revision 1.6
diff -u -r1.6 MediaTracker.java
--- MediaTracker.java	1999/02/10 21:34:33	1.6
+++ MediaTracker.java	1999/02/19 00:43:34
@@ -68,14 +68,79 @@
 
 public synchronized Object[] getErrorsAny()
 {
-	// We assume we don't get image errors here
-	return (null);
+	int used = 0;
+	int size = 1;
+	int ic;	
+	Object[] array;
+	final boolean load = false;
+
+	array = new Object[size];
+
+	for ( MediaTrackerEntry e = images; e != null; e = e.next ) {
+		ic = e.img.checkImage( e.w, e.h, e, load);
+			
+		if ( (ic & ImageObserver.ERROR) != 0 ) {
+		        // Expand the byte array if we need to make more room
+		        if (used >= size) {
+			    Object[] oldArray = array;
+			    int new_size = size * 2;
+			    
+			    array = new Object[new_size];
+			    System.arraycopy(oldArray, 0, array, 0, used);
+			    oldArray = null;
+			    
+			    size = new_size;
+			}
+		        // Add this object to our object array
+			array[used++] = e.img;
+		}
+	}
+
+	if (used == 0)
+	    return null;
+	
+	Object[] ret = new Object[used];
+	System.arraycopy(ret, 0, array, 0, used);
+	return ret;
 }
 
 public synchronized Object[] getErrorsID(int id)
 {
-	// We assume we don't get image errors here
-	return (null);
+    	int used = 0;
+	int size = 1;
+	int ic;	
+	Object[] array;
+	final boolean load = false;
+
+	array = new Object[size];
+	
+	for ( MediaTrackerEntry e = getNextEntry( id, null); e != null; e = getNextEntry( id, e) ) {
+		ic = e.img.checkImage( e.w, e.h, e, load);
+			
+		if ( (ic & ImageObserver.ERROR) != 0 ) {
+		        // Expand the byte array if we need to make more room
+		        if (used >= size) {
+			    Object[] oldArray = array;
+			    int new_size = size * 2;
+			    
+			    array = new Object[new_size];
+			    System.arraycopy(oldArray, 0, array, 0, used);
+			    oldArray = null;
+			    
+			    size = new_size;
+			}
+
+			// Add this object to our object array
+			array[used++] = e.img;
+		}
+	}
+
+	if (used == 0)
+	    return null;
+	
+	Object[] ret = new Object[used];
+	System.arraycopy(ret, 0, array, 0, used);
+	return ret;
 }
 
 MediaTrackerEntry getNextEntry ( int id, MediaTrackerEntry prev ) {
@@ -91,14 +156,12 @@
 
 public synchronized boolean isErrorAny()
 {
-	// We assume we don't get image errors here
-	return (false);
+	return ((statusAll(false) & ERRORED) != 0);
 }
 
 public synchronized boolean isErrorID(int id)
 {
-	// We assume we don't get image errors here
-	return (false);
+	return ((statusID(id,false) & ERRORED) != 0);	
 }
 
 public synchronized void removeImage(Image image)



More information about the kaffe mailing list