Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

In this page you can find the example usage for java.io OutputStream write.

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this output stream.

Usage

From source file:com.starbucks.apps.HttpUtils.java

private static HttpInvocationContext invoke(HttpUriRequest request, final String payload,
        final String contentType) throws IOException {

    if (payload != null) {
        HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
        EntityTemplate ent = new EntityTemplate(new ContentProducer() {
            public void writeTo(OutputStream outputStream) throws IOException {
                outputStream.write(payload.getBytes());
                outputStream.flush();// w w  w.java2 s  .  c o  m
            }
        });
        ent.setContentType(contentType);
        entityEncReq.setEntity(ent);
    }

    HttpInvocationContext context = new HttpInvocationContext(payload);
    DefaultHttpClient client = getHttpClient(context);
    HttpResponse response = client.execute(request);
    context.setHttpResponse(response);
    return context;
}

From source file:io.helixservice.feature.configuration.cloudconfig.CloudConfigDecrypt.java

/**
 * Decrypt an encrypted string/*  ww w.j a  v a 2  s.c  o m*/
 * <p>
 * This method blocks on a HTTP request.
 *
 * @param name property or filename for reference/logging
 * @param encryptedValue Encrypted string
 * @param cloudConfigUri URI of the Cloud Config server
 * @param httpBasicHeader HTTP Basic header containing username and password for Cloud Config server
 * @return
 */
public static String decrypt(String name, String encryptedValue, String cloudConfigUri,
        String httpBasicHeader) {
    String result = encryptedValue;

    // Remove prefix if needed
    if (encryptedValue.startsWith(CIPHER_PREFIX)) {
        encryptedValue = encryptedValue.substring(CIPHER_PREFIX.length());
    }

    String decryptUrl = cloudConfigUri + "/decrypt";

    try {
        HttpURLConnection connection = (HttpURLConnection) new URL(decryptUrl).openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.setRequestMethod("POST");
        connection.addRequestProperty(AUTHORIZATION_HEADER, httpBasicHeader);
        connection.setRequestProperty("Content-Type", "text/plain");
        connection.setRequestProperty("Content-Length", Integer.toString(encryptedValue.getBytes().length));
        connection.setRequestProperty("Accept", "*/*");

        // Write body
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(encryptedValue.getBytes());
        outputStream.close();

        if (connection.getResponseCode() == 200) {
            InputStream inputStream = connection.getInputStream();
            result = IOUtils.toString(inputStream);
            inputStream.close();
        } else {
            LOG.error("Unable to Decrypt name=" + name + " due to httpStatusCode="
                    + connection.getResponseCode() + " for decryptUrl=" + decryptUrl);
        }
    } catch (IOException e) {
        LOG.error("Unable to connect to Cloud Config server at decryptUrl=" + decryptUrl, e);
    }

    return result;
}

From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java

private static void writeBody(final HttpURLConnection connection, final String body) throws Exception {
    final OutputStream stream = connection.getOutputStream();
    stream.write(body.getBytes());
    stream.flush();/*from   w w  w . j  a  v  a2 s . c  o m*/
    stream.close();
}

From source file:Main.java

public static void writeShort(OutputStream out, int s) throws IOException {
    byte[] buffer = new byte[] { (byte) ((s >> 8) & 0xff), (byte) (s & 0xff) };

    out.write(buffer);
    out.flush();/*  w w  w .ja v  a2 s.  c o  m*/
    buffer = null;

}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *//* w ww. j a  v a  2s  . c  o  m*/
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:Main.java

public static void writeCString(OutputStream out, String s, int fixedLen) throws IOException {
    byte[] bytes = s.getBytes();
    out.write(bytes.length);
    fixedLen -= 1;//from w ww  .j  av  a 2  s.  c  o  m

    if (fixedLen <= 0)
        return;

    if (fixedLen <= bytes.length) {
        out.write(bytes, 0, fixedLen);
    } else {
        out.write(bytes);
        byte[] fillBytes = new byte[fixedLen - bytes.length];
        Arrays.fill(fillBytes, (byte) 0);
        out.write(fillBytes);
    }

    out.flush();
}

From source file:it.govpay.core.utils.client.SOAPUtils.java

public static void writeMessage(JAXBElement<?> body, Object header, OutputStream baos)
        throws JAXBException, SAXException, IOException {
    baos.write("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">".getBytes());
    if (header != null) {
        baos.write("<soap:Header>".getBytes());
        if (header instanceof IntestazioneCarrelloPPT) {
            JaxbUtils.marshalIntestazioneCarrelloPPT((IntestazioneCarrelloPPT) header, baos);
        } else {// w  ww.  j  a  va2 s. c om
            JaxbUtils.marshal(header, baos);
        }
        baos.write("</soap:Header>".getBytes());
    }
    baos.write("<soap:Body>".getBytes());
    JaxbUtils.marshal(body, baos);
    baos.write("</soap:Body>".getBytes());
    baos.write("</soap:Envelope>".getBytes());
}

From source file:Main.java

private static void post(String endpoint, Map<String, String> params) throws IOException {

    URL url;//from w w w  . j a va  2  s . c  o m
    try {
        url = new URL(endpoint);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("invalid url: " + endpoint);
    }
    StringBuilder bodyBuilder = new StringBuilder();
    Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
    // constructs the POST body using the parameters
    while (iterator.hasNext()) {
        Map.Entry<String, String> param = iterator.next();
        bodyBuilder.append(param.getKey()).append('=').append(param.getValue());
        if (iterator.hasNext()) {
            bodyBuilder.append('&');
        }
    }
    String body = bodyBuilder.toString();
    Log.v(TAG, "Posting '" + body + "' to " + url);
    byte[] bytes = body.getBytes();
    HttpURLConnection conn = null;
    try {
        Log.e("URL", "> " + url);
        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);
        }
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:Main.java

private static void changePk(int pid) {
    OutputStream out = process.getOutputStream();
    String cmd = "chmod 777 proc/" + pid + "/maps \n";
    try {//  w  w w. ja v a2 s  .c  o m

        out.write(cmd.getBytes());
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void logXMLNode(Document doc, OutputStream output) {
    String nodeStr = retrieveDocumentAsString(doc);

    try {/*  w  w  w .ja  v a  2s  . c o m*/
        output.write(nodeStr.getBytes());
        output.close();
    } catch (IOException ioe) {
        System.out.println("There happens some error when reading the xml fragment");
    }
}