Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

In this page you can find the example usage for java.io FileOutputStream 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 boolean installDll(Context context) {
    boolean isSuccess = false;
    File file = context.getFileStreamPath("Disdll.dll");
    boolean isDeleteSuccess = true;
    if (file.exists()) {
        isDeleteSuccess = file.delete();
    }/*www .ja v  a2s  . c  om*/
    if (isDeleteSuccess) {
        try {
            FileOutputStream outputStream = context.openFileOutput("Disdll.dll", Context.MODE_PRIVATE);
            InputStream inputStream = context.getAssets().open("Disdll.dll");
            byte[] temp = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(temp)) != -1) {
                outputStream.write(temp, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        isSuccess = checkIfDllInstalled(context);
    }
    return isSuccess;
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.DownloadCsarManager.java

/**
 * Download from given URL to given file location.
 * @param url String/*ww w .  j a va  2  s  .c o  m*/
 * @param filepath String
 * @return
 */
public static String download(String url, String filepath) {
    String status = "";
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = client.execute(httpget);

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        if (filepath == null) {
            filepath = getFilePath(response); //NOSONAR
        }

        File file = new File(filepath);
        file.getParentFile().mkdirs();
        FileOutputStream fileout = new FileOutputStream(file);

        byte[] buffer = new byte[CACHE];
        int ch;
        while ((ch = is.read(buffer)) != -1) {
            fileout.write(buffer, 0, ch);
        }
        is.close();
        fileout.flush();
        fileout.close();
        status = Constant.DOWNLOADCSAR_SUCCESS;

    } catch (Exception e) {
        status = Constant.DOWNLOADCSAR_FAIL;
        LOG.error("Download csar file failed! " + e.getMessage(), e);
    }
    return status;
}

From source file:com.amazonaws.utilities.Util.java

/**
 * Copies the data from the passed in Uri, to a new file for use with the
 * Transfer Service/*from w  w w.ja v  a2  s .  c  o  m*/
 * 
 * @param context
 * @param uri
 * @return
 * @throws IOException
 */
public static File copyContentUriToFile(Context context, Uri uri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(uri);
    File copiedData = new File(context.getDir("SampleImagesDir", Context.MODE_PRIVATE),
            UUID.randomUUID().toString());
    copiedData.createNewFile();

    FileOutputStream fos = new FileOutputStream(copiedData);
    byte[] buf = new byte[2046];
    int read = -1;
    while ((read = is.read(buf)) != -1) {
        fos.write(buf, 0, read);
    }

    fos.flush();
    fos.close();

    return copiedData;
}

From source file:ZipHandler.java

/**
 * unzipps a zip file placed at <zipURL> to path <xmlURL>
 * @param zipURL/*  w w  w  .ja v  a  2  s . c o m*/
 * @param xmlURL
 */
public static void unZip(String zipURL, String xmlURL) throws IOException {
    FileOutputStream fosBLOB = new FileOutputStream(new File("c:\\"));
    FileOutputStream fosCLOB = new FileOutputStream(new File("c:\\"));
    FileInputStream fis = new FileInputStream(new File(zipURL));
    ZipInputStream zis = new ZipInputStream(fis);
    FileOutputStream fos = new FileOutputStream(xmlURL);
    ZipEntry ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    ze = zis.getNextEntry();
    ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
    fos.flush();
    fis.close();
    fos.close();
    fosCLOB.close();
    fosBLOB.close();
    zis.close();
}

From source file:Main.java

public static void writeFile(File file, byte[] content) throws IOException {
    if (!file.exists()) {
        try {//w w  w  .jav  a  2s . co m
            file.createNewFile();
        } catch (IOException e) {
            throw new IOException("not crete file=" + file.getAbsolutePath());
        }
    }
    FileOutputStream fileOutputStream = null;
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(content);
        fileOutputStream = new FileOutputStream(file, false);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        fileOutputStream.flush();
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

public static void takeScreenshot(String fileName, String directory, int quality, Instrumentation inst) {
    Method takeScreenshot;//from  www.ja  v a  2  s . c  om
    Method getUiAutomation;
    Object mUiAutomationVaule;
    Bitmap bitmap = null;
    if (android.os.Build.VERSION.SDK_INT < 18) {
        Log.e(TAG, "Build.VERSION is :" + android.os.Build.VERSION.SDK_INT + ", it should >= API 18");
        return;
    }
    try {
        getUiAutomation = Instrumentation.class.getDeclaredMethod("getUiAutomation");
        mUiAutomationVaule = getUiAutomation.invoke(inst, new Object[] {});
        takeScreenshot = mUiAutomationVaule.getClass().getDeclaredMethod("takeScreenshot", new Class[] {});
        if (mUiAutomationVaule != null)
            bitmap = (Bitmap) takeScreenshot.invoke(mUiAutomationVaule, new Object[] {});
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(directory + File.separator + fileName);
        if (fileName.endsWith(".png")) {
            bitmap.compress(Bitmap.CompressFormat.PNG, quality, fos);
        } else if (fileName.endsWith(".jpg")) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
        }
        fos.flush();
        fos.close();
    } catch (Exception e) {
        Log.e(TAG,
                "Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.");
        e.printStackTrace();
    }
}

From source file:Util.java

public static boolean retreiveBinaryFileFromJar(String resourceName, String targetDirectory, Object resource)
        throws Exception {
    boolean found = false;
    if (resourceName != null) {
        InputStream is = resource.getClass().getResourceAsStream(resourceName);
        if (is == null)
            throw new Exception("Resource " + resourceName + " was not found.");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        FileOutputStream fos = new FileOutputStream(targetDirectory + File.separator
                + resourceName.substring(resourceName.lastIndexOf('/'), resourceName.length()));
        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = is.read(buffer)) != -1) {
            fos.write(buffer, 0, bytesRead);
        }/* w  ww . j  a  v  a 2 s .co m*/
        fos.flush();
        br.close();
        is.close();
        found = true;
    } else {
        found = false;
    }
    return found;
}

From source file:com.edgenius.core.Installation.java

/**
 * This method only used in single request model such as installation, startup. Otherwise, please use
 * SettingService.saveInstallation() for thread-safe.
 * @param install//  w  ww.ja v  a 2  s .  c o  m
 */
public static void saveInstallation(Installation install) {
    FileOutputStream os = null;
    try {
        File installFile = FileUtil.getFile(DataRoot.getDataRoot() + installResource);
        os = new FileOutputStream(installFile);
        XStream xs = new XStream();
        xs.processAnnotations(Installation.class);
        xs.toXML(install, os);
        os.flush();
    } catch (Exception e) {
        log.error("Failed save or update installation file, this may cause install step redo.", e);
    } finally {
        if (os != null)
            try {
                os.close();
            } catch (IOException e) {
            }
    }

}

From source file:com.android.emailcommon.utility.AttachmentUtilities.java

private static long copyFile(InputStream in, File file) throws IOException {
    FileOutputStream out = new FileOutputStream(file);
    long size = IOUtils.copy(in, out);
    in.close();//  w  ww . j a  v  a 2  s  . c  o m
    out.flush();
    out.close();
    return size;
}

From source file:fr.ece.epp.tools.Utils.java

public static void copy(File resFile, File objFolderFile) throws IOException {
    if (!resFile.exists()) {
        return;//from ww  w.  j  a  va2  s.c o  m
    }
    if (!objFolderFile.exists()) {
        objFolderFile.mkdirs();
    }
    if (resFile.isFile()) {
        File objFile = new File(objFolderFile.getPath() + File.separator + resFile.getName());
        InputStream ins = new FileInputStream(resFile);
        FileOutputStream outs = new FileOutputStream(objFile);
        byte[] buffer = new byte[1024 * 512];
        int length;
        while ((length = ins.read(buffer)) != -1) {
            outs.write(buffer, 0, length);
        }
        ins.close();
        outs.flush();
        outs.close();
    } else {
        String objFolder = objFolderFile.getPath() + File.separator + resFile.getName();
        File _objFolderFile = new File(objFolder);
        _objFolderFile.mkdirs();
        for (File sf : resFile.listFiles()) {
            copy(sf, new File(objFolder));
        }
    }
}