ImageIO problems encoding jpeg

Hi group

I have some code that saves an image to a file using ImageIO. And it
works for all formats except jpeg. A scaled down version follows:

public void write(String filename) throws IOException {
int idx = filename.lastIndexOf('.');
if (idx >= 0) {
File file = new File(filename);
String format = filename.substring(idx + 1).toLowerCase();
ImageIO.write(image, format, file);

The image has been instanciated using:
image = new BufferedImage(w > 0 ? w : 1, h > 0 ? h : 1,
....where 'w' is the width and 'h' is the height.

And given "img.png" I get a very nice PNG image. But "img.jpg" gives a
weird result.

The two images can be seen here (they are quite small):

Bigger versions can be seen here:

Firefox actually says the following when asked to display the JPEG:
The image “”; cannot be displayed, because
it contains errors.
....but The Gimp has no problem displaying it.

Has anybody seen this ?
Or do you know what I am doing wrong ?