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 copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[8192];
    int read;//from  w w  w. j av a2s.co  m
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
    out.flush();
}

From source file:com.microsoft.azure.hdinsight.sdk.storage.adls.WebHDFSUtils.java

public static void uploadFileToADLS(@NotNull IHDIStorageAccount storageAccount, @NotNull File localFile,
        @NotNull String remotePath, boolean overWrite) throws Exception {
    com.microsoft.azuretools.sdkmanage.AzureManager manager = AuthMethodManager.getInstance().getAzureManager();
    String tid = manager.getSubscriptionManager().getSubscriptionTenant(storageAccount.getSubscriptionId());
    String accessToken = manager.getAccessToken(tid);

    // TODO: accountFQDN should work for Mooncake
    String storageName = storageAccount.getName();
    ADLStoreClient client = ADLStoreClient.createClient(String.format("%s.azuredatalakestore.net", storageName),
            accessToken);//from   w  w w. ja v a 2s  . c o m
    OutputStream stream = null;
    try {
        stream = client.createFile(remotePath, IfExists.OVERWRITE);
        IOUtils.copy(new FileInputStream(localFile), stream);
        stream.flush();
        stream.close();
    } catch (ADLException e) {
        // ADLS operation may get a 403 when 'user' didn't have access to ADLS under Service Principle model
        // currently we didn't have a good way to solve this problem
        // we just popup the exception message for customers to guide customers login under interactive model
        if (e.httpResponseCode == 403
                || HttpStatusCode.valueOf(e.httpResponseMessage) == HttpStatusCode.FORBIDDEN) {
            throw new HDIException(
                    "Forbidden. Attached Azure DataLake Store is not supported in Automated login model. Please logout first and try Interactive login model",
                    403);
        }
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:Main.java

public static void takeScreenshot(String where, String name, View v) {

    // image naming and path to include sd card appending name you choose
    // for file/*from  w w w.j a  v  a  2 s.c  o m*/
    String mPath = where + "/" + name;

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = v.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fout);
        fout.flush();
        fout.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:de.nava.informa.utils.FileUtils.java

/**
 * Copies a file from <code>inFile</code> to <code>outFile</code>.
 *//*from  ww  w.  j  a  v a2  s.c om*/
public static void copyFile(File inFile, File outFile) {
    try {
        logger.debug("Copying file " + inFile + " to " + outFile);
        InputStream in = new FileInputStream(inFile);
        OutputStream out = new FileOutputStream(outFile);
        byte[] buf = new byte[8 * 1024];
        int n;
        while ((n = in.read(buf)) >= 0) {
            out.write(buf, 0, n);
            out.flush();
        }
        in.close();
        out.close();
    } catch (Exception e) {
        logger.warn("Error occurred while copying file " + inFile + " to " + outFile);
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setupDatabase(Context context, String db_name) {
    ContextWrapper cw = new ContextWrapper(context);
    String db_path = cw.getDatabasePath(db_name).getPath();

    try {//from w  w w.  j  a v a2 s. c  om
        // Setup
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(db_path);

        // Write all the things.
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        // Cleanup
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        // You done goofed.
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Export the given bitmap to the specified {@code File}
 * @param context the context/*from  w  w w .j av  a  2 s  . c om*/
 * @param bitmap the bitmap to export
 * @param file the file in which to save the view
 */
public static void exportBitmapToFile(@NonNull final Context context, @NonNull final Bitmap bitmap,
        @NonNull final File file) {
    OutputStream outputStream = null;
    try {
        outputStream = new BufferedOutputStream(new FileOutputStream(file, false));
        bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY, outputStream);
        outputStream.flush();
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
            //No Worries
        }
    }
}

From source file:Main.java

/**
 * Copy configuration file from assets to data folder.
 *
 * @param file File to copy//from   w  ww. ja va 2  s  .com
 */
private static void copyAssetToData(File file) {
    try {
        InputStream myInput = mContext.getAssets().open(file.getName());
        String outFileName = file.getPath();
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.cloudbees.workflow.Util.java

public static int postToJenkins(Jenkins jenkins, String url, String content, String contentType)
        throws IOException {
    String jenkinsUrl = jenkins.getRootUrl();
    URL urlObj = new URL(jenkinsUrl + url.replace("/jenkins/job/", "job/"));
    HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();

    try {// w  w w  .j av a  2s  . c o m
        conn.setRequestMethod("POST");

        // Set the crumb header, otherwise the POST may be rejected.
        NameValuePair crumbHeader = getCrumbHeaderNVP(jenkins);
        conn.setRequestProperty(crumbHeader.getName(), crumbHeader.getValue());

        if (contentType != null) {
            conn.setRequestProperty("Content-Type", contentType);
        }
        if (content != null) {
            byte[] bytes = content.getBytes(Charset.forName("UTF-8"));

            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
            final OutputStream os = conn.getOutputStream();
            try {
                os.write(bytes);
                os.flush();
            } finally {
                os.close();
            }
        }

        return conn.getResponseCode();
    } finally {
        conn.disconnect();
    }
}

From source file:Main.java

public static void readFullyWriteToOutputStream(@NonNull InputStream in, @NonNull OutputStream out)
        throws IOException {
    try {/*from w ww.j a  v a2 s. c om*/
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        out.close();
    }
}

From source file:com.comcast.viper.flume2storm.zookeeper.ZkTestUtils.java

/**
 * Utility function to send a command to the internal server. ZK servers
 * accepts 4 byte command strings to test their liveness.
 */// w  w  w  .java  2  s .  c om
protected static String send4LetterWord(final String host, final int port, final String cmd) {
    Preconditions.checkArgument(cmd.length() == 4);
    try {
        final Socket sock = new Socket(host, port);
        OutputStream outstream = null;
        BufferedReader reader = null;
        try {
            outstream = sock.getOutputStream();
            outstream.write(cmd.getBytes());
            outstream.flush();

            reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            final StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            return sb.toString();
        } finally {
            if (outstream != null) {
                outstream.close();
            }
            sock.close();
            if (reader != null) {
                reader.close();
            }
        }
    } catch (final Exception e) {
        System.out.println(e.getMessage());
        return StringUtils.EMPTY;
    }
}