Android Utility Classes
Here are a few classes, I have created during my learning of android application development.
package com.mindzgroup.android.utilities;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* A class to download the contents from any url
* <p><strong>Author:</strong> Anant Anand Gupta<br/>
* <strong>Date:</strong> 6 December 2015<br/>
* <strong>Name:</strong> DownloadManager<br/>
* <strong>Usage:</strong> <br/> <pre>{@code
* // get the string data from url
* DownloadManager.getStringFromURL(new URL("http://www.google.com/");
*
* // get the binary data from url
* DownloadManager.getBytesFromURL(new URL("http://www.google.com/");
* }
* </pre></p>
*/
public class DownloadManager {
public static String getStringFromURL(URL url) throws IOException {
String retData = null;
byte[] byteData = getBytesFromURL(url);
if (byteData != null) {
retData = new String(byteData);
}
return retData;
}
public static byte[] getBytesFromURL(URL url) throws IOException {
byte[] retData;
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
urlConnection.setRequestProperty("Accept", "*/*");
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.connect();
boolean isError = urlConnection.getResponseCode() >= 400;
InputStream inputStream = isError ? urlConnection.getErrorStream() : urlConnection.getInputStream();
if (!isError) {
retData = StreamManager.ReadBytesFromStream(inputStream);
} else {
retData = null;
}
if (inputStream != null) {
inputStream.close();
}
urlConnection.disconnect();
return retData;
}
}
package com.mindzgroup.android.utilities;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* A class to read data from InputStream
* <p><strong>Author:</strong> Anant Anand Gupta<br/>
* <strong>Date:</strong> 6 December 2015<br/>
* <strong>Name:</strong> StreamManager<br/>
* <strong>Usage:</strong> <br/> <pre>{@code
* // get the string data from url
* StreamManager.readStringFromStream(inputStream);
*
* // get the binary data from url
* StreamManager.readBytesFromStream(inputStream);
* }
* </pre></p>
*/
public class StreamManager {
public static byte[] readBytesFromStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream contents = new ByteArrayOutputStream();
int bufferLength;
// create a buffer...
byte[] buffer = new byte[10240];
while ((bufferLength = inputStream.read(buffer)) > 0) {
contents.write(buffer,0,bufferLength);
}
return contents.toByteArray();
}
public static String readStringFromStream(InputStream inputStream) throws IOException {
String contents = "";
int bufferLength;
// create a buffer...
byte[] buffer = new byte[10240];
while ((bufferLength = inputStream.read(buffer)) > 0) {
String readData = new String(buffer,0,bufferLength);
contents += readData;
}
return contents;
}
}