import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Calendar;

public class HttpAgain extends MIDlet {

    private Display display;
    StringBuffer bfinal;
    //String url = "http://www.javacourses.com/hello.txt";
    String url0 = "http://domain/test0";
    String url1 = "http://domain/test1";
    String url2 = "http://domain/test2";
    public HttpAgain() {
       display = Display.getDisplay(this);
       bfinal = new StringBuffer();
    }

    /**
     * This will be invoked when we start the MIDlet
     */
    public void startApp() {
	try {
	    getViaStreamConnection(url0, "test");
            getViaStreamConnection(url1, "600KB Test");
	    getViaStreamConnection(url2, "1MB Test");
	} catch (IOException e) {
            //Handle Exceptions any other way you like.
            System.out.println("IOException " + e);
            e.printStackTrace();
	}
    }

    /**
     * Pause, discontinue ....
     */
    public void pauseApp() {
	
    }

    /**
     * Destroy must cleanup everything.  
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * read url via stream connection
     */
    void getViaStreamConnection(String url, String TestName) throws IOException {
        StreamConnection c = null;
        InputStream s = null;
        StringBuffer b = new StringBuffer();
        TextBox t = null;
	//Calendar calendar = Calendar.getInstance();
	int total = 0;
        try {
	  long real_start_millis = System.currentTimeMillis();
          c = (StreamConnection)Connector.open(url);
	  //calendar = Calendar.getInstance();
	  //long start_millis = calendar.getTime().getTime();
	  long start_millis = System.currentTimeMillis();
	  b.append("Time to open conn: " + (int)(start_millis-real_start_millis)+ "\n");
	  System.out.println(b.toString());
	  b.delete(0, b.length());
	     
          s = c.openInputStream();
          int ch;
          //while((ch = s.read()) != -1) {
	  byte buf[] = new byte[10000];
	  int last_displayed = 0;
	  while((ch =s.read(buf, 0, 10000))!=-1) {
	     total+=ch;
	     //b.append("Read " + total + " bytes\r");
	     //System.out.println(b.toString());
	     //b.delete(0, b.length());
	     
	     if (total > last_displayed + 100000) { 
		     StringBuffer btemp = new StringBuffer();
		     btemp.append("Read " + (int)(total/1024) + " Kbytes\r");
		     t = new TextBox("hello....", btemp.toString(), 1024, 0);
		     display.setCurrent(t);
		     last_displayed = total; 
	     }
	     //t = new TextBox("hello....", b.toString(), 1024, 0);
	     
             //b.append((char) ch);
          }
	  //calendar = Calendar.getInstance();
	  long end_millis = System.currentTimeMillis();
	  
	  long total_time = end_millis - start_millis;
	  double temp = (8000.0/1024.0)*(double)total/(double)total_time;
	  int bandw = (int)temp;
	  long real_total_time = end_millis - real_start_millis;
	  double real_temp = (8000.0/1024.0)*(double)total/(double)real_total_time;
	  int real_bandw = (int)real_temp;
	  if (total > 1000) {
	    bfinal.append(TestName + "\n");
	    bfinal.append("Bytes: " + total + " Time: " + (int)total_time + "ms.\n Bandw: " + bandw + "\n");
	    //bfinal.append("time from start: " + (int)real_total_time + "ms.\n Real Bandw: " + real_bandw + "\n");
	    bfinal.append("Time to open conn: " + (int)(start_millis - real_start_millis) + "\n");
	    bfinal.append("\n");
             System.out.println(bfinal.toString());
             t = new TextBox("hello....", bfinal.toString(), 1024, 0);
	  }
        } finally {
           if(s != null) {
              s.close();
           }
           if(c != null) {
              c.close();
           }
        }
	//if (total < 1000) {
	//     return;
	//} 
        // display the contents of the file in a text box.
        display.setCurrent(t);
    }
}


