FileDocCategorySizeDatePackage
BandwidthTestUtil.javaAPI DocAndroid 5.1 API5705Thu Mar 12 22:22:12 GMT 2015com.android.bandwidthtest.util

BandwidthTestUtil

public class BandwidthTestUtil extends Object

Fields Summary
private static final String
LOG_TAG
Constructors Summary
Methods Summary
public static booleanDownloadFromUrl(java.lang.String targetUrl, java.io.File file)
Download a given file from a target url to a given destination file.

param
targetUrl the url to download
param
file the {@link File} location where to save to
return
true if it succeeded

        try {
            URL url = new URL(targetUrl);
            Log.d(LOG_TAG, "Download begining");
            Log.d(LOG_TAG, "Download url:" + url);
            Log.d(LOG_TAG, "Downloaded file name:" + file.getAbsolutePath());
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;
            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {
            Log.d(LOG_TAG, "Failed to download file with error: " + e);
            return false;
        }
        return true;
    
public static java.lang.StringbuildDownloadUrl(java.lang.String server, int size, java.lang.String deviceId, java.lang.String timestamp)
Creates the Download string for the test server.

param
server url of the test server
param
size in bytes of the file to download
param
deviceId the device id that is downloading
param
timestamp
return
download url

        String downloadUrl = server + "/download?size=" + size + "&device_id=" + deviceId +
                "×tamp=" + timestamp;
        return downloadUrl;
    
public static intparseIntValueFromFile(java.io.File file)
Parses the first line in a file if exists.

param
file {@link File} the input
return
the integer value of the first line of the file.

                                   
         
        int value = 0;
        if (file.exists()) {
            try {
                FileInputStream fstream = new FileInputStream(file);
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine = br.readLine();
                if (strLine != null) {
                    value = Integer.parseInt(strLine);
                }
                // Close the input stream
                in.close();
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }
        }
        return value;
    
public static booleanpostFileToServer(java.lang.String server, java.lang.String deviceId, java.lang.String timestamp, java.io.File file)
Post a given file for a given device and timestamp to the server.

param
server {@link String} url of test server
param
deviceId {@link String} device id that is uploading
param
timestamp {@link String} timestamp
param
file {@link File} to upload
return
true if it succeeded

        try {
            Log.d(LOG_TAG, "Uploading begining");
            HttpClient httpClient = new DefaultHttpClient();
            String uri = server;
            if (!uri.endsWith("/")) {
                uri += "/";
            }
            uri += "upload";
            Log.d(LOG_TAG, "Upload url:" + uri);
            HttpPost postRequest = new HttpPost(uri);
            Part[] parts = {
                    new StringPart("device_id", deviceId),
                    new StringPart("timestamp", timestamp),
                    new FilePart("file", file)
            };
            MultipartEntity reqEntity = new MultipartEntity(parts, postRequest.getParams());
            postRequest.setEntity(reqEntity);
            HttpResponse res = httpClient.execute(postRequest);
            res.getEntity().getContent().close();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Could not upload file with error: " + e);
            return false;
        }
        return true;