Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.TextUtils;

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static long getNetSpeed() {
        ProcessBuilder cmd;
        long readBytes = 0;
        BufferedReader rd = null;
        try {
            String[] args = { "/system/bin/cat", "/proc/net/dev" };
            cmd = new ProcessBuilder(args);
            Process process = cmd.start();
            rd = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                if (line.contains("lan0") || line.contains("eth0")) {
                    String[] delim = line.split(":");
                    if (delim.length >= 2) {
                        readBytes = parserNumber(delim[1].trim());
                        break;
                    }
                }
            }
            rd.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (rd != null) {
                try {
                    rd.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return readBytes;
    }

    private static long parserNumber(String line) throws Exception {
        long ret = 0;
        if (!TextUtils.isEmpty(line)) {
            String[] delim = line.split(" ");
            if (delim.length >= 1) {
                ret = Long.parseLong(delim[0]);
            }
        }
        return ret;
    }
}