Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

In this page you can find the example usage for java.io BufferedOutputStream write.

Prototype

@Override
public synchronized void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

protected void processPicasaMedia(String path, String extension) throws Exception {
    if (Config.DEBUG) {
        Log.i(TAG, "Picasa Started");
    }/*  w w  w .  j av  a  2  s.co  m*/
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(path));

        filePath = FileUtils.getDirectory(foldername) + File.separator
                + Calendar.getInstance().getTimeInMillis() + extension;

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        inputStream.close();
        outStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (Config.DEBUG) {
        Log.i(TAG, "Picasa Done");
    }
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

protected void processPicasaMedia(String path, String extension) throws Exception {
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Picasa Started");
    }/*from  ww  w. ja v a2  s  . c  om*/
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(path));

        filePath = foldername + File.separator + Calendar.getInstance().getTimeInMillis() + extension;

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = inputStream.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        inputStream.close();
        outStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Picasa Done");
    }
}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

protected void processGooglePhotosMedia(String path, String extension) throws Exception {
    if (Config.DEBUG) {
        Log.i(TAG, "Google photos Started");
        Log.i(TAG, "URI: " + path);
        Log.i(TAG, "Extension: " + extension);
    }/*from w  w  w  . j  av  a2  s . c  o m*/
    String retrievedExtension = checkExtension(Uri.parse(path));
    if (retrievedExtension != null && !TextUtils.isEmpty(retrievedExtension)) {
        extension = "." + retrievedExtension;
    }
    try {

        filePath = FileUtils.getDirectory(foldername) + File.separator
                + Calendar.getInstance().getTimeInMillis() + extension;

        ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
                .openFileDescriptor(Uri.parse(path), "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        InputStream inputStream = new FileInputStream(fileDescriptor);

        BufferedInputStream reader = new BufferedInputStream(inputStream);

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = reader.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (Config.DEBUG) {
        Log.i(TAG, "Picasa Done");
    }
}

From source file:com.haru.ui.image.workers.MediaProcessorThread.java

protected void processGooglePhotosMedia(String path, String extension) throws Exception {
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Google photos Started");
        Log.i(TAG, "URI: " + path);
        Log.i(TAG, "Extension: " + extension);
    }//ww w.  j  a  va2s. c  o m
    String retrievedExtension = checkExtension(Uri.parse(path));
    if (retrievedExtension != null && !TextUtils.isEmpty(retrievedExtension)) {
        extension = "." + retrievedExtension;
    }
    try {

        filePath = foldername + File.separator + Calendar.getInstance().getTimeInMillis() + extension;

        ParcelFileDescriptor parcelFileDescriptor = context.getContentResolver()
                .openFileDescriptor(Uri.parse(path), "r");

        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();

        InputStream inputStream = new FileInputStream(fileDescriptor);

        BufferedInputStream reader = new BufferedInputStream(inputStream);

        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] buf = new byte[2048];
        int len;
        while ((len = reader.read(buf)) > 0) {
            outStream.write(buf, 0, len);
        }
        outStream.flush();
        outStream.close();
        inputStream.close();
        process();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    if (/* TODO: DEBUG */ true) {
        Log.i(TAG, "Picasa Done");
    }
}

From source file:com.informatica.um.binge.api.impl.PluginsFactory.java

/**
 * Download the plugin zip file from mond and save it locally.
 * @param url//w ww .j  av  a2  s.  c  om
 * @param fileName
 * @throws Exception
 */
private void downloadPluginZip(String url, String fileName) throws Exception {
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(getInputStream(url));
        out = new BufferedOutputStream(new FileOutputStream(new File(fileName)));
        byte[] buffer = new byte[8192];
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    } catch (Exception e) {
        BingeUtils.rethrowVDSException(e);
        throw new VDSException(VDSErrorCode.PLUGIN_DOWNLOAD_FAILED, e, url);
    } finally {
        BingeUtils.close(LOG, url, in);
        BingeUtils.close(LOG, fileName, out);
    }
}

From source file:com.symbian.driver.remoting.master.MasterRemoteImpl.java

/**
 * get package file from client remotely in multiple calls
 * @return//from www . j  a  va2s. co  m
 */
private boolean getRemoteFile(TestJob job) {
    //lookup client remote inteface
    String clientName = job.getRegistrationId();
    ClientRemote clientRemote = ClientRegister.getClient(clientName);

    String remoteFileName = job.getSourceFolder() + File.separator + job.getTestPackageName();
    String targetFile = job.getTagetFolder() + File.separator + job.getTestPackageName();

    //read file from remote in multiple calls to avoid OutOfMem if pass large file in memory at once. 
    BufferedOutputStream out = null;
    try {
        File testpackage = new File(targetFile);
        out = new BufferedOutputStream(new FileOutputStream(testpackage));
        byte[] data = null;
        long pos = 0;
        while ((data = clientRemote.readFile(remoteFileName, pos, TRANSFER_LENGTH)) != null) {

            out.write(data, 0, data.length);
            out.flush();
            pos += data.length;
        }
        LOGGER.log(Level.INFO, "received remote file:" + remoteFileName);
        LOGGER.log(Level.INFO, "  saved to :" + targetFile + " file size:" + testpackage.length());
        return true;
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Master: Unable to retrive remote package file " + job.getId() + ".", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ioe) {
            }
            out = null;
        }
    }

    return false;
}

From source file:com.viettel.dms.download.DownloadFile.java

/**
 * Copy from one stream to another.  Throws IOException in the event of error
 * (for example, SD card is full)/* www  . j  a  v a 2  s  .c  o  m*/
 * 
 * @param is         Input stream.
 * @param os         Output stream.
 * @param buffer      Temporary buffer to use for copy.
 * @param bufferSize   Size of temporary buffer, in bytes.
 */
public static void copyStream(BufferedInputStream is, BufferedOutputStream os, byte[] buffer, int bufferSize)
        throws IOException {
    int sizeDownloaded = 0;
    int percent = 0;
    try {
        for (;;) {
            int count = is.read(buffer, 0, bufferSize);
            if (count >= 0) {
                sizeDownloaded += count;
                percent = (int) ((float) (sizeDownloaded) / (float) (fileSize) * 100);
                VTLog.d("copyStream", "current count: " + count + " sizeDownloaded: " + sizeDownloaded
                        + " fileSize: " + fileSize + " percent: " + percent);
                if (percent > 98) {
                    percent = 98;
                }
                try {
                    Context ct = GlobalInfo.getInstance().getActivityContext();
                    if (ct != null && ct instanceof GlobalBaseActivity) {
                        ((GlobalBaseActivity) ct).updateProgressPercentDialog(percent);
                    }
                } catch (Exception e) {
                    VTLog.e("copyStream", "fail", e);
                }
            }
            if (count == -1) {
                break;
            }
            os.write(buffer, 0, count);
        }
        os.flush();
    } catch (IOException e) {
        throw e;
    }
}

From source file:it.readbeyond.minstrel.commander.Commander.java

private void copyFileFromAssetsWWW(String sourcePath, String destinationPath,
        final CallbackContext callbackContext) {
    String source = "www" + File.separator + sourcePath;
    String destination = this.normalizePath(destinationPath);
    try {// w  ww  .  j av a2s  . c  o m
        File d = new File(destination);

        // create parent directory, if not existing
        File destinationParent = d.getParentFile();
        destinationParent.mkdirs();

        // TODO check for write permission?
        // copy in chunks of 4 KB
        final int BUFFER = 4096;
        BufferedInputStream is = new BufferedInputStream(
                cordova.getActivity().getApplicationContext().getAssets().open(source));
        int numberOfBytesRead;
        byte data[] = new byte[BUFFER];
        FileOutputStream fos = new FileOutputStream(d);
        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
        while ((numberOfBytesRead = is.read(data, 0, BUFFER)) > -1) {
            dest.write(data, 0, numberOfBytesRead);
        }
        dest.flush();
        dest.close();
        is.close();
        fos.close();
        callbackContext.success(MESSAGE_FILE_COPIED);

    } catch (Exception e) {
        callbackContext.success(MESSAGE_ERROR_WHILE_COPYING);
    }
}

From source file:edu.ku.brc.specify.plugins.ipadexporter.iPadRepositoryHelper.java

/**
 * @param urlStr// w w w.  j av  a2s.c  o  m
 * @param tmpFile
 * @return
 */
public boolean fillFileFromWeb(final String urlStr, final File tmpFile) {
    networkConnError = false;
    try {
        URL url = new URL(urlStr);
        InputStream inpStream = url.openStream();
        if (inpStream != null) {
            BufferedInputStream in = new BufferedInputStream(inpStream);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile));

            do {
                int numBytes = in.read(bytes);
                if (numBytes == -1) {
                    break;
                }
                bos.write(bytes, 0, numBytes);

            } while (true);
            in.close();
            bos.close();

            return true;
        }

    } catch (java.net.UnknownHostException uex) {
        networkConnError = true;

    } catch (IOException ex) {
        ex.printStackTrace();

        /*int inx = urlStr.lastIndexOf('/');
        File inFile = new File("/Users/rods/workspace/SQLiteExample/"+urlStr.substring(inx));
        try
        {
        FileUtils.copyFile(inFile, tmpFile);
        } catch (IOException e)
        {
        e.printStackTrace();
        }*/
    }

    return false;
}

From source file:edu.stanford.epadd.launcher.Splash.java

public static void copy_stream_to_file(InputStream is, String filename) throws IOException {
    int bufsize = 64 * 1024;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {//from   ww  w  .ja v a 2 s.c o  m
        File f = new File(filename);
        if (f.exists()) {
            // out.println ("File " + filename + " exists");
            boolean b = f.delete(); // best effort to delete file if it exists. this is because windows often complains about perms
            if (!b)
                out.println("Warning: failed to delete " + filename);
        }
        bis = new BufferedInputStream(is, bufsize);
        bos = new BufferedOutputStream(new FileOutputStream(filename), bufsize);
        byte buf[] = new byte[bufsize];
        while (true) {
            int n = bis.read(buf);
            if (n <= 0)
                break;
            bos.write(buf, 0, n);
        }
    } catch (IOException ioe) {
        out.println("ERROR trying to copy data to file: " + filename + ", forging ahead nevertheless");
    } finally {
        if (bis != null)
            bis.close();
        if (bos != null)
            bos.close();
    }
}