Skip to content

Instantly share code, notes, and snippets.

@koush
Created November 8, 2010 23:43
Show Gist options
  • Save koush/668494 to your computer and use it in GitHub Desktop.
Save koush/668494 to your computer and use it in GitHub Desktop.
package com.koushikdutta.rommanager;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import android.util.Log;
public class StreamUtility {
private static final String LOGTAG = "ROMManager";
public static int copyStream(InputStream input, OutputStream output) throws IOException
{
byte[] stuff = new byte[1024];
int read = 0;
int total = 0;
while ((read = input.read(stuff)) != -1)
{
output.write(stuff, 0, read);
total += read;
}
return total;
}
public static String readToEnd(InputStream input) throws IOException
{
DataInputStream dis = new DataInputStream(input);
byte[] stuff = new byte[1024];
ByteArrayOutputStream buff = new ByteArrayOutputStream();
int read = 0;
int total = 0;
while ((read = dis.read(stuff)) != -1)
{
buff.write(stuff, 0, read);
total += read;
}
return new String(buff.toByteArray());
}
public static String downloadUrl(String url) throws IOException {
URL downloadUrl = new URL(url);
Log.i(LOGTAG, "Downloading: " + downloadUrl);
DataInputStream input = new DataInputStream(downloadUrl.openStream());
String content = StreamUtility.readToEnd(input);
input.close();
return content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment