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.lexgrid.loader.writer.NoClosingRootTagStaxEventItemWriter.java

@Override
public void close() {
    super.close();

    String rootTag = "</" + getRootTagName() + ">";

    String xml = null;/*from  w w w  .  ja  v  a  2 s .  c  om*/
    try {
        xml = FileUtils.readFileToString(xmlFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!xml.endsWith(rootTag)) {
        try {
            FileOutputStream os = new FileOutputStream(xmlFile, true);
            FileChannel channel = os.getChannel();

            ByteBuffer byteBuffer = ByteBuffer.wrap(rootTag.getBytes());
            channel.write(byteBuffer);
            channel.close();

            xml = FileUtils.readFileToString(xmlFile);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }
}

From source file:com.nike.cerberus.operation.gateway.PublishLambdaOperation.java

private File downloadArtifact(final URL artifactUrl) {
    final File tempDir = Files.createTempDir();
    final String filePath = tempDir.getPath() + File.pathSeparator + "lambda.artifact";
    logger.debug("Downloading artifact to {}", tempDir.getAbsolutePath());

    try {/*from   w  w  w.j  av  a 2s  .c  o  m*/
        ReadableByteChannel rbc = Channels.newChannel(artifactUrl.openStream());
        FileOutputStream fos = new FileOutputStream(filePath);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch (IOException e) {
        logger.error("Failed to download the lambda artifact!", e);
        throw new IllegalStateException("Failed to download the lambda artifact.", e);
    }

    return new File(filePath);
}

From source file:org.opencastproject.util.FileSupport.java

/**
 * Copies the specified <code>sourceLocation</code> to <code>targetLocation</code> and returns a reference to the
 * newly created file or directory./* w ww . j  a v  a  2s  . co  m*/
 * <p/>
 * If <code>targetLocation</code> is an existing directory, then the source file or directory will be copied into this
 * directory, otherwise the source file will be copied to the file identified by <code>targetLocation</code>.
 * <p/>
 * If <code>overwrite</code> is set to <code>false</code>, this method throws an {@link IOException} if the target
 * file already exists.
 * <p/>
 * Note that if <code>targetLocation</code> is a directory than the directory itself, not only its content is copied.
 * 
 * @param sourceFile
 *          the source file or directory
 * @param targetFile
 *          the directory to copy the source file or directory to
 * @param overwrite
 *          <code>true</code> to overwrite existing files
 * @return the created copy
 * @throws IOException
 *           if copying of the file or directory failed
 */
public static File copy(File sourceFile, File targetFile, boolean overwrite) throws IOException {

    // This variable is used when the channel copy files, and stores the maximum size of the file parts copied from source to target
    final int chunk = 1024 * 1024 * 512; // 512 MB

    // This variable is used when the cannel copy fails completely, as the size of the memory buffer used to copy the data from one stream to the other.
    final int bufferSize = 1024 * 1024; // 1 MB 

    File dest = determineDestination(targetFile, sourceFile, overwrite);

    // We are copying a directory
    if (sourceFile.isDirectory()) {
        if (!dest.exists()) {
            dest.mkdirs();
        }
        File[] children = sourceFile.listFiles();
        for (File child : children) {
            copy(child, dest, overwrite);
        }
    }
    // We are copying a file
    else {
        // If dest is not an "absolute file", getParentFile may return null, even if there *is* a parent file.
        // That's why "getAbsoluteFile" is used here
        dest.getAbsoluteFile().getParentFile().mkdirs();
        if (dest.exists())
            delete(dest);

        FileChannel sourceChannel = null;
        FileChannel targetChannel = null;
        FileInputStream sourceStream = null;
        FileOutputStream targetStream = null;
        long size = 0;

        try {
            sourceStream = new FileInputStream(sourceFile);
            targetStream = new FileOutputStream(dest);
            try {
                sourceChannel = sourceStream.getChannel();
                targetChannel = targetStream.getChannel();
                size = targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            } catch (IOException ioe) {
                logger.warn("Got IOException using Channels for copying.");
            } finally {
                // This has to be in "finally", because in 64-bit machines the channel copy may fail to copy the whole file without causing a exception
                if ((sourceChannel != null) && (targetChannel != null) && (size < sourceFile.length()))
                    // Failing back to using FileChannels *but* with chunks and not altogether
                    logger.info("Trying to copy the file in chunks using Channels");
                if (size != sourceFile.length()) {
                    while (size < sourceFile.length())
                        size += targetChannel.transferFrom(sourceChannel, size, chunk);
                }
            }
        } catch (IOException ioe) {
            if ((sourceStream != null) && (targetStream != null) && (size < sourceFile.length())) {
                logger.warn(
                        "Got IOException using Channels for copying in chunks. Trying to use stream copy instead...");
                int copied = 0;
                byte[] buffer = new byte[bufferSize];
                while ((copied = sourceStream.read(buffer, 0, buffer.length)) != -1)
                    targetStream.write(buffer, 0, copied);
            } else
                throw ioe;
        } finally {
            if (sourceChannel != null)
                sourceChannel.close();
            if (sourceStream != null)
                sourceStream.close();
            if (targetChannel != null)
                targetChannel.close();
            if (targetStream != null)
                targetStream.close();
        }

        if (sourceFile.length() != dest.length()) {
            logger.warn("Source " + sourceFile + " and target " + dest + " do not have the same length");
            // TOOD: Why would this happen?
            // throw new IOException("Source " + sourceLocation + " and target " +
            // dest + " do not have the same length");
        }
    }
    return dest;
}

From source file:com.buaa.cfs.io.nativeio.NativeIO.java

/**
 * Unbuffered file copy from src to dst without tainting OS buffer cache
 * <p>/*from   ww w .  j av  a 2  s.c  o m*/
 * In POSIX platform: It uses FileChannel#transferTo() which internally attempts unbuffered IO on OS with native
 * sendfile64() support and falls back to buffered IO otherwise.
 * <p>
 * It minimizes the number of FileChannel#transferTo call by passing the the src file size directly instead of a
 * smaller size as the 3rd parameter. This saves the number of sendfile64() system call when native sendfile64() is
 * supported. In the two fall back cases where sendfile is not supported, FileChannle#transferTo already has its own
 * batching of size 8 MB and 8 KB, respectively.
 * <p>
 * In Windows Platform: It uses its own native wrapper of CopyFileEx with COPY_FILE_NO_BUFFERING flag, which is
 * supported on Windows Server 2008 and above.
 * <p>
 * Ideally, we should use FileChannel#transferTo() across both POSIX and Windows platform. Unfortunately, the
 * wrapper(Java_sun_nio_ch_FileChannelImpl_transferTo0) used by FileChannel#transferTo for unbuffered IO is not
 * implemented on Windows. Based on OpenJDK 6/7/8 source code, Java_sun_nio_ch_FileChannelImpl_transferTo0 on
 * Windows simply returns IOS_UNSUPPORTED.
 * <p>
 * Note: This simple native wrapper does minimal parameter checking before copy and consistency check (e.g., size)
 * after copy. It is recommended to use wrapper function like the Storage#nativeCopyFileUnbuffered() function in
 * hadoop-hdfs with pre/post copy checks.
 *
 * @param src The source path
 * @param dst The destination path
 *
 * @throws IOException
 */
public static void copyFileUnbuffered(File src, File dst) throws IOException {
    if (nativeLoaded && Shell.WINDOWS) {
        copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath());
    } else {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel input = null;
        FileChannel output = null;
        try {
            fis = new FileInputStream(src);
            fos = new FileOutputStream(dst);
            input = fis.getChannel();
            output = fos.getChannel();
            long remaining = input.size();
            long position = 0;
            long transferred = 0;
            while (remaining > 0) {
                transferred = input.transferTo(position, remaining, output);
                remaining -= transferred;
                position += transferred;
            }
        } finally {
            IOUtils.cleanup(LOG, output);
            IOUtils.cleanup(LOG, fos);
            IOUtils.cleanup(LOG, input);
            IOUtils.cleanup(LOG, fis);
        }
    }
}

From source file:com.mgmtp.perfload.perfalyzer.util.ChannelManager.java

/**
 * Returns a channel with the specified key. Channels are cached internally under the specified
 * key. If this method is called the first time for the specified key, a new channel is opened
 * and cached. Directories are created as necessary.
 *
 * @param channelKey//ww w . ja  va  2 s  . co m
 *            the channel key
 * @return the channel
 */
public WritableByteChannel getChannel(final String channelKey) throws IOException {
    ChannelStreamContainer csc = cscMap.get(channelKey);
    if (csc == null) {
        csc = new ChannelStreamContainer();
        cscMap.put(channelKey, csc);
        File globalDestFile = new File(destDir,
                fileNamingStrategy.createFileName(channelKey).getFile().getPath());
        Files.createParentDirs(globalDestFile);
        FileOutputStream fos = new FileOutputStream(globalDestFile);
        csc.os = fos;
        csc.ch = fos.getChannel();
    }
    return csc.ch;
}

From source file:org.richfaces.tests.qa.plugin.utils.files.SimpleExtractor.java

@Override
public void extract(File baseDir, File archive) throws IOException {
    if (archive.getAbsolutePath().endsWith("zip")) {
        getLog().info(MessageFormat.format("Extracting zip file <{0}> to directory <{1}>.",
                archive.getAbsolutePath(), baseDir));
        final ZipInputStream is = new ZipInputStream(new BufferedInputStream(new FileInputStream(archive)));
        ZipEntry entry;//from   w w  w  . ja v  a  2  s. c  o  m
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(baseDir, entry.getName());
                if (file.exists()) {
                    file.delete();
                }
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(is), file);
            }
        }
        is.close();
    } else if (archive.getAbsolutePath().endsWith("tar.bz2") || archive.getAbsolutePath().endsWith("tar.gz")) {
        getLog().info(MessageFormat.format("Extracting tar.bz2/tar.gz file <{0}> to directory <{1}>.",
                archive.getAbsolutePath(), baseDir));

        // unzip to tar
        File tarfile = new File(baseDir, "archive.tar");
        tarfile.delete();
        BZip2CompressorInputStream from = new BZip2CompressorInputStream(
                new BufferedInputStream(new FileInputStream(archive)));
        FileOutputStream to = new FileOutputStream(tarfile);
        to.getChannel().transferFrom(Channels.newChannel(from), 0, Long.MAX_VALUE);
        // untar
        final TarArchiveInputStream is = new TarArchiveInputStream(
                new BufferedInputStream(new FileInputStream(tarfile)));
        TarArchiveEntry entry;
        File file;
        while ((entry = is.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                file = new File(baseDir, entry.getName());
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(is), file);
            }
        }
        is.close();
        tarfile.delete();
    } else {
        throw new UnsupportedOperationException("Not supported file format " + archive.getName());
    }
    getLog().info(MessageFormat.format("Extracting of <{0}> completed.", archive.getAbsolutePath()));
}

From source file:control.services.DownloadPortraitsHttpServiceImpl.java

@Override
public File downloadPortraisFile(String portraitsFileName, String portraitsFolderName)
        throws FileNotFoundException {
    File file = null;//  w  ww . j  a  va2s.  c o m
    try {
        URL website = new URL(PROTOCOL_HOST + CLASH_HOST + PORTRAITS_PATH + portraitsFileName);
        //https://www.colorado.edu/conflict/peace/download/peace.zip
        // speedtest.ftp.otenet.gr/files/test10Mb.db
        //  URL website = new URL("https://www.colorado.edu/conflict/peace/download/peace.zip");
        file = new File(portraitsFolderName + File.separator + portraitsFileName);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());

        FileOutputStream fos = new FileOutputStream(file);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.close();

    } catch (IOException ex) {
        LOG.error(ex);
        throw new FileNotFoundException();
    }
    return file;
}

From source file:org.sonar.batch.bootstrap.BatchPluginExploder.java

private File unzipFile(File cachedFile) throws IOException {
    String filename = cachedFile.getName();
    File destDir = new File(cachedFile.getParentFile(), filename + "_unzip");
    File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock");
    if (!destDir.exists()) {
        FileOutputStream out = new FileOutputStream(lockFile);
        try {//from  w  ww. j  a va2  s  .c  o m
            java.nio.channels.FileLock lock = out.getChannel().lock();
            try {
                // Recheck in case of concurrent processes
                if (!destDir.exists()) {
                    File tempDir = fileCache.createTempDir();
                    ZipUtils.unzip(cachedFile, tempDir, newLibFilter());
                    FileUtils.moveDirectory(tempDir, destDir);
                }
            } finally {
                lock.release();
            }
        } finally {
            out.close();
            FileUtils.deleteQuietly(lockFile);
        }
    }
    return destDir;
}

From source file:com.facebook.infrastructure.net.io.ContentStreamState.java

private void createFileChannel() throws IOException {
    if (fc_ == null) {
        logger_.debug("Creating file for " + streamContext_.getTargetFile());
        FileOutputStream fos = new FileOutputStream(streamContext_.getTargetFile(), true);
        fc_ = fos.getChannel();
    }//from  w ww. j  av a2 s.  co  m
}

From source file:org.sonar.batch.bootstrap.BatchPluginJarExploder.java

private File unzipFile(File cachedFile) throws IOException {
    String filename = cachedFile.getName();
    File destDir = new File(cachedFile.getParentFile(), filename + "_unzip");
    File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock");
    if (!destDir.exists()) {
        FileOutputStream out = new FileOutputStream(lockFile);
        try {/*from   w  w w .j  av  a 2  s. c o  m*/
            java.nio.channels.FileLock lock = out.getChannel().lock();
            try {
                // Recheck in case of concurrent processes
                if (!destDir.exists()) {
                    File tempDir = fileCache.createTempDir();
                    ZipUtils.unzip(cachedFile, tempDir, newLibFilter());
                    FileUtils.moveDirectory(tempDir, destDir);
                }
            } finally {
                lock.release();
            }
        } finally {
            out.close();
            deleteQuietly(lockFile);
        }
    }
    return destDir;
}