tags:

views:

136

answers:

3

Hello!

Should I write

BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
ImageIO.write(im, "JPEG", os);

instead of

ImageIO.write(im, "JPEG", file);

I.e. are ImageIO file operations buffered by default or not?

Thanks!

A: 

I believe it depends on the specific implementation of the IIORegistry which I suppose is system dependent.

I would expect it to be buffered, but I suppose you could go with the first option to be completely sure.

aioobe
A: 

If you pass in a File, the underlying implementation will write directly to a RandomAccessFile (created in "rw" mode), so no buffering. Specifically, a FileImageOutputStream will be used as the ImageOutputStream.

kschneid
So, should I use BufferedOutputStream for optimization or id doesn't make sense?
Andrey
How do you know this? Is it documented? Do you have a reference in the api-source?
aioobe
@aioobe: I don't believe it's documented, but it is in the source.
kschneid
which source file?
aioobe
@Andrey: If you're not running into performance issues, the code for just using a file is probably simpler/cleaner. If you are running into performance issues, then it might be worth investigating. Just make sure you profile the app so that you're addressing real issues.
kschneid
@aioobe: `javax.imageio.stream.FileImageOutputStream`
kschneid
A: 

You will need to use BufferedOutputStream (As in ex. 1 mentioned in question).

ImageIo.write is not buffered by default. It depends upon what you pass to it in arguments. In case of File object being passed it won't be buffered write.

YoK