BandwidthTestUtilpublic class BandwidthTestUtil extends Object
Fields Summary |
---|
private static final String | LOG_TAG |
Methods Summary |
---|
public static boolean | DownloadFromUrl(java.lang.String targetUrl, java.io.File file)Download a given file from a target url to a given destination file.
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.String | buildDownloadUrl(java.lang.String server, int size, java.lang.String deviceId, java.lang.String timestamp)Creates the Download string for the test server.
String downloadUrl = server + "/download?size=" + size + "&device_id=" + deviceId +
"×tamp=" + timestamp;
return downloadUrl;
| public static int | parseIntValueFromFile(java.io.File file)Parses the first line in a file if exists.
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 boolean | postFileToServer(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.
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;
|
|