error creating image

Im getting error when trying to create an image from array

when i am reading the image as array from the network

publicvoid write(OutputStream out)throws IOException{

DataOutputStream dout =new DataOutputStream(out);

//Write mood name

dout.writeUTF(m_moodName);

if(m_img ==null){

//Write there is no image

dout.writeBoolean(false);

}else{

//Write there is image

dout.writeBoolean(true);

PngEncoder pngEncoder =new PngEncoder(m_img);

byte[] array = pngEncoder.pngEncode();

//Write the array length

dout.writeInt(array.length);

//Write the array

dout.write(array);

dout.flush();

}

}

publicvoid read(InputStream in)throws IOException{

DataInputStream din =new DataInputStream(in);

//Read mood name

m_moodName = din.readUTF();

//Read if there is image

boolean bool = din.readBoolean();

if(bool ==true){

//Read byte array length

int length = din.readInt();

byte[] array =newbyte[length];

//Read to byte array

din.readFully(array);

m_img = Toolkit.getDefaultToolkit().createImage(array);

}

}

This is the exception that i am getting

Uncaught error fetching image:

java.lang.NegativeArraySizeException

at sun.awt.image.PNGImageDecoder.produceImage(Unknown Source)

at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)

at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)

at sun.awt.image.ImageFetcher.run(Unknown Source)

I am getting this error from this line... (i think)

m_img = Toolkit.getDefaultToolkit().createImage(array);

and here is PNG encoder that i am using

http://catcode.com/pngencoder/

please help...

shay

[3100 byte] By [shay_tea] at [2007-10-3 3:17:24]
# 1
Someone can help?
shay_tea at 2007-7-14 21:08:59 > top of Java-index,Security,Cryptography...
# 2

There are many possible reasons, for example:

- pngEncoder produces wrong result.

- byte array is corrupted on transition.

I'd suggest to save this byte array as file and check if this image can be open by any image viewer.

Also, it seems Image I/O is applicable in your scenario.

Consider using it because it is recomeded API to work with images nowadays

(unless you have something ImageIO can not handle, e.g. animated images)

Andrew.Brygina at 2007-7-14 21:08:59 > top of Java-index,Security,Cryptography...
# 3
I also have this problem when loading image from file...the wierd thing is.. that sometimes it's working and sometimes it dontIMAGE IO from the nio package?
shay_tea at 2007-7-14 21:08:59 > top of Java-index,Security,Cryptography...
# 4
No, from javax.imageio package. Take a look to javax.imageio.ImageIO class.It provides utility methods to read/write images.
Andrew.Brygina at 2007-7-14 21:08:59 > top of Java-index,Security,Cryptography...
# 5
Thanks ...it's working greate now
shay_tea at 2007-7-14 21:08:59 > top of Java-index,Security,Cryptography...