List of usage examples for java.io DataOutputStream DataOutputStream
public DataOutputStream(OutputStream out)
From source file:Main.java
public static String runCommand(String[] commands) { DataOutputStream outStream = null; DataInputStream responseStream; try {//from w w w . j a va 2 s .c o m ArrayList<String> logs = new ArrayList<String>(); Process process = Runtime.getRuntime().exec("su"); Log.i(TAG, "Executed su"); outStream = new DataOutputStream(process.getOutputStream()); responseStream = new DataInputStream(process.getInputStream()); for (String single : commands) { Log.i(TAG, "Command = " + single); outStream.writeBytes(single + "\n"); outStream.flush(); if (responseStream.available() > 0) { Log.i(TAG, "Reading response"); logs.add(responseStream.readLine()); Log.i(TAG, "Read response"); } else { Log.i(TAG, "No response available"); } } outStream.writeBytes("exit\n"); outStream.flush(); String log = ""; for (int i = 0; i < logs.size(); i++) { log += logs.get(i) + "\n"; } Log.i(TAG, "Execution compeleted"); return log; } catch (IOException e) { Log.d(TAG, e.getMessage()); } return null; }
From source file:Main.java
public static String do_exec_with_root(String cmd) { String s = "\n"; try {// w w w. j av a2 s .c o m Process su_p = Runtime.getRuntime().exec("su"); DataOutputStream dataOutputStream = new DataOutputStream(su_p.getOutputStream()); dataOutputStream.writeBytes(cmd + "\n"); dataOutputStream.writeBytes("exit" + "\n"); dataOutputStream.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(su_p.getInputStream())); String line = null; while ((line = in.readLine()) != null) { s += line + "\n"; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; }
From source file:Main.java
public static String stringFromHttpPost(String urlStr, String body) { HttpURLConnection conn;/*from w w w .j av a 2 s .c o m*/ try { URL e = new URL(urlStr); conn = (HttpURLConnection) e.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setInstanceFollowRedirects(true); conn.setRequestMethod("POST"); OutputStream os1 = conn.getOutputStream(); DataOutputStream out1 = new DataOutputStream(os1); out1.write(body.getBytes("UTF-8")); out1.flush(); conn.connect(); String line; BufferedReader reader; StringBuffer sb = new StringBuffer(); if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) { reader = new BufferedReader( new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8")); } else { reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); } while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); logError(e.getMessage()); } return null; }
From source file:Main.java
public static void post(String actionUrl, String file) { try {/*w ww .ja v a 2s . c om*/ URL url = new URL(actionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Charset", "UTF-8"); con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****"); DataOutputStream ds = new DataOutputStream(con.getOutputStream()); FileInputStream fStream = new FileInputStream(file); int bufferSize = 1024; // 1MB byte[] buffer = new byte[bufferSize]; int bufferLength = 0; int length; while ((length = fStream.read(buffer)) != -1) { bufferLength = bufferLength + 1; ds.write(buffer, 0, length); } fStream.close(); ds.flush(); InputStream is = con.getInputStream(); int ch; StringBuilder b = new StringBuilder(); while ((ch = is.read()) != -1) { b.append((char) ch); } new String(b.toString().getBytes("ISO-8859-1"), "utf-8"); ds.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static String uploadFile(String filePath, String requestURL) { String result = ""; File file = new File(filePath); try {/*from w ww. ja v a 2 s .com*/ HttpURLConnection connection = initHttpURLConn(requestURL); if (filePath != null) { DataOutputStream dos = new DataOutputStream(connection.getOutputStream()); StringBuffer sb = new StringBuffer(); InputStream is = new FileInputStream(filePath); byte[] bytes = new byte[1024]; int len = 0; while ((len = is.read(bytes)) != -1) { dos.write(bytes, 0, len); } dos.flush(); is.close(); int res = connection.getResponseCode(); Log.e(TAG, "response code:" + res); if (res == 200) { Log.e(TAG, "request success"); InputStream input = connection.getInputStream(); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = input.read()) != -1) { sb1.append((char) ss); } result = sb1.toString(); Log.d(TAG, "result: " + result); input.close(); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static boolean restartThermald(Process su) { try {/*from ww w . j a v a2s .c o m*/ DataOutputStream out = new DataOutputStream(su.getOutputStream()); out.writeBytes("stop thermald\n"); try { Thread.sleep(1000); } catch (Exception e) { } out.writeBytes("start thermald\n"); } catch (Exception e) { String Error = "Error restarting thermald. Exception: "; Log.e(TAG, Error, e); return false; } return true; }
From source file:Main.java
public static boolean fileWriteOneLine(String fname, String value, Process su) { try {/*w w w . j a va 2s . c o m*/ DataOutputStream out = new DataOutputStream(su.getOutputStream()); out.writeBytes("echo " + value + " > " + fname + "\n"); } catch (Exception e) { String Error = "Error writing to " + fname + ". Exception: "; Log.e(TAG, Error, e); return false; } return true; }
From source file:Main.java
private static byte[] ExecuteCommand(String command, Boolean useroot, boolean forcenew) throws Exception { Process p;/*from w w w .j a v a2 s . c o m*/ DataOutputStream stdin; InputStream stdout; ByteArrayOutputStream baos; int read; byte[] buffer; /* * If for any reason the command does not print anything we are stuck forever. * Make sure that we print SOMETHING ALWAYS! */ command = "RESULT=$(" + command + "); if [[ $RESULT == '' ]]; then echo '#null#';else echo $RESULT;fi\n"; p = getProcess(useroot, forcenew); stdin = new DataOutputStream(p.getOutputStream()); stdout = p.getInputStream(); buffer = new byte[BUFF_LEN]; baos = new ByteArrayOutputStream(); stdin.writeBytes(command); while (true) { read = stdout.read(buffer); baos.write(buffer, 0, read); if (read < BUFF_LEN) { //we have read everything break; } } if (forcenew) { stdin.writeBytes("exit\n"); stdin.flush(); stdin.close(); } //p.waitFor(); return baos.toByteArray(); }
From source file:Main.java
public static void writeInts(String file, int[] ints) throws IOException { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 4 * 1024)); try {//from w w w . jav a 2s.co m int len = ints.length; out.writeInt(len); for (int i = 0; i < len; i++) { out.writeInt(ints[i]); } } finally { out.close(); } }
From source file:Main.java
public static void writeFloats(String file, float[] floats) throws IOException { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 4 * 1024)); try {/*from w ww .j ava 2 s . c om*/ int len = floats.length; out.writeInt(len); for (int i = 0; i < len; i++) { out.writeFloat(floats[i]); } } finally { out.close(); } }