Example usage for java.io OutputStream flush

List of usage examples for java.io OutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:Main.java

public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buf = new byte[1024];
    int len;//from  w ww.j  a v  a 2 s.c o m
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    out.flush();
    in.close();
    out.close();
}

From source file:Main.java

private static void killProcess(String packageName) {
    OutputStream out = process.getOutputStream();
    String cmd = "am force-stop " + packageName + " \n";

    try {/* ww  w  . j ava  2  s.  c  o  m*/

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

From source file:Main.java

public static void saveJpegFile(String fileName, Bitmap bitmap) {
    // file/*from w ww. j a  va2  s .c o m*/
    File file = new File(extStorageDirectory, fileName + ".jpg");
    File fileDirectory = new File(extStorageDirectory);
    if (!fileDirectory.exists()) {
        fileDirectory.mkdir();
    }
    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.appdynamics.common.RESTClient.java

public static void sendPost(String urlString, String input, String apiKey) {
    try {//from   w w  w .  j  a v  a2 s  .co 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: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();
            }/*from  www .j  a va2 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:Main.java

public static void writeExtractedFileToDisk(InputStream in, OutputStream outs) throws IOException {
    byte[] buffer = new byte[1024];
    int length;/*from w  w w.jav  a 2s. c  o  m*/
    while ((length = in.read(buffer)) > 0) {
        outs.write(buffer, 0, length);
    }
    outs.flush();
    outs.close();
    in.close();
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T//  www.j ava2 s .co m
 * @param object the object to be transformed into XML
 * @param file the output file
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, File file) throws JAXBException, IOException {
    OutputStream stream = new FileOutputStream(file);

    try {
        marshal(element, object, stream);

        stream.flush();
        stream.close();
    } catch (JAXBException e) {
        IOUtils.closeQuietly(stream);

        throw e;
    } catch (IOException e) {
        IOUtils.closeQuietly(stream);

        throw e;
    }
}

From source file:Main.java

private static void copyFile(File source, File destination) throws IOException {
    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destination);

    byte[] buffer = new byte[1024];
    int length;/*from w w  w . j a  v  a2 s.c  o m*/
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:blast.shell.karaf.ssh.BlastShellFactoryImpl.java

private static void flush(OutputStream... streams) {
    for (OutputStream s : streams) {
        try {/*from   w w w  .j  a  v  a2s  .  com*/
            s.flush();
        } catch (IOException e) {
            // Ignore
        }
    }
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, List<String> names) throws IOException {
    OutputStream stream = connection.getOutputStream();
    String body = JSONArray.toJSONString(names);
    stream.write(body.getBytes());//from  ww w  .jav  a 2  s.co  m
    stream.flush();
    stream.close();
}