OSDN Git Service

Fixes for koush's Superuser on marshmallow
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / util / StreamUtility.java
1 package com.koushikdutta.superuser.util;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.DataInputStream;
5 import java.io.DataOutputStream;
6 import java.io.File;
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;
16
17 import org.json.JSONException;
18 import org.json.JSONObject;
19
20 import java.net.HttpURLConnection;
21 import java.net.URL;
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24
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
31             buffer.flip();
32             // write to the channel, may block
33             dest.write(buffer);
34             // If partial transfer, shift remainder down
35             // If buffer is empty, same as doing clear()
36             buffer.compact();
37         }
38         // EOF will leave buffer in fill state
39         buffer.flip();
40         // make sure the buffer is fully drained.
41         while (buffer.hasRemaining()) {
42             dest.write(buffer);
43         }
44     }
45
46     public static void copyStream(InputStream input, OutputStream output) throws IOException
47     {
48         final ReadableByteChannel inputChannel = Channels.newChannel(input);
49         final WritableByteChannel outputChannel = Channels.newChannel(output);
50         // copy the channels
51         fastChannelCopy(inputChannel, outputChannel);
52     }
53
54     public static String downloadUriAsString(String uri) throws IOException {
55         URL get = new URL(uri);
56         return downloadUriAsString(get);
57     }
58
59
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();
64         String line;
65         while((line = reader.readLine()) != null) {
66             result.append(line);
67         }
68         return result.toString(); 
69     }
70
71     public static JSONObject downloadUriAsJSONObject(String uri) throws IOException, JSONException {
72         return new JSONObject(downloadUriAsString(uri));
73     }
74
75     public static JSONObject downloadUriAsJSONObject(URL req) throws IOException, JSONException {
76         return new JSONObject(downloadUriAsString(req));
77     }
78
79     public static byte[] readToEndAsArray(InputStream input) throws IOException
80     {
81         DataInputStream dis = new DataInputStream(input);
82         byte[] stuff = new byte[1024];
83         ByteArrayOutputStream buff = new ByteArrayOutputStream();
84         int read = 0;
85         while ((read = dis.read(stuff)) != -1)
86         {
87             buff.write(stuff, 0, read);
88         }
89         input.close();
90         return buff.toByteArray();
91     }
92
93     public static void eat(InputStream input) throws IOException {
94         byte[] stuff = new byte[1024];
95         while (input.read(stuff) != -1);
96     }
97
98  public static String readToEnd(InputStream input) throws IOException
99  {
100      return new String(readToEndAsArray(input));
101  }
102
103     static public String readFile(String filename) throws IOException {
104         return readFile(new File(filename));
105     }
106
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);
111         input.close();
112         return new String(buffer);
113     }
114
115     public static void writeFile(File file, String string) throws IOException {
116         writeFile(file.getAbsolutePath(), string);
117     }
118
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());
124         dout.close();
125     }
126 }
127