List of usage examples for java.io DataOutputStream close
@Override public void close() throws IOException
From source file:Main.java
public static int execRootCmdSilent(String cmd) { int result = -1; DataOutputStream dos = null; try {/*from ww w . j av a 2s. c o m*/ Process p = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(p.getOutputStream()); dos.writeBytes(cmd + "\n"); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); p.waitFor(); result = p.exitValue(); } catch (Exception e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; }
From source file:com.android.volley.toolbox.http.HurlStack.java
@SuppressWarnings("deprecation") /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards compatibility. // If the request's post body is null, then the assumption is that the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length explicitly, // since this is handled by HttpURLConnection using the size of the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody);/*from ww w. j a v a 2 s . c om*/ out.close(); } break; case Method.GET: // Not necessary to set the request method because connection defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: //connection.setRequestMethod("PATCH"); //If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:Main.java
public static byte[] getBodyBytes(String ip, String port, byte[] key, byte[] ips) throws NumberFormatException, IOException { String[] ipArr = ip.split("\\."); byte[] ipByte = new byte[4]; ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF); ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF); ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF); ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.write(ipByte);/*from w w w . java 2 s. c o m*/ if (!isNum(port)) throw new NumberFormatException("port is not number..."); byte[] portByte = short2bytes(Integer.parseInt(port)); dos.write(portByte); // dos.writeByte(key.getBytes().length); dos.write(key); if (ips != null && ips.length > 0 && dos != null) { dos.write(ips); } byte[] bs = baos.toByteArray(); baos.close(); dos.close(); return bs; }
From source file:com.microsoft.office365.msgraphsnippetapp.SnippetsUnitTests.java
@BeforeClass public static void getAccessTokenUsingPasswordGrant() throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, JSONException { URL url = new URL(TOKEN_ENDPOINT); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); String urlParameters = String.format( "grant_type=%1$s&resource=%2$s&client_id=%3$s&username=%4$s&password=%5$s", GRANT_TYPE, URLEncoder.encode(ServiceConstants.AUTHENTICATION_RESOURCE_ID, "UTF-8"), clientId, username, password);/*from ww w . j a va 2 s . c om*/ connection.setRequestMethod(REQUEST_METHOD); connection.setRequestProperty("Content-Type", CONTENT_TYPE); connection.setRequestProperty("Content-Length", String.valueOf(urlParameters.getBytes("UTF-8").length)); connection.setDoOutput(true); DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream()); dataOutputStream.writeBytes(urlParameters); dataOutputStream.flush(); dataOutputStream.close(); connection.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JsonParser jsonParser = new JsonParser(); JsonObject grantResponse = (JsonObject) jsonParser.parse(response.toString()); accessToken = grantResponse.get("access_token").getAsString(); HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder().addHeader("Authorization", "Bearer " + accessToken) // This header has been added to identify this sample in the Microsoft Graph service. // If you're using this code for your project please remove the following line. .addHeader("SampleID", "android-java-snippets-rest-sample").build(); return chain.proceed(request); } }).addInterceptor(logging).build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(ServiceConstants.AUTHENTICATION_RESOURCE_ID) .client(client).addConverterFactory(GsonConverterFactory.create()).build(); contactService = retrofit.create(MSGraphContactService.class); drivesService = retrofit.create(MSGraphDrivesService.class); eventsService = retrofit.create(MSGraphEventsService.class); groupsService = retrofit.create(MSGraphGroupsService.class); mailService = retrofit.create(MSGraphMailService.class); meService = retrofit.create(MSGraphMeService.class); userService = retrofit.create(MSGraphUserService.class); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.US); dateTime = simpleDateFormat.format(new Date()); }
From source file:com.daidiansha.acarry.control.network.core.volley.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */ static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards // compatibility. // If the request's post body is null, then the assumption is that // the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length // explicitly, // since this is handled by HttpURLConnection using the size of // the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody);//w w w .ja v a2s . c om out.close(); } break; case Method.GET: // Not necessary to set the request method because connection // defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: // connection.setRequestMethod("PATCH"); // If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:Main.java
public static boolean isRoot() { Process process = null;//from w w w . j av a 2s . co m DataOutputStream dos = null; try { process = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(process.getOutputStream()); dos.writeBytes("exit\n"); dos.flush(); int exitValue = process.waitFor(); if (exitValue == 0) { return true; } } catch (IOException e) { } catch (InterruptedException e) { e.printStackTrace(); } finally { if (dos != null) { try { dos.close(); } catch (IOException e) { } } } return false; }
From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java
public static long writeInputStreamAndGetSize(String directory, String filename, InputStream is) throws IOException { File outDir = new File(directory); if (!outDir.exists()) outDir.mkdirs();/* ww w . j a v a 2 s . com*/ File outFile = new File(directory, filename); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))); byte buf[] = new byte[1024]; int len; while ((len = is.read(buf)) > 0) dos.write(buf, 0, len); dos.close(); return outFile.length(); }
From source file:com.androidex.volley.toolbox.HurlStack.java
@SuppressWarnings("deprecation") /* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request) throws IOException, AuthFailureError { switch (request.getMethod()) { case Method.DEPRECATED_GET_OR_POST: // This is the deprecated way that needs to be handled for backwards // compatibility. // If the request's post body is null, then the assumption is that // the request is // GET. Otherwise, it is assumed that the request is a POST. byte[] postBody = request.getPostBody(); if (postBody != null) { // Prepare output. There is no need to set Content-Length // explicitly, // since this is handled by HttpURLConnection using the size of // the prepared // output stream. connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType()); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.write(postBody);//w w w. j a v a 2s . c o m out.close(); } break; case Method.GET: // Not necessary to set the request method because connection // defaults to GET but // being explicit here. connection.setRequestMethod("GET"); break; case Method.DELETE: connection.setRequestMethod("DELETE"); break; case Method.POST: connection.setRequestMethod("POST"); addBodyIfExists(connection, request); break; case Method.PUT: connection.setRequestMethod("PUT"); addBodyIfExists(connection, request); break; case Method.HEAD: connection.setRequestMethod("HEAD"); break; case Method.OPTIONS: connection.setRequestMethod("OPTIONS"); break; case Method.TRACE: connection.setRequestMethod("TRACE"); break; case Method.PATCH: // connection.setRequestMethod("PATCH"); // If server doesnt support patch uncomment this connection.setRequestMethod("POST"); connection.setRequestProperty("X-HTTP-Method-Override", "PATCH"); addBodyIfExists(connection, request); break; default: throw new IllegalStateException("Unknown method type."); } }
From source file:BihuHttpUtil.java
public static String doHttpPost(String xmlInfo, String URL) { System.out.println("??:" + xmlInfo); byte[] xmlData = xmlInfo.getBytes(); InputStream instr = null;/*from ww w . ja va 2 s . c om*/ java.io.ByteArrayOutputStream out = null; try { URL url = new URL(URL); URLConnection urlCon = url.openConnection(); urlCon.setDoOutput(true); urlCon.setDoInput(true); urlCon.setUseCaches(false); urlCon.setRequestProperty("Content-Type", "text/xml"); urlCon.setRequestProperty("Content-length", String.valueOf(xmlData.length)); System.out.println(String.valueOf(xmlData.length)); DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream()); printout.write(xmlData); printout.flush(); printout.close(); instr = urlCon.getInputStream(); byte[] bis = IOUtils.toByteArray(instr); String ResponseString = new String(bis, "UTF-8"); if ((ResponseString == null) || ("".equals(ResponseString.trim()))) { System.out.println(""); } System.out.println("?:" + ResponseString); return ResponseString; } catch (Exception e) { e.printStackTrace(); return "0"; } finally { try { out.close(); instr.close(); } catch (Exception ex) { return "0"; } } }
From source file:Main.java
public static void execCmd(String cmd) { DataOutputStream dos = null; DataInputStream dis = null;//from w w w . j av a 2 s . c o m try { Process p = Runtime.getRuntime().exec("su"); dos = new DataOutputStream(p.getOutputStream()); cmd += "\n"; dos.writeBytes(cmd); dos.flush(); dos.writeBytes("exit\n"); dos.flush(); p.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (dis != null) dis.close(); } catch (IOException e) { e.printStackTrace(); } } }