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 Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    OutputStream outStream = null;
    Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = Bitmap.Config.ARGB_8888;
    }/*from   w  w w  .  j a v  a  2 s.com*/
    String dataPath = Environment.getExternalStorageDirectory().toString() + "/SignChat/Temp/temp" + "0"
            + pictureNum + ".jpg";

    try {

        FileOutputStream out = new FileOutputStream(dataPath);

        // NEWLY ADDED CODE STARTS HERE [
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE); // Text Color
        paint.setStrokeWidth(12); // Text Size
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text
        // Overlapping
        // Pattern
        // some more settings...

        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawText("Testing...", 10, 10, paint);
        // NEWLY ADDED CODE ENDS HERE ]

        bitmap.compress(CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:uploadProcess.java

public static boolean decrypt(File inputFolder, String fileName, String patientID) {
    try {/*from w  w w . j  ava 2 s. c  om*/
        //Download File from Cloud..
        DropboxUpload download = new DropboxUpload();
        download.downloadFile(fileName, StoragePath.getDropboxDir() + patientID, inputFolder);

        String ukey = GetKey.getPatientKey(patientID);
        File inputFile = new File(inputFolder.getAbsolutePath() + File.separator + fileName);
        FileInputStream fis = new FileInputStream(inputFile);
        File outputFolder = new File(inputFolder.getAbsolutePath() + File.separator + "temp");
        if (!outputFolder.exists()) {
            outputFolder.mkdir();
        }
        FileOutputStream fos = new FileOutputStream(outputFolder.getAbsolutePath() + File.separator + fileName);
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.DECRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();
        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:com.jaspersoft.studio.server.protocol.soap.SoapConnection.java

public static File writeToTemp(byte[] b64data) throws IOException {
    File inputFile = FileUtils.createTempFile("save", "jrxml");
    inputFile.deleteOnExit();//w ww  .  j  a  va  2s  . c om
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(inputFile);
        out.write(Base64.decodeBase64(b64data));
        out.flush();
    } finally {
        FileUtils.closeStream(out);
    }
    return inputFile;
}

From source file:uploadProcess.java

public static boolean encrypt(File inputFolder, File outputFolder, String fileName, String patientID) {
    try {//from  w  w  w  .j a v  a 2s.  co  m

        String ukey = GetKey.getPatientKey(patientID);
        File filePath = new File(inputFolder.getAbsolutePath() + File.separator + fileName);
        FileInputStream fis = new FileInputStream(filePath);
        File outputFile = new File(outputFolder.getAbsolutePath() + File.separator + fileName);
        FileOutputStream fos = new FileOutputStream(outputFile);
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        //            System.out.println(key);
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();

        //Upload File to cloud
        DropboxUpload upload = new DropboxUpload();
        upload.uploadFile(outputFolder, fileName, StoragePath.getDropboxDir() + patientID);
        DeleteDirectory.delete(outputFolder);
        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:com.zacwolf.commons.crypto.Crypter_Blowfish.java

/**
 * /*w w  w  .  j  a va2 s.  c  o m*/
 * Create a new key with a custom keysize less-than equal to mykeysizemax,
 * specifying a custom salter
 * 
 * @param keyfile
 * @param keysize
 * @param salter
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws InvalidKeySpecException
 */
public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter)
        throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    if (keysize > Cipher.getMaxAllowedKeyLength(mytype))
        throw new InvalidKeySpecException(
                "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of "
                        + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype);

    final FileOutputStream fos = new FileOutputStream(keyfile);
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(mytype);
        kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available
        final SecretKey sk = kgen.generateKey();
        fos.write(sk.getEncoded());
    } finally {
        if (fos != null) {
            fos.flush();
            fos.close();
        }
    }
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*from   www .  java2 s  . c om*/
 *
 * @param filePath
 *            ?
 * @param fileContent
 *            ?
 * @return ?truefalse
 */
public static Boolean writeFileByByte(String filePath, byte[] fileContent) {
    try {
        if (null == filePath || null == fileContent) {
            return false;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        fileOutputStream.write(fileContent);
        fileOutputStream.flush();
        fileOutputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
        XLog.d(CLASS_NAME, e.getMessage());
        return false;
    }
    return true;
}

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Decompress./*from   w w  w.ja  v  a 2 s.  co m*/
 *
 * @param prefix
 *            the prefix
 * @param inputFile
 *            the input file
 * @param tempFile
 *            the temp file
 * @return the file
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static File decompress(final String prefix, final File inputFile, final File tempFile)
        throws IOException {
    final File tmpDestDir = createTodayPrefixedDirectory(prefix, new File(tempFile.getParent()));

    ZipFile zipFile = new ZipFile(inputFile);

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);

        if (entry.isDirectory()) {
            // Assume directories are stored parents first then
            // children.
            (new File(tmpDestDir, entry.getName())).mkdir();

            continue;
        }

        File newFile = new File(tmpDestDir, entry.getName());

        FileOutputStream fos = new FileOutputStream(newFile);
        try {
            byte[] buf = new byte[1024];
            int len;

            while ((len = stream.read(buf)) >= 0) {
                saveCompressedStream(buf, fos, len);
            }

        } catch (IOException e) {
            zipFile.close();

            IOException ioe = new IOException("Not valid ZIP archive file type.");
            ioe.initCause(e);
            throw ioe;
        } finally {
            fos.flush();
            fos.close();

            stream.close();
        }
    }
    zipFile.close();

    if ((tmpDestDir.listFiles().length == 1) && (tmpDestDir.listFiles()[0].isDirectory())) {
        return getShpFile(tmpDestDir.listFiles()[0]);
    }

    // File[] files = tmpDestDir.listFiles(new FilenameFilter() {
    //
    // public boolean accept(File dir, String name) {
    // return FilenameUtils.getExtension(name).equalsIgnoreCase("shp");
    // }
    // });
    //
    // return files.length > 0 ? files[0] : null;

    return getShpFile(tmpDestDir);
}

From source file:Main.java

public static String mergeFlockedFiles(String FilePath) {

    String result = null;/*  w  w  w .  java2 s . c  o m*/
    try {
        result = java.net.URLDecoder.decode(FilePath, "UTF-8");
    } catch (UnsupportedEncodingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    String fileName = result.substring(result.lastIndexOf('/') + 1);
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        long leninfile = 0, leng = 0;
        int count = 1, data = 0;
        int bytesRead = 0;
        try {
            File filename = new File(flockedFilesFolder.toString() + "/" + fileName);
            if (filename.exists())
                filename.delete();
            //BUFFER_SIZE = (int) filename.length();
            System.out.println("filename: " + filename);
            FileOutputStream fos = new FileOutputStream(filename, true);

            while (true) {
                File filepart = new File(filename + ".part" + count);
                System.out.println("part filename: " + filepart);
                if (filepart.exists()) {
                    FileInputStream fis = new FileInputStream(filepart);
                    byte fileBytes[] = new byte[(int) filepart.length()];
                    bytesRead = fis.read(fileBytes, 0, (int) filepart.length());
                    assert (bytesRead == fileBytes.length);
                    assert (bytesRead == (int) filepart.length());
                    fos.write(fileBytes);
                    fos.flush();
                    fileBytes = null;
                    fis.close();
                    fis = null;
                    count++;
                    filepart.delete();
                } else
                    break;
            }
            fos.close();
            fos = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return fileName;
}

From source file:com.mingsoft.weixin.util.UploadDownUtils.java

/**
 * ? //from  w w w  . j  a v a2s  . co m
 * 
 * @return
 */
@Deprecated
public static String downMedia(String access_token, String msgType, String media_id, String path) {
    String localFile = null;
    //      SimpleDateFormat df = new SimpleDateFormat("/yyyyMM/");
    try {
        String url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + access_token
                + "&media_id=" + media_id;
        // log.error(path);
        // ? ?
        URL urlObj = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        String xx = conn.getHeaderField("Content-disposition");
        try {
            log.debug("===? +==?==" + xx);
            if (xx == null) {
                InputStream in = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
                String line = null;
                String result = null;
                while ((line = reader.readLine()) != null) {
                    if (result == null) {
                        result = line;
                    } else {
                        result += line;
                    }
                }
                System.out.println(result);
                JSONObject dataJson = JSONObject.parseObject(result);
                return dataJson.getString("errcode");
            }
        } catch (Exception e) {
        }
        if (conn.getResponseCode() == 200) {
            String Content_disposition = conn.getHeaderField("Content-disposition");
            InputStream inputStream = conn.getInputStream();
            // // ?
            // Long fileSize = conn.getContentLengthLong();
            //  +
            String savePath = path + "/" + msgType;
            // ??
            String fileName = StringUtil.getDateSimpleStr()
                    + Content_disposition.substring(Content_disposition.lastIndexOf(".")).replace("\"", "");
            // 
            File saveDirFile = new File(savePath);
            if (!saveDirFile.exists()) {
                saveDirFile.mkdirs();
            }
            // ??
            if (!saveDirFile.canWrite()) {
                log.error("??");
                throw new Exception();
            }
            // System.out.println("------------------------------------------------");
            // ?
            File file = new File(saveDirFile + "/" + fileName);
            FileOutputStream outStream = new FileOutputStream(file);
            int len = -1;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                outStream.write(b, 0, len);
            }
            outStream.flush();
            outStream.close();
            inputStream.close();
            // ?
            localFile = fileName;
        }
    } catch (Exception e) {
        log.error("? !", e);
    } finally {

    }
    return localFile;
}

From source file:net.duckling.ddl.util.FileUtil.java

public static void copyFolder(String oldPath, String newPath) {

    try {/*from w w  w.  ja v  a  2  s  . c om*/
        (new File(newPath)).mkdirs();
        File a = new File(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()) {
                copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        LOG.error(e);
    }

}