List of usage examples for java.io DataOutputStream writeBytes
public final void writeBytes(String s) throws IOException
From source file:com.webarch.common.net.http.HttpService.java
/** * ?url// ww w . j a v a2s. c o m * * @param inputStream ? * @param fileName ?? * @param fileType * @param contentType * @param identity * @return */ public static String uploadMediaFile(String requestUrl, FileInputStream inputStream, String fileName, String fileType, String contentType, String identity) { String lineEnd = System.getProperty("line.separator"); String twoHyphens = "--"; String boundary = "*****"; String result = null; try { //? URL submit = new URL(requestUrl); HttpURLConnection conn = (HttpURLConnection) submit.openConnection(); // conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); //? conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", DEFAULT_CHARSET); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); //?? DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + fileName + ";Content-Type=\"" + contentType + lineEnd); dos.writeBytes(lineEnd); byte[] buffer = new byte[8192]; // 8k int count = 0; while ((count = inputStream.read(buffer)) != -1) { dos.write(buffer, 0, count); } inputStream.close(); //? dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is, DEFAULT_CHARSET); BufferedReader br = new BufferedReader(isr); result = br.readLine(); dos.close(); is.close(); } catch (IOException e) { logger.error("", e); } return result; }
From source file:com.theonespy.util.Util.java
public static void disableMobileData() { Util.Log("Disable mobile data"); try {/*from w w w .j a v a 2 s .c om*/ Process su = Runtime.getRuntime().exec("su"); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes("svc data disable\n "); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); try { su.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } outputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.apache.hadoop.mapred.TestKillSubProcesses.java
/** * Runs a recursive shell script to create a chain of subprocesses *//* ww w . java 2 s . c om*/ private static void runChildren(JobConf conf) throws IOException { if (ProcessTree.isSetsidAvailable) { FileSystem fs = FileSystem.getLocal(conf); if (fs.exists(scriptDir)) { fs.delete(scriptDir, true); } // Create the directory and set open permissions so that the TT can // access. fs.mkdirs(scriptDir); fs.setPermission(scriptDir, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL)); // create shell script Random rm = new Random(); Path scriptPath = new Path(scriptDirName, "_shellScript_" + rm.nextInt() + ".sh"); String shellScript = scriptPath.toString(); // Construct the script. Set umask to 0000 so that TT can access all the // files. String script = "umask 000\n" + "echo $$ > " + scriptDirName + "/childPidFile" + "$1\n" + "echo hello\n" + "trap 'echo got SIGTERM' 15 \n" + "if [ $1 != 0 ]\nthen\n" + " sh " + shellScript + " $(($1-1))\n" + "else\n" + " while true\n do\n" + " sleep 2\n" + " done\n" + "fi"; DataOutputStream file = fs.create(scriptPath); file.writeBytes(script); file.close(); // Set executable permissions on the script. new File(scriptPath.toUri().getPath()).setExecutable(true); LOG.info("Calling script from map task : " + shellScript); Runtime.getRuntime().exec(shellScript + " " + numLevelsOfSubProcesses); String childPid = UtilsForTests.getPidFromPidFile(scriptDirName + "/childPidFile" + 0); while (childPid == null) { LOG.warn(scriptDirName + "/childPidFile" + 0 + " is null; Sleeping..."); try { Thread.sleep(500); } catch (InterruptedException ie) { LOG.warn("sleep is interrupted:" + ie); break; } childPid = UtilsForTests.getPidFromPidFile(scriptDirName + "/childPidFile" + 0); } } }
From source file:com.sinpo.xnfc.nfc.Util.java
/** * ??? Root??(ROOT??)/*w ww . jav a2 s .c om*/ * * @return ?/??Root?? */ public static boolean upgradeRootPermission(String pkgCodePath) { Process process = null; DataOutputStream os = null; try { String cmd = "chmod 777 " + pkgCodePath; process = Runtime.getRuntime().exec("su"); //?root?? os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } return true; }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doPOST(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;//from w ww .j a va2 s .co m URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // connection.setRequestProperty("token",token); connection.setConnectTimeout(30000); connection.setReadTimeout(30000); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); String content = ""; if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } out.writeBytes(content.substring(1)); } out.flush(); out.close(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:eu.crowdrec.contest.sender.RequestSender.java
/** * Send a line from a logFile to an HTTP server (single-threaded). * // w w w . j ava2 s . com * @param logline the line that should by sent * * @param connection the connection to the http server, must not be null * * @return the response or null (if an error has been detected) */ static private String excutePost(final String logline, final String serverURL) { // split the logLine into several token String[] token = logline.split("\t"); String type = token[0]; String property = token[3]; String entity = token[4]; // encode the content as URL parameters. String urlParameters = ""; try { urlParameters = String.format("type=%s&properties=%s&entities=%s", URLEncoder.encode(type, "UTF-8"), URLEncoder.encode(property, "UTF-8"), URLEncoder.encode(entity, "UTF-8")); } catch (UnsupportedEncodingException e1) { logger.warn(e1.toString()); } // initialize a HTTP connection to the server // reuse of connection objects is delegated to the JVM HttpURLConnection connection = null; try { final URL url = new URL(serverURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); // Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); // Get Response BufferedReader rd = null; InputStream is = null; try { is = connection.getInputStream(); rd = new BufferedReader(new InputStreamReader(is)); StringBuffer response = new StringBuffer(); for (String line = rd.readLine(); line != null; line = rd.readLine()) { response.append(line); response.append(" "); } return response.toString(); } catch (IOException e) { logger.warn("receivind response failed, ignored."); } finally { if (is != null) { is.close(); } if (rd != null) { rd.close(); } } } catch (MalformedURLException e) { System.err.println("invalid server URL, program stopped."); System.exit(-1); } catch (IOException e) { System.err.println("i/o error connecting the http server, program stopped. e=" + e); System.exit(-1); } catch (Exception e) { System.err.println("general error connecting the http server, program stopped. e:" + e); e.printStackTrace(); System.exit(-1); } finally { // close the connection if (connection != null) { connection.disconnect(); } } return null; }
From source file:Main.java
public static String runCommand(String[] commands) { DataOutputStream outStream = null; DataInputStream responseStream; try {/*from w ww.j a v a 2s . c om*/ 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:org.dcm4che3.tool.stowrs.StowRS.java
private static void writeBulkDataPart(MediaType mediaType, DataOutputStream wr, String uri, List<BulkData> chunks) throws IOException { wr.writeBytes("\r\n--" + MULTIPART_BOUNDARY + "\r\n"); wr.writeBytes("Content-Type: " + toContentType(mediaType) + " \r\n"); wr.writeBytes("Content-Location: " + uri + " \r\n"); wr.writeBytes("\r\n"); for (BulkData chunk : chunks) { writeBulkDataToStream(chunk, wr); }// w ww .j a v a 2 s .co m }
From source file:com.sinpo.xnfc.nfc.Util.java
public static String execRootCmd(String cmd) { String result = ""; DataOutputStream dos = null; DataInputStream dis = null;//from w w w . j a v a 2s .com try { Process p = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(p.getOutputStream()); dis = new DataInputStream(p.getInputStream()); dos.writeBytes(cmd + "\n"); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); String line = null; while ((line = dis.readLine()) != null) { result += line + "\r\n"; } p.waitFor(); } catch (Exception e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } if (dis != null) { try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
From source file:com.flozano.socialauth.util.HttpUtil.java
public static void write(final DataOutputStream out, final String outStr) throws IOException { out.writeBytes(outStr); LOG.debug(outStr);//from w w w .j av a 2s .c om }