Reading an image from a URL

import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import java.net.*;
 
public class Toolbox
{
        private static Hashtable urlImagesCache = new Hashtable();
 
        public static Image getImage(URL url)
        {
                try
                {
                        Image cached = (Image)urlImagesCache.get(url);
                        if(cached != null) return cached;
 
                        InputStream in = url.openConnection().getInputStream();
                        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                        byte buffer[] = new byte[1024*4];
                        int last_read_bytes = 0;
                        while((last_read_bytes = in.read(buffer)) != -1)
                        {
                                bytes.write(buffer,0,last_read_bytes);
                        }
 
                        Image image =  java.awt.Toolkit.getDefaultToolkit().createImage(bytes.toByteArray());
                        m.addImage(image,0);
                        try {
                            m.waitForAll();
                        }
                        catch (InterruptedException ex) {ex.printStackTrace();}
                        m.removeImage(image);
 
                        urlImagesCache.put(url,image);
 
                        return image;
                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                        return null;
                }
        }
}