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:no.digipost.android.api.ApiAccess.java

public static void uploadFile(Context context, String uri, File file) throws DigipostClientException {
    try {//from  w  w  w . ja v  a 2  s  .c om
        try {

            FileBody filebody = new FileBody(file, ApiConstants.CONTENT_OCTET_STREAM);
            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null,
                    Charset.forName(ApiConstants.ENCODING));
            multipartEntity.addPart("subject", new StringBody(FilenameUtils.removeExtension(file.getName()),
                    ApiConstants.MIME, Charset.forName(ApiConstants.ENCODING)));
            multipartEntity.addPart("file", filebody);
            multipartEntity.addPart("token", new StringBody(TokenStore.getAccess()));

            URL url = new URL(uri);
            HttpsURLConnection httpsClient = (HttpsURLConnection) url.openConnection();
            httpsClient.setRequestMethod("POST");
            httpsClient.setDoOutput(true);
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                httpsClient.setFixedLengthStreamingMode(multipartEntity.getContentLength());
            } else {
                httpsClient.setChunkedStreamingMode(0);
            }
            httpsClient.setRequestProperty("Connection", "Keep-Alive");
            httpsClient.addRequestProperty("Content-length", multipartEntity.getContentLength() + "");
            httpsClient.setRequestProperty(ApiConstants.AUTHORIZATION,
                    ApiConstants.BEARER + TokenStore.getAccess());
            httpsClient.addRequestProperty(multipartEntity.getContentType().getName(),
                    multipartEntity.getContentType().getValue());

            try {
                OutputStream outputStream = new BufferedOutputStream(httpsClient.getOutputStream());
                multipartEntity.writeTo(outputStream);
                outputStream.flush();
                NetworkUtilities.checkHttpStatusCode(context, httpsClient.getResponseCode());
            } finally {
                httpsClient.disconnect();
            }

        } catch (DigipostInvalidTokenException e) {
            OAuth.updateAccessTokenWithRefreshToken(context);
            uploadFile(context, uri, file);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, context.getString(R.string.error_your_network));
        throw new DigipostClientException(context.getString(R.string.error_your_network));
    }
}

From source file:Main.java

public static void saveInstance(OutputStream outputStream, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, outputStream);

    outputStream.flush();

}

From source file:org.cloudfoundry.caldecott.client.TunnelHelper.java

private static File copyCaldecottZipFile(ClassPathResource cpr) throws IOException {
    File temp = File.createTempFile("caldecott", "zip");
    InputStream in = cpr.getInputStream();
    OutputStream out = new FileOutputStream(temp);
    int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = in.read(bytes)) != -1) {
        out.write(bytes, 0, read);/*from w  w  w. j a  va  2  s .  co m*/
    }
    in.close();
    out.flush();
    out.close();
    return temp;
}

From source file:Main.java

public static File savebitmap(Bitmap bitmap) {
    OutputStream outStream = null;
    File folder = new File(Environment.getExternalStorageDirectory().toString() + "/InSyte");
    folder.mkdirs();/*w ww.j  a v  a 2s. c  om*/
    String mDirectory = folder.toString();
    File file = new File(mDirectory, "IMG_" + System.currentTimeMillis() + ".jpg");
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return file;

}

From source file:br.msf.maven.compressor.CompressorService.java

private static void writeContents(final CharSequence content, final File f, final Charset encoding)
        throws Exception {
    if (f == null) {
        return;//  www. ja v a 2s . c om
    }
    final byte[] bytes = content.toString().getBytes(encoding);
    OutputStream os = null;
    try {
        if (!f.exists()) {
            f.getParentFile().mkdirs(); // create path directories
            f.createNewFile(); // create empty file
        }
        os = new FileOutputStream(f);
        os.write(bytes);
        os.flush();
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:com.yahoo.pulsar.zookeeper.LocalBookkeeperEnsemble.java

public static boolean waitForServerUp(String hp, long timeout) {
    long start = MathUtils.now();
    String split[] = hp.split(":");
    String host = split[0];/*from   www  .j  a  v a  2s .c  o m*/
    int port = Integer.parseInt(split[1]);
    while (true) {
        try {
            Socket sock = new Socket(host, port);
            BufferedReader reader = null;
            try {
                OutputStream outstream = sock.getOutputStream();
                outstream.write("stat".getBytes());
                outstream.flush();

                reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                String line = reader.readLine();
                if (line != null && line.startsWith("Zookeeper version:")) {
                    LOG.info("Server UP");
                    return true;
                }
            } finally {
                sock.close();
                if (reader != null) {
                    reader.close();
                }
            }
        } catch (IOException e) {
            // ignore as this is expected
            LOG.info("server " + hp + " not up " + e);
        }

        if (MathUtils.now() > start + timeout) {
            break;
        }
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            // ignore
        }
    }
    return false;
}

From source file:com.gson.util.HttpKit.java

/**
 * @description /*from   ww w  . j  a v  a 2 s. c  om*/
 * ??: POST 
 * @return       : 
 * @throws IOException 
 * @throws NoSuchProviderException 
 * @throws NoSuchAlgorithmException 
 * @throws KeyManagementException 
 */
public static String post(String url, String params)
        throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
    HttpURLConnection http = null;
    if (isHttps(url)) {
        http = initHttps(url, _POST, null);
    } else {
        http = initHttp(url, _POST, null);
    }
    OutputStream out = http.getOutputStream();
    out.write(params.getBytes(DEFAULT_CHARSET));
    out.flush();
    out.close();

    InputStream in = http.getInputStream();
    BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
    String valueString = null;
    StringBuffer bufferRes = new StringBuffer();
    while ((valueString = read.readLine()) != null) {
        bufferRes.append(valueString);
    }
    in.close();
    if (http != null) {
        http.disconnect();// 
    }
    return bufferRes.toString();
}

From source file:de.matzefratze123.staffinformer.util.IOUtil.java

public static void copy(InputStream stream, File output) throws IOException {
    Validate.notNull(stream, "InputStream cannot be null");
    Validate.notNull(output, "Output file cannot be null");

    final int BUFFER_SIZE = 1024;

    OutputStream outStream = null;

    try {//w w w.j  a v  a2 s .c  om
        if (!output.exists()) {
            output.createNewFile();
        }

        outStream = new FileOutputStream(output);

        byte[] buffer = new byte[BUFFER_SIZE];
        int read;

        while ((read = stream.read(buffer)) > 0) {
            outStream.write(buffer, 0, read);
        }

        outStream.flush();
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        } catch (Exception e) {
        }
    }
}

From source file:com.xebialabs.overthere.cifs.telnet.CifsTelnetConnection.java

private static void send(final OutputStream stdin, final String lineToSend) throws IOException {
    byte[] bytesToSend = (lineToSend + "\r\n").getBytes();
    stdin.write(bytesToSend);//from   w  w w .j a  v a 2 s .c  o  m
    stdin.flush();
}

From source file:de.iteratec.svg.SvgGraphicWriter.java

/**
 * Writes this document to the given output stream.
 * //from  w ww  .  j a  v a 2s. c  o  m
 * @param graphicsData
 *          Byte array with the diagram data
 * @param out
 *          The OutputStream to write to.
 * @throws IOException
 * @throws SvgExportException
 *           Iff the write operation was unsuccessful.
 */
public static void writeToSvg(byte[] graphicsData, OutputStream out) throws SvgExportException {
    try {
        out.write(graphicsData);
        out.flush();
    } catch (IOException e) {
        LOGGER.error("I/O Error: Writing to output stream failed.");
        throw new SvgExportException("Unable to write graphics to output stream.", e);
    }
}