return byte array

Hello i have a function that return a byte array. I did it this way but i m wondering if it is the good way, so if someone can help improve it. Thank you.

try

{

InputStream is =new BufferedInputStream(new FileInputStream(nomFichierDemande));

byte[] readData =null;

while (is.available() > 0)

{

readData =newbyte[is.available()];

is.read(readData);

}

return readData;

}

catch (IOException e)

{

System.out.println("probleme lors de lecture de l'image");

e.printStackTrace();

}

the source for my stream is an image file, bmp, gif or png, will it work the same for all kinds of file ?

Thanks for help.

Message was edited by:

JusteUneQuestion

Message was edited by:

JusteUneQuestion

[1338 byte] By [JusteUneQuestiona] at [2007-11-27 7:41:43]
# 1
(a) You should use File.length() rather than InputStream.available(): that's not what it's for.(b) You should reconsider the whole idea of loading the entire file into memory. It doesn't scale. Sooner or later you'll encounter a file that doesn't fit into memory - then what?
ejpa at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 2

> (a) You should use File.length() rather than

> InputStream.available(): that's not what it's for.

Hello i change the code like this :

InputStream is = new BufferedInputStream(new FileInputStream(imageFile));

byte[] readData = null;

readData = new byte[(int)imageFile.length()];

is.read(readData);

return readData;

Is it like this that u mean i have to use File.length, or was it only for the while loop ? I dont think that i need this while loop finally.

> (b) You should reconsider the whole idea of loading

> the entire file into memory. It doesn't scale. Sooner

> or later you'll encounter a file that doesn't fit

> into memory - then what?

Can you give me a better idea? i thought about this but dont really know how to do to improve it. There is a limitation in the size of image anyway but i d like to have a good code in case for later.

With this code i could read gif file but i m having problem for bmp so maybe its because it doesnt scale like you said.

Message was edited by:

JusteUneQuestion

JusteUneQuestiona at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 3
When i debug it i have the buf property of BufferedInputStream has property bug = byte[2048] but its an array of 0. I guess it shouldnt be 0 so what could be the problem ? Thank you.
JusteUneQuestiona at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 4
You don't need to debug BufferedInputStream, it is OK.I think your method should return the InputStream itself actually.
ejpa at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 5

> You don't need to debug BufferedInputStream, it is

> OK.

>

> I think your method should return the InputStream

> itself actually.

Can i cast an InputStream to a byte array cause the value returned by the method is cast to a byte array ?

this is the call to the method :

byte[] result = (byte[])gen.generateImageExt(url, height, width, type, option, getPageContext().getRequest(), parametres);

JusteUneQuestiona at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 6

Of course not. But your compiler could have told you that, you don't need to ask questions on a forum and get an answer from the other side of the world. The point is, again, that the whole concept of reading the file into a byte[] array is wrong. So the method should return the input stream, and the caller should be amended to use an InputStream too.

Obviously.

ejpa at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 7
So how can i display a bmp file to my frame ? I m using ImageIcon(byte[]) that is why i want to pass a byte array. Its working perfectly for gif and jpeg if i m just using ImageIcon(String filename) but not for *.bmp.
JusteUneQuestiona at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 8

> So how can i display a bmp file to my frame ? I m

> using ImageIcon(byte[]) that is why i want to pass a

> byte array. Its working perfectly for gif and jpeg

> if i m just using ImageIcon(String filename) but not

> for *.bmp.

This indicates that the problem has nothing to do with reading the file and everything to do with the fact that the JRE cannot decode bmp files. One approach you can try is http://www.cs.queensu.ca/~blostein/java.html but Google is your friend.

sabre150a at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...
# 9

> This indicates that the problem has nothing to do

> with reading the file

I thought that using a stream could help. If i knew the answer i wouldnt post here.

and everything to do with the

> fact that the JRE cannot decode bmp files. One

> approach you can try is

> http://www.cs.queensu.ca/~blostein/java.html but

> Google is your friend.

Thanks for this indication, now i can start search for something useful with my friend.

JusteUneQuestiona at 2007-7-12 19:22:23 > top of Java-index,Core,Core APIs...