package edu.nettester.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * Common static methods
 * 
 * @author Daoyuan
 * @since 14-04-20
 */
public class CommonMethod implements Constant {
	
    public static String PHPSESSID;
    public static String M_UID;
    public static String M_UNAME;
    public static String M_HASH;
    
    private static void readServerList(BufferedReader br) throws IOException {
        //BufferedReader br = new BufferedReader(new InputStreamReader(is));
        
        HashMap<String, String> localmap = new LinkedHashMap<String, String>();
        
        while (true) {
            String line = br.readLine();
            if (line == null)
                break;
            
            String[] twos = line.split("\t");
            localmap.put(twos[0], twos[1]);
        }
        
        if (!localmap.isEmpty()) {
            servermap.clear();
            servermap.putAll(localmap);
        }
    }
    
    public static void readServerList(String filepath) throws FileNotFoundException, IOException {
        FileReader fr = new FileReader(filepath);
        BufferedReader br = new BufferedReader(fr);
        readServerList(br);
        br.close();
        fr.close();
    }
    
    public static boolean isFileExists(String filepath) {
        File file = new File(filepath);
        return file.exists();
    }
    
    public static void writeFile(String filepath, InputStream is) throws IOException {
        File file = new File(filepath);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        
        String line;
        while ((line = br.readLine()) != null) {
            bw.write(line + "\n");
        }
        
        bw.close();
        br.close();
    }
    
    public static String transferDOWN_TP(String oldValue) {
        String newValue = oldValue;
        
        return newValue;
    }
    
    public static String transferUP_TP(String oldValue) {
        String newValue = oldValue;
        
        return newValue;
    }
    
    public static String transferAVG_RTT(String oldValue) {
        String newValue = oldValue;
        
        return newValue;
    }

}

@@ -305,32 +305,60 @@
 utStreamReader;%0A
+import java.util.Formatter;%0A
 import java.util
@@ -2206,13 +2206,8 @@
 sfer
-DOWN_
 TP(S
@@ -2272,138 +2272,340 @@
     
-%0A        return newValue;%0A    %7D%0A    %0A    public static String transferUP_TP(String oldValue) %7B%0A        String newValue = oldValue;
+float ov = Float.valueOf(oldValue)/1000;%0A        Formatter fmt = new Formatter();%0A        %0A        if(ov%3E=100) %7B%0A        %09newValue = String.valueOf(Math.floor(ov+0.5));%0A        %7D else if(ov%3E10) %7B%0A        %09newValue = fmt.format(%22%25.1f%22, ov).toString();%0A        %7D else %7B%0A        %09newValue = fmt.format(%22%25.2f%22, ov).toString();%0A        %7D
 %0A   
@@ -2637,32 +2637,36 @@
 alue;%0A    %7D%0A    
+    
 %0A    public stat

update unit transfer method