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:bluevia.SendSMS.java

public static void sendBlueViaSMS(String user_email, String phone_number, String sms_message) {
    try {//  w  w w. jav  a  2  s. c om

        Properties blueviaAccount = Util.getNetworkAccount(user_email, "BlueViaAccount");
        if (blueviaAccount != null) {
            String consumer_key = blueviaAccount.getProperty("BlueViaAccount.consumer_key");
            String consumer_secret = blueviaAccount.getProperty("BlueViaAccount.consumer_secret");
            String access_key = blueviaAccount.getProperty("BlueViaAccount.access_key");
            String access_secret = blueviaAccount.getProperty("BlueViaAccount.access_secret");

            com.google.appengine.api.urlfetch.FetchOptions.Builder.doNotValidateCertificate();

            OAuthConsumer consumer = (OAuthConsumer) new DefaultOAuthConsumer(consumer_key, consumer_secret);
            consumer.setMessageSigner(new HmacSha1MessageSigner());
            consumer.setTokenWithSecret(access_key, access_secret);

            URL apiURI = new URL("https://api.bluevia.com/services/REST/SMS/outbound/requests?version=v1");
            HttpURLConnection request = (HttpURLConnection) apiURI.openConnection();

            request.setRequestProperty("Content-Type", "application/json");
            request.setRequestMethod("POST");
            request.setDoOutput(true);

            consumer.sign(request);
            request.connect();

            String smsTemplate = "{\"smsText\": {\n  \"address\": {\"phoneNumber\": \"%s\"},\n  \"message\": \"%s\",\n  \"originAddress\": {\"alias\": \"%s\"},\n}}";
            String smsMsg = String.format(smsTemplate, phone_number, sms_message, access_key);

            OutputStream os = request.getOutputStream();
            os.write(smsMsg.getBytes());
            os.flush();

            int rc = request.getResponseCode();

            if (rc == HttpURLConnection.HTTP_CREATED)
                log.info(String.format("SMS sent to %s. Text: %s", phone_number, sms_message));
            else
                log.severe(String.format("Error %d sending SMS:%s\n", rc, request.getResponseMessage()));
        } else
            log.warning("BlueVia Account seems to be not configured!");

    } catch (Exception e) {
        log.severe(String.format("Exception sending SMS: %s", e.getMessage()));
    }
}

From source file:com.intera.roostrap.util.EncryptionUtil.java

private static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);//from  ww  w  .  j  av a  2 s.c o  m
    }
    os.flush();
    os.close();
    is.close();
}

From source file:Main.java

public static void copyAssets(Context pContext, String pAssetFilePath, String pDestDirPath) {
    AssetManager assetManager = pContext.getAssets();
    InputStream in = null;//from  www  .j ava 2s . co m
    OutputStream out = null;
    try {
        in = assetManager.open(pAssetFilePath);
        File outFile = new File(pDestDirPath, pAssetFilePath);
        out = new FileOutputStream(outFile);
        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (IOException e) {
        Log.e("tag", "Failed to copy asset file: " + pAssetFilePath, e);
    }
}

From source file:bluevia.SendSMS.java

public static void setFacebookWallPost(String userEmail, String post) {
    try {//from   www .  j a  v  a2  s . c  o  m

        Properties facebookAccount = Util.getNetworkAccount(userEmail, Util.FaceBookOAuth.networkID);

        if (facebookAccount != null) {
            String access_key = facebookAccount.getProperty(Util.FaceBookOAuth.networkID + ".access_key");

            com.google.appengine.api.urlfetch.FetchOptions.Builder.doNotValidateCertificate();

            Entity blueviaUser = Util.getUser(userEmail);

            //FIXME
            URL fbAPI = new URL(
                    "https://graph.facebook.com/" + (String) blueviaUser.getProperty("alias") + "/feed");
            HttpURLConnection request = (HttpURLConnection) fbAPI.openConnection();

            String content = String.format("access_token=%s&message=%s", access_key,
                    URLEncoder.encode(post, "UTF-8"));

            request.setRequestMethod("POST");
            request.setRequestProperty("Content-Type", "javascript/text");
            request.setRequestProperty("Content-Length", "" + Integer.toString(content.getBytes().length));
            request.setDoOutput(true);
            request.setDoInput(true);

            OutputStream os = request.getOutputStream();
            os.write(content.getBytes());
            os.flush();
            os.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            int rc = request.getResponseCode();
            if (rc == HttpURLConnection.HTTP_OK) {
                StringBuffer body = new StringBuffer();
                String line;

                do {
                    line = br.readLine();
                    if (line != null)
                        body.append(line);
                } while (line != null);

                JSONObject id = new JSONObject(body.toString());
            } else
                log.severe(
                        String.format("Error %d posting FaceBook wall:%s\n", rc, request.getResponseMessage()));
        }
    } catch (Exception e) {
        log.severe(String.format("Exception posting FaceBook wall: %s", e.getMessage()));
    }
}

From source file:in.goahead.apps.util.URLUtils.java

public static void AppendStream(InputStream inputStream, String outputFileName, long skip) throws IOException {
    long actualSkip = 0;
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    actualSkip = inputStream.skip(skip);
    Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    if (actualSkip == skip) {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
        OutputStream os = new FileOutputStream(outputFileName, true);
        DownloadStream(inputStream, os);
        os.flush();
        os.close();//from  ww w  .  j  av a 2s . c o m
    } else {
        Logger.debug("Skip length: " + skip + " Actual skip: " + actualSkip);
    }

}

From source file:de.qaware.chronix.converter.common.Compression.java

/**
 * Compresses the given byte[]/*from w  w  w. j  a v a  2s . c  om*/
 *
 * @param decompressed - the byte[] to compress
 * @return the byte[] compressed
 */
public static byte[] compress(byte[] decompressed) {
    if (decompressed == null) {
        return new byte[] {};
    }
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(decompressed.length);
    OutputStream gzipOutputStream = null;
    try {
        gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(decompressed);
        gzipOutputStream.flush();
        byteArrayOutputStream.flush();
    } catch (IOException e) {
        LOGGER.error("Exception occurred while compressing gzip stream.", e);
        return null;
    } finally {
        IOUtils.closeQuietly(gzipOutputStream);
        IOUtils.closeQuietly(byteArrayOutputStream);
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:com.opengamma.web.analytics.json.Compressor.java

static void decompressStream(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    @SuppressWarnings("resource")
    InputStream iStream = new GZIPInputStream(
            new Base64InputStream(new BufferedInputStream(inputStream), false, -1, null));
    OutputStream oStream = new BufferedOutputStream(outputStream);
    byte[] buffer = new byte[2048];
    int bytesRead;
    while ((bytesRead = iStream.read(buffer)) != -1) {
        oStream.write(buffer, 0, bytesRead);
    }//  w  w w  .  j  a  v a 2  s  .  com
    oStream.flush();
}

From source file:Main.java

/**
 * Copies an input stream to an output stream. When the reading is done, the
 * input stream is closed.//from   w w w  .  ja  v  a2  s. com
 * 
 * @param inputStream
 *            The input stream.
 * @param outputStream
 *            The output stream.
 * @throws IOException
 */
public static void copy(InputStream inputStream, java.io.OutputStream outputStream) throws IOException {
    int bytesRead;
    byte[] buffer = new byte[2048];

    while ((bytesRead = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, bytesRead);
    }

    outputStream.flush();
    inputStream.close();
}

From source file:Main.java

private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[4096];

    try {/*from  w w w  .  j  a v  a 2 s  . c  o m*/
        while (true) {
            int bytes = in.read(buffer);
            if (bytes == -1)
                break;

            out.write(buffer, 0, bytes);
        }

        out.flush();
    } finally {
        try {
            in.close();
        } finally {
            out.close();
        }
    }
}

From source file:Main.java

public static void CopyStream(InputStream is, OutputStream os) {
    final int buffer_size = 1024;
    try {// w ww. j a v  a2 s . c  o  m
        byte[] bytes = new byte[buffer_size];
        for (;;) {
            int count = is.read(bytes, 0, buffer_size);
            if (count == -1)
                break;
            os.write(bytes, 0, count);
        }
        os.flush();
    } catch (Exception ex) {
    }
}