List of usage examples for java.io OutputStream write
public void write(byte b[]) throws IOException
b.length
bytes from the specified byte array to this output stream. From source file:com.github.terma.m.node.Node.java
private static void send(final String serverHost, final int serverPort, final String context, final List<Event> events) throws IOException { final HttpURLConnection connection = (HttpURLConnection) new URL("http", serverHost, serverPort, context + "/node").openConnection(); connection.setDoOutput(true);//from w w w. j av a 2 s .co m connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/json"); connection.setRequestProperty("charset", "utf-8"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); connection.connect(); OutputStream outputStream = connection.getOutputStream(); outputStream.write(new Gson().toJson(events).getBytes()); connection.getInputStream().read(); outputStream.close(); }
From source file:JavaCloud.Utils.java
public static Object request(String address, String function, final JSONObject params) throws CoreException { try {// ww w. ja v a2s.c om GenericUrl url = new GenericUrl(address + "/" + function); NetHttpTransport transport = new NetHttpTransport(); HttpRequest request = transport.createRequestFactory().buildPostRequest(url, new HttpContent() { @Override public long getLength() throws IOException { return params.toString().length(); } @Override public String getType() { return "text/json"; } @Override public boolean retrySupported() { return false; } @Override public void writeTo(OutputStream outputStream) throws IOException { outputStream.write(params.toString().getBytes()); } }); HttpResponse response = request.execute(); JSONParser parser = new JSONParser(); JSONObject object = (JSONObject) parser.parse(response.parseAsString()); if (!object.get("status").equals("ok")) { throw new CoreException(object.get("status").toString()); } if (object.containsKey("data") && object.get("data") != null) return parser.parse(object.get("data").toString()); else return null; } catch (ParseException e) { System.out.println("Error parsing response: " + e.toString()); throw new CoreException("json_malformed"); } catch (IOException e) { System.out.println("Error connecting to server: " + e.toString()); throw new CoreException("connection_failed"); } }
From source file:Main.java
public static void pushFileFromRAW(Context mContext, File outputFile, int RAW, boolean Override) throws IOException { if (!outputFile.exists() || Override) { if (Override && outputFile.exists()) if (!outputFile.delete()) { throw new IOException(outputFile.getName() + " can't be deleted!"); }//from w ww . ja v a2 s . c o m InputStream is = mContext.getResources().openRawResource(RAW); OutputStream os = new FileOutputStream(outputFile); byte[] data = new byte[is.available()]; is.read(data); os.write(data); is.close(); os.close(); } }
From source file:com.bytelightning.opensource.pokerface.ProxySpecificTest.java
/** * Context handler for the SunHttpServer *//*from w ww . j av a 2 s . com*/ protected static void OnRemoteTargetRequest(HttpExchange exchange) { Headers rspHdrs = exchange.getResponseHeaders(); rspHdrs.set("Date", Utils.GetHTTPDateFormater().format(new Date())); rspHdrs.set("Content-Type", "text/html"); try { exchange.sendResponseHeaders(200, 0); OutputStream out = exchange.getResponseBody(); out.write("<htm><head><title>TEST</title></head><body>TEST</body></html>".getBytes("utf-8")); out.close(); } catch (IOException e) { Assert.fail(e.getMessage()); } }
From source file:Main.java
private static void killProcess(String packageName) { OutputStream out = process.getOutputStream(); String cmd = "am force-stop " + packageName + " \n"; try {//from w w w .j a v a2 s . c o m out.write(cmd.getBytes()); out.flush(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Issue a POST request to the server.//from w w w. j a v a 2 s .com * * @param endpoint * POST address. * @param params * request parameters. * * @throws java.io.IOException * propagated from POST. */ public static String post(String endpoint, Map<String, String> params) throws IOException { URL url; try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } // Get Response InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\n'); } rd.close(); return response.toString(); } finally { if (conn != null) { conn.disconnect(); } } }
From source file:Main.java
/** * Issue a POST request to the server./* w w w . j a v a 2 s . c o m*/ * * @param endpoint * POST address. * @param params * request parameters. * * @throws IOException * propagated from POST. */ public static String post_t(String endpoint, Map<String, Object> params, String contentType) throws IOException { URL url; try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Entry<String, Object>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Entry<String, Object> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", contentType); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } // Get Response InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\n'); } rd.close(); return response.toString(); } finally { if (conn != null) { conn.disconnect(); } } }
From source file:brickhouse.udf.bloom.BloomFactory.java
public static void WriteBloomToStream(OutputStream stream, Filter bloom) throws IOException { stream.write(WriteBloomToString(bloom).getBytes()); stream.flush();/* ww w . j a v a 2s .co m*/ }
From source file:com.appdynamics.common.RESTClient.java
public static void sendPost(String urlString, String input, String apiKey) { try {//from w ww . j a v a 2s . c o m URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBase64((apiKey).getBytes()))); OutputStream os = conn.getOutputStream(); os.write(input.getBytes()); os.flush(); if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; logger.info("Output from Server .... \n"); while ((output = br.readLine()) != null) { logger.info(output); } conn.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void writeCString(OutputStream os, String s, String characterSet) throws IOException { if (s == null || s.length() == 0) { os.write(0); return;/*from w w w . j av a 2 s .c om*/ } if (characterSet == null) { characterSet = "utf-8"; } byte[] bytes = s.getBytes(characterSet); writeCLenData(os, bytes); }