• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Loading GIF from a byte[] ?

How can my Applet convert a byte array in "gif89" format
to an Image object?
as you know, there is no "getImage(byte b[])".. only
"getImage(URL u)" or "getImage(File f)"
Since it's an Applet, writing it as a file and loading it with getImage(file) won't work, and neither will trying to set up a "server" on the local machine that will return it to me as a url... (Applet security... arghhh....)

I know there are some classes to do that in sun.awt.image,
but those won't usually be available for me in normal java enabled browsers!
any known workaround for this problem??
even one that only works on MSIE and Netscape....

(I saw that many people are fighting this question in several newsgroups, it's a good example of BAD awt design!)
Ofer Tal
1 Solution
I think you are right in that you cannot convert what you have into a form where GetImage can deal with it.  Again, JAVA is saving us from ourselves and our impure motives ;-)

How did you get the GIF image into the byte[] to start with?  I assume that you have built it in your applet.  If so, rather than build a GIF in memory (or along with doing that if you must) just draw the image in the applet.
Actually, you can do this. It's not obvious, but the AWT does support it; however, jhance has already given you an answer. If you like it - fine; otherwise, reject it and let someone else try.
Actualy there a several ways it can be done. Though as russgold states, none of them are obvious.
ofer052997Author Commented:
first of all... loading an image as a byte[] is not a problem..
just get the InputStream of the URL/URLConnection, and start read()'ing!
of course, if I would have built the image myself in memory, I would simply use the MemoryImageSource to display it.

in JDK1.1 there is a simple solution - the new Toolkit has a method createImage(byte b[]) that decodes the Image from gif/jpg format directly from the byteStream (BTW - the "impure" motivation for my question was an attempt to "stream" Images through a socket OR read many small gifs in one connection...).
Just to be clear - my question is weather the same thing can be done in a way that all (major) Java enabled browsers can display the Image. (Even those who don't support JDK1.1.x) - Netscape4/MSIE4 do it perfectly with the new method call!.

Ofer Tal
Here is how I would do it in JDK 1.02:

Implement the ImageProducer interface with a class which knows how to convert GIF format into pixels stored in another byte array (this means that you have to write your own GIF interpreter).  Then, simply deliver those pixels as a byte array to the connected ImageConsumer when startProduction is called.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now