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:com.ckfinder.connector.utils.ImageUtils.java

/**
 * writes unchanged file to disk./*from  w w  w.j av  a  2s. co  m*/
 *
 * @param stream - stream to read the file from
 *
 * @param destFile - file to write to
 *
 * @throws IOException when error occurs.
 */
private static void writeUntouchedImage(final InputStream stream, final File destFile) throws IOException {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    byte[] buffer = new byte[MAX_BUFF_SIZE];
    int readNum = -1;
    while ((readNum = stream.read(buffer)) != -1) {
        byteArrayOS.write(buffer, 0, readNum);
    }
    byte[] bytes = byteArrayOS.toByteArray();
    byteArrayOS.close();
    FileOutputStream fileOS = new FileOutputStream(destFile);
    fileOS.write(bytes);
    fileOS.flush();
    fileOS.close();
}

From source file:Main.java

public static void copyAsset(Context context, String assetFileName, String dstFile) {
    AssetManager assetManager = null;//from   www. ja v  a 2  s . c  om
    assetManager = context.getAssets();
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        inputStream = assetManager.open(assetFileName);
        fileOutputStream = new FileOutputStream(dstFile);
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        fileOutputStream.write(buffer);
        fileOutputStream.flush();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            inputStream.close();
            fileOutputStream.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * writes unchanged file to disk./*from www .  j ava  2s .  co  m*/
 *
 * @param stream - stream to read the file from
 *
 * @param destFile - file to write to
 *
 * @throws IOException when error occurs.
 */
private static void writeUntouchedImage(final InputStream stream, final File destFile) throws IOException {
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    byte[] buffer = new byte[MAX_BUFF_SIZE];
    int readNum;
    while ((readNum = stream.read(buffer)) != -1) {
        byteArrayOS.write(buffer, 0, readNum);
    }
    byte[] bytes = byteArrayOS.toByteArray();
    byteArrayOS.close();
    FileOutputStream fileOS = new FileOutputStream(destFile);
    fileOS.write(bytes);
    fileOS.flush();
    fileOS.close();
}

From source file:Main.java

public static void copyDirFile(String oldPath, String newPath) {
    try {//from w  w w  .  j  a v a  2 s.c  o m
        new File(newPath).mkdirs();
        File a = new File(oldPath);
        System.out.println("oldPath:" + oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyDirFile(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFromPackage(Context context, int ressourceId, String target) throws IOException {
    FileOutputStream lOutputStream = context.openFileOutput(target, 0);
    InputStream lInputStream = context.getResources().openRawResource(ressourceId);
    int readByte;
    byte[] buff = new byte[8048];
    while ((readByte = lInputStream.read(buff)) != -1) {
        lOutputStream.write(buff, 0, readByte);
    }/* w w w . j av a2 s  . co m*/
    lOutputStream.flush();
    lOutputStream.close();
    lInputStream.close();
}

From source file:org.openhab.io.caldav.internal.Util.java

public static void storeToDisk(String calendarId, String filename, Calendar calendar) {
    File cacheFile = getCacheFile(calendarId, filename);
    try {/*w w  w  .ja  v  a2s  .c o  m*/
        FileOutputStream fout = new FileOutputStream(cacheFile);
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.setValidating(false);
        outputter.output(calendar, fout);
        fout.flush();
        fout.close();
    } catch (IOException e) {
        log.error("cannot store event '{}' to disk (path={}): {}", filename, cacheFile.getAbsoluteFile(),
                e.getMessage());
    } catch (ValidationException e) {
        log.error("cannot store event '{}' to disk (path={}): {}", filename, cacheFile.getAbsoluteFile(),
                e.getMessage());
    }
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ?DSA??p,q,g,j,x,y/*from  w ww. ja v a2s  .c  om*/
 * 
 * @param seed
 *            ??
 * @throws Exception
 * @author huangbuji
 *         <p />
 *         Create at 2014-2-8 ?4:45:26
 */
@SuppressWarnings("restriction")
public static void genKey(String seed) throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    DSAPublicKeyImpl publicKey = (DSAPublicKeyImpl) keyPair.getPublic();
    DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
    DSAParams dsaParams = privateKey.getParams();
    Base64 base64 = new Base64();
    String p = new String(base64.encode(dsaParams.getP().toByteArray()), "utf8");
    String q = new String(base64.encode(dsaParams.getQ().toByteArray()), "utf8");
    String g = new String(base64.encode(dsaParams.getG().toByteArray()), "utf8");
    String x = new String(base64.encode(privateKey.getX().toByteArray()), "utf8");
    String y = new String(base64.encode(publicKey.getY().toByteArray()), "utf8");
    System.out.println("P: " + p);
    System.out.println("Q: " + q);
    System.out.println("G: " + g);
    System.out.println("X: " + x);
    System.out.println("Y: " + y);

    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    System.err.println("public: " + publicKeyString);
    System.err.println("private: " + privateKeyString);

    File publicFile = new File("D:/binPublic.ky");
    File privateFile = new File("D:/binPrivate.ky");
    FileOutputStream out = new FileOutputStream(publicFile);
    out.write(publicKey.getEncoded());
    out.flush();
    out.close();
    out = new FileOutputStream(privateFile);
    out.write(privateKey.getEncoded());
    out.flush();
    out.close();
}

From source file:Main.java

public static boolean putBitmapToFile(Bitmap bitmap, File file) {
    if (bitmap == null || file == null)
        return false;
    FileOutputStream fos = null;
    try {/*w w  w .ja v a2  s  .  c om*/
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.flush();
                fos.close();
                if (!bitmap.isRecycled())
                    bitmap.recycle();
            } catch (IOException ignored) {
            }
    }
    return false;
}

From source file:com.spstudio.common.image.ImageUtils.java

/**
 * ,?byte/*from  w w  w . j  a  v a2s .c  o m*/
 *
 * @return
 */
public static void writeFile(byte[] bao) {
    File file = new File("d:/touxiang1.jpg");

    FileOutputStream fos = null;
    ByteArrayOutputStream baos = null;
    byte[] data = null;

    try {
        fos = new FileOutputStream(file);
        fos.write(bao);

        fos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(ImageUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.snipme.download.ImageDownloader.java

public static String saveToSdCard(Bitmap bitmap, String filename) {

    String stored = null;/*from  ww  w .  jav a  2s  .com*/
    if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "SnipMe");

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        Log.i("DownloadImage", "Create Folder Snipme");
        if (!mediaStorageDir.mkdirs()) {
            Log.d("DownloadImage", "failed to create directory SnipMe");
            return null;
        }
    }
    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File file = new File(mediaStorageDir.getPath() + File.separator + filename + "_" + timeStamp + ".png");

    //File sdcard = Environment.getExternalStorageDirectory();

    //File folder = new File(sdcard.getAbsoluteFile(), "SnipMe");// the
    // dot
    // makes
    // this
    // directory
    // hidden
    // to
    // the
    // user
    //folder.mkdir();
    //File file = new File(folder.getAbsoluteFile(), filename + ".png");
    if (file.exists())
        return stored;

    try {
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();
        stored = "success";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return stored;
}