diff options
author | Koushik Dutta <koushd@gmail.com> | 2013-09-10 22:06:55 -0700 |
---|---|---|
committer | Koushik Dutta <koushd@gmail.com> | 2013-09-10 22:06:55 -0700 |
commit | 22a2ee68b0b23379593baf007b911f3b043f2088 (patch) | |
tree | 89a35ee07f6331f23846552172fb6f21079c82a0 /AndroidAsyncTest/src/com | |
parent | c7fbf81aa1d58b288fe42ce4569a3ca8c5300016 (diff) | |
download | AndroidAsync-22a2ee68b0b23379593baf007b911f3b043f2088.tar.gz AndroidAsync-22a2ee68b0b23379593baf007b911f3b043f2088.tar.bz2 AndroidAsync-22a2ee68b0b23379593baf007b911f3b043f2088.zip |
move StreamUtility
Change-Id: I52626e70495dc40a4cd039ea4f2cec23a7952d3a
Diffstat (limited to 'AndroidAsyncTest/src/com')
3 files changed, 2 insertions, 132 deletions
diff --git a/AndroidAsyncTest/src/com/koushikdutta/async/test/FileTests.java b/AndroidAsyncTest/src/com/koushikdutta/async/test/FileTests.java index 8b2ae36..1d37239 100644 --- a/AndroidAsyncTest/src/com/koushikdutta/async/test/FileTests.java +++ b/AndroidAsyncTest/src/com/koushikdutta/async/test/FileTests.java @@ -5,6 +5,7 @@ import com.koushikdutta.async.FileDataEmitter; import com.koushikdutta.async.future.Future; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.async.parser.StringParser; +import com.koushikdutta.async.util.StreamUtility; import junit.framework.TestCase; diff --git a/AndroidAsyncTest/src/com/koushikdutta/async/test/HttpServerTests.java b/AndroidAsyncTest/src/com/koushikdutta/async/test/HttpServerTests.java index 3d6ca3c..937ed8a 100644 --- a/AndroidAsyncTest/src/com/koushikdutta/async/test/HttpServerTests.java +++ b/AndroidAsyncTest/src/com/koushikdutta/async/test/HttpServerTests.java @@ -12,6 +12,7 @@ import com.koushikdutta.async.http.server.AsyncHttpServer; import com.koushikdutta.async.http.server.AsyncHttpServerRequest; import com.koushikdutta.async.http.server.AsyncHttpServerResponse; import com.koushikdutta.async.http.server.HttpServerRequestCallback; +import com.koushikdutta.async.util.StreamUtility; import junit.framework.TestCase; diff --git a/AndroidAsyncTest/src/com/koushikdutta/async/test/StreamUtility.java b/AndroidAsyncTest/src/com/koushikdutta/async/test/StreamUtility.java deleted file mode 100644 index d381f94..0000000 --- a/AndroidAsyncTest/src/com/koushikdutta/async/test/StreamUtility.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.koushikdutta.async.test; - -import android.net.http.AndroidHttpClient; - -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpUriRequest; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.ByteBuffer; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.WritableByteChannel; - -class StreamUtility { - public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { - final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); - while (src.read(buffer) != -1) { - // prepare the buffer to be drained - buffer.flip(); - // write to the channel, may block - dest.write(buffer); - // If partial transfer, shift remainder down - // If buffer is empty, same as doing recycle() - buffer.compact(); - } - // EOF will leave buffer in fill state - buffer.flip(); - // make sure the buffer is fully drained. - while (buffer.hasRemaining()) { - dest.write(buffer); - } - } - - public static void copyStream(InputStream input, OutputStream output) throws IOException - { -// final ReadableByteChannel inputChannel = Channels.newChannel(input); -// final WritableByteChannel outputChannel = Channels.newChannel(output); -// // copy the channels -// fastChannelCopy(inputChannel, outputChannel); -// // closing the channels -//// inputChannel.close(); -//// outputChannel.close(); - - - byte[] stuff = new byte[65536]; - int read = 0; - int total = 0; - while ((read = input.read(stuff)) != -1) - { - output.write(stuff, 0, read); - total += read; - } -// return total; - } - - public static String downloadUriAsString(String uri) throws IOException { - HttpGet get = new HttpGet(uri); - return downloadUriAsString(get); - } - - - public static String downloadUriAsString(final HttpUriRequest req) throws IOException { - AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); - try { - HttpResponse res = client.execute(req); - return readToEnd(res.getEntity().getContent()); - } - finally { - client.close(); - } - } - - public static JSONObject downloadUriAsJSONObject(String uri) throws IOException, JSONException { - return new JSONObject(downloadUriAsString(uri)); - } - - public static JSONObject downloadUriAsJSONObject(HttpUriRequest req) throws IOException, JSONException { - return new JSONObject(downloadUriAsString(req)); - } - - public static byte[] readToEndAsArray(InputStream input) throws IOException - { - DataInputStream dis = new DataInputStream(input); - byte[] stuff = new byte[1024]; - ByteArrayOutputStream buff = new ByteArrayOutputStream(); - int read = 0; - while ((read = dis.read(stuff)) != -1) - { - buff.write(stuff, 0, read); - } - dis.close(); - return buff.toByteArray(); - } - - public static String readToEnd(InputStream input) throws IOException - { - return new String(readToEndAsArray(input)); - } - - static public String readFile(String filename) throws IOException { - return readFile(new File(filename)); - } - - static public String readFile(File file) throws IOException { - byte[] buffer = new byte[(int) file.length()]; - DataInputStream input = new DataInputStream(new FileInputStream(file)); - input.readFully(buffer); - return new String(buffer); - } - - public static void writeFile(File file, String string) throws IOException { - file.getParentFile().mkdirs(); - DataOutputStream dout = new DataOutputStream(new FileOutputStream(file)); - dout.write(string.getBytes()); - dout.close(); - } - - public static void writeFile(String file, String string) throws IOException { - writeFile(new File(file), string); - } -} - |