Example usage for java.io FileOutputStream getChannel

List of usage examples for java.io FileOutputStream getChannel

Introduction

In this page you can find the example usage for java.io FileOutputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file output stream.

Usage

From source file:org.cloudbyexample.dc.util.file.DownloadUtil.java

/**
 * Download URL to specified dir using specified file name.
 */// w  ww . ja  v  a2 s  .  c om
public static File download(File dir, String url, String fileName) throws IOException {
    FileOutputStream fos = null;

    File file = new File(dir, fileName);

    try {
        URL website = new URL(url);

        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        fos = new FileOutputStream(file);
        // ex: '1 << 24' is 16MB for max download
        //            fos.getChannel().transferFrom(rbc, 0, 1 << 24');
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

        logger.debug("Downloaded '{}' from '{}'.", file, url);
    } finally {
        IOUtils.closeQuietly(fos);
    }

    return file;
}

From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA4679VFSPasswordSecurityTestCase.java

/**
 * Copy the given source file to the given destination
 *
 * @param sourceFile source file/*ww  w . ja v a2s .  c om*/
 * @param destFile   destination file
 * @throws java.io.IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream(sourceFile);
        fileOutputStream = new FileOutputStream(destFile);

        FileChannel source = fileInputStream.getChannel();
        FileChannel destination = fileOutputStream.getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:org.forgerock.openidm.patch.Main.java

private static void storePatchBundle(File workingDir, File installDir, Map<String, Object> params)
        throws PatchException {

    URL url = getPatchLocation();

    // Download the patch file
    ReadableByteChannel channel = null;
    try {/*from  w  w w  . ja  va2  s . c  o  m*/
        channel = Channels.newChannel(url.openStream());
    } catch (IOException ex) {
        throw new PatchException("Failed to access the specified file " + url + " " + ex.getMessage(), ex);
    }

    String targetFileName = new File(url.getPath()).getName();
    File patchDir = new File(workingDir, "patch/bin");
    patchDir.mkdirs();
    File targetFile = new File(patchDir, targetFileName);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(targetFile);
    } catch (FileNotFoundException ex) {
        throw new PatchException("Error in getting the specified file to " + targetFile, ex);
    }

    try {
        fos.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
        System.out.println("Downloaded to " + targetFile);
    } catch (IOException ex) {
        throw new PatchException("Failed to get the specified file " + url + " to: " + targetFile, ex);
    }
}

From source file:ja.centre.util.io.Files.java

public static void copy(String source, String destination) throws IOException {
    FileInputStream fis = null;/*w ww .j  a  v  a  2 s. c  o  m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(destination);

        FileChannel fic = null;
        FileChannel foc = null;
        try {
            fic = fis.getChannel();
            foc = fos.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);
            do {
                buffer.flip();
                foc.write(buffer);
                buffer.clear();
            } while (fic.read(buffer) != -1);
        } finally {
            closeQuietly(fic);
            closeQuietly(foc);
        }
    } finally {
        closeQuietly(fis);
        closeQuietly(fos);
    }
}

From source file:com.todotxt.todotxttouch.util.Util.java

public static void copyFile(File origFile, File newFile, boolean overwrite) {
    if (!origFile.exists()) {
        Log.e(TAG, "Error renaming file: " + origFile + " does not exist");

        throw new TodoException("Error copying file: " + origFile + " does not exist");
    }/*from www. j a  va2 s. co  m*/

    createParentDirectory(newFile);

    if (!overwrite && newFile.exists()) {
        Log.e(TAG, "Error copying file: destination exists: " + newFile);

        throw new TodoException("Error copying file: destination exists: " + newFile);
    }

    try {
        FileInputStream fis = new FileInputStream(origFile);
        FileOutputStream fos = new FileOutputStream(newFile);
        FileChannel in = fis.getChannel();
        fos.getChannel().transferFrom(in, 0, in.size());
        fis.close();
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, "Error copying " + origFile + " to " + newFile);

        throw new TodoException("Error copying " + origFile + " to " + newFile, e);
    }
}

From source file:org.wso2.carbon.esb.vfs.transport.test.ESBJAVA3470.java

/**
 * Copy the given source file to the given destination
 *
 * @param sourceFile/*from w  w  w .j a  v a2s  . c om*/
 *                 source file
 * @param destFile
 *                 destination file
 * @throws IOException
 */
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        fileInputStream = new FileInputStream(sourceFile);
        fileOutputStream = new FileOutputStream(destFile);

        FileChannel source = fileInputStream.getChannel();
        FileChannel destination = fileOutputStream.getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}

From source file:org.ngrinder.common.util.FileUtil.java

/**
 * Write object into file./*from   www.  j av  a  2 s .co  m*/
 * 
 * @param file
 *            file
 * @param obj
 *            obj to be written.
 */
public static void writeObjectToFile(File file, Object obj) {
    FileOutputStream fout = null;
    ObjectOutputStream oout = null;
    ByteArrayOutputStream bout = null;
    FileLock lock = null;
    try {
        bout = new ByteArrayOutputStream();
        oout = new ObjectOutputStream(bout);
        oout.writeObject(obj);
        oout.flush();
        byte[] byteArray = bout.toByteArray();
        fout = new FileOutputStream(file, false);
        lock = fout.getChannel().lock();
        fout.write(byteArray);
    } catch (Exception e) {
        LOGGER.error("IO error for file {} : {}", file, e.getMessage());
        LOGGER.debug("Details : ", e);
    } finally {
        if (lock != null) {
            try {
                lock.release();
            } catch (Exception e) {
                LOGGER.error("unlocking is failed for file {}", file, e);
            }
        }
        IOUtils.closeQuietly(bout);
        IOUtils.closeQuietly(fout);
        IOUtils.closeQuietly(oout);
    }
}

From source file:org.jboss.as.forge.util.Files.java

/**
 * Copies the source file to the destination file.
 *
 * @param srcFile    the file to copy/*w  ww . ja  va2 s. c  om*/
 * @param targetFile the target file
 *
 * @return {@code true} if the file was successfully copied, {@code false} if the copy failed or was incomplete
 *
 * @throws IOException if an IO error occurs copying the file
 */
public static boolean copyFile(final File srcFile, final File targetFile) throws IOException {
    FileInputStream in = null;
    FileOutputStream out = null;
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        in = new FileInputStream(srcFile);
        inChannel = in.getChannel();
        out = new FileOutputStream(targetFile);
        outChannel = out.getChannel();
        long bytesTransferred = 0;
        while (bytesTransferred < inChannel.size()) {
            bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel);
        }
    } finally {
        Streams.safeClose(outChannel);
        Streams.safeClose(out);
        Streams.safeClose(inChannel);
        Streams.safeClose(in);
    }
    return srcFile.length() == targetFile.length();
}

From source file:org.akubraproject.fs.FSBlob.java

private static void nioCopy(File source, File dest) throws IOException {
    FileInputStream f_in = null;//  w  w w . j  a  v  a2s  .c o  m
    FileOutputStream f_out = null;

    log.debug("Performing force copy-and-delete of source '" + source + "' to '" + dest + "'");
    try {
        f_in = new FileInputStream(source);

        try {
            f_out = new FileOutputStream(dest);

            FileChannel in = f_in.getChannel();
            FileChannel out = f_out.getChannel();
            in.transferTo(0, source.length(), out);
        } finally {
            IOUtils.closeQuietly(f_out);
        }
    } finally {
        IOUtils.closeQuietly(f_in);
    }

    if (!dest.exists())
        throw new IOException("Failed to copy file to new location: " + dest);
}

From source file:Main.java

/**
 * Effective way to copy files by file channel.
 * @return Return the number of copied files.
 *///from   www  .jav  a2 s  . c om
public static int fileChannelCopy(File[] sources, File[] targets) {
    int result = 0;
    if (sources == null || targets == null) {
        return result;
    }

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fc_in = null;
    FileChannel fc_out = null;
    try {
        for (int i = 0, len_s = sources.length, len_t = targets.length; i < len_s && i < len_t; ++i) {
            if (sources[i] == null || targets[i] == null) {
                continue;
            }

            fis = new FileInputStream(sources[i]);
            fos = new FileOutputStream(targets[i]);
            fc_in = fis.getChannel();
            fc_out = fos.getChannel();
            fc_in.transferTo(0, fc_in.size(), fc_out);

            ++result;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fc_out != null) {
            try {
                fc_out.close();
            } catch (IOException e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
            }
        }
        if (fc_in != null) {
            try {
                fc_in.close();
            } catch (IOException e) {
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }

    return result;
}