Wrong reload number when using Embedded

Hi,

I find some interested results when “copy” the code in page 54 of book-of-vaadin.pdf:


package com.example.dynaimage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.vaadin.Application;
import com.vaadin.terminal.StreamResource;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;

@SuppressWarnings("serial")
public class DynaimageApplication extends Application {
	
	private Embedded embedded; 
	
	@Override
	public void init() {
		Window mainWindow = new Window("Dynaimage Application");
		Label label = new Label("Hello Vaadin user");
		mainWindow.addComponent(label);
		
		embedded = new Embedded("Image title", createImageResource());
		mainWindow.addComponent(embedded);		
		
		Button button = new Button("Click me", new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				embedded.setSource(createImageResource());				
			}
		});
		mainWindow.addComponent(button);
		setMainWindow(mainWindow);
	}
	
	private StreamResource createImageResource() {		
		StreamResource.StreamSource imagesource = new MyImageSource ();
		return new StreamResource(imagesource, "myimage.png", this);				
	}
	
	private static int reloads = 0;
	public class MyImageSource implements StreamResource.StreamSource {

		ByteArrayOutputStream imagebuffer = null;
		
		public MyImageSource() {
			/* Create an image and draw something on it. */
			BufferedImage image = new BufferedImage (200, 200, BufferedImage.TYPE_INT_RGB);
			Graphics drawable = image.getGraphics();
			drawable.setColor(Color.lightGray);
			drawable.fillRect(0,0,200,200);
			drawable.setColor(Color.yellow);
			drawable.fillOval(25,25,150,150);
			drawable.setColor(Color.blue);
			drawable.drawRect(0,0,199,199);
			drawable.setColor(Color.black);
			drawable.drawString("Reloads="+reloads, 75, 100);
			reloads++;
			try {
				/* Write the image to a buffer. */
				imagebuffer = new ByteArrayOutputStream();
				ImageIO.write(image, "png", imagebuffer);
			} catch (IOException e) {
				
			}
		}
		
		@Override
		public InputStream getStream () {

			if (imagebuffer == null) {
				return null;
			} else { 
				return new ByteArrayInputStream(imagebuffer.toByteArray());
			}
		}
	}

}

The reload numbers are expected to be 0, 1, 2, 3, …, 43, 44, 45, … 60, 61, 62, …, but the results are wrong and inconsistent in IE and Firefox:

  1. In IE, the number order is 1, 0, 2, 3, …, 43, 44, 209, 210, …
  2. In Firefox, the order is 46, 47, 48, …, 60, 119, 62, 63, …

Is there anything wrong with my code?

Thanks,
Watt