1 package com.koushikdutta.superuser.util;
3 import java.io.ByteArrayOutputStream;
4 import java.io.DataInputStream;
5 import java.io.DataOutputStream;
7 import java.io.FileInputStream;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.nio.ByteBuffer;
13 import java.nio.channels.Channels;
14 import java.nio.channels.ReadableByteChannel;
15 import java.nio.channels.WritableByteChannel;
17 import org.json.JSONException;
18 import org.json.JSONObject;
20 import java.net.HttpURLConnection;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
25 public class StreamUtility {
26 //private static final String LOGTAG = StreamUtility.class.getSimpleName();
27 public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
28 final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 17);
29 while (src.read(buffer) != -1) {
30 // prepare the buffer to be drained
32 // write to the channel, may block
34 // If partial transfer, shift remainder down
35 // If buffer is empty, same as doing clear()
38 // EOF will leave buffer in fill state
40 // make sure the buffer is fully drained.
41 while (buffer.hasRemaining()) {
46 public static void copyStream(InputStream input, OutputStream output) throws IOException
48 final ReadableByteChannel inputChannel = Channels.newChannel(input);
49 final WritableByteChannel outputChannel = Channels.newChannel(output);
51 fastChannelCopy(inputChannel, outputChannel);
54 public static String downloadUriAsString(String uri) throws IOException {
55 URL get = new URL(uri);
56 return downloadUriAsString(get);
60 public static String downloadUriAsString(final URL req) throws IOException {
61 InputStream in = req.openStream();
62 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
63 StringBuilder result = new StringBuilder();
65 while((line = reader.readLine()) != null) {
68 return result.toString();
71 public static JSONObject downloadUriAsJSONObject(String uri) throws IOException, JSONException {
72 return new JSONObject(downloadUriAsString(uri));
75 public static JSONObject downloadUriAsJSONObject(URL req) throws IOException, JSONException {
76 return new JSONObject(downloadUriAsString(req));
79 public static byte[] readToEndAsArray(InputStream input) throws IOException
81 DataInputStream dis = new DataInputStream(input);
82 byte[] stuff = new byte[1024];
83 ByteArrayOutputStream buff = new ByteArrayOutputStream();
85 while ((read = dis.read(stuff)) != -1)
87 buff.write(stuff, 0, read);
90 return buff.toByteArray();
93 public static void eat(InputStream input) throws IOException {
94 byte[] stuff = new byte[1024];
95 while (input.read(stuff) != -1);
98 public static String readToEnd(InputStream input) throws IOException
100 return new String(readToEndAsArray(input));
103 static public String readFile(String filename) throws IOException {
104 return readFile(new File(filename));
107 static public String readFile(File file) throws IOException {
108 byte[] buffer = new byte[(int) file.length()];
109 DataInputStream input = new DataInputStream(new FileInputStream(file));
110 input.readFully(buffer);
112 return new String(buffer);
115 public static void writeFile(File file, String string) throws IOException {
116 writeFile(file.getAbsolutePath(), string);
119 public static void writeFile(String file, String string) throws IOException {
120 File f = new File(file);
121 f.getParentFile().mkdirs();
122 DataOutputStream dout = new DataOutputStream(new FileOutputStream(f));
123 dout.write(string.getBytes());