Example usage for java.nio.channels FileChannel force

List of usage examples for java.nio.channels FileChannel force

Introduction

In this page you can find the example usage for java.nio.channels FileChannel force.

Prototype

public abstract void force(boolean metaData) throws IOException;

Source Link

Document

Forces any updates to this channel's file to be written to the storage device that contains it.

Usage

From source file:com.healthmarketscience.jackcess.Database.java

/**
 * Create a new Database for the given fileFormat
 * @param fileFormat version of new database.
 * @param mdbFile Location to write the new database to.  <b>If this file
 *    already exists, it will be overwritten.</b>
 * @param autoSync whether or not to enable auto-syncing on write.  if
 *                 {@code true}, writes will be immediately flushed to disk.
 *                 This leaves the database in a (fairly) consistent state
 *                 on each write, but can be very inefficient for many
 *                 updates.  if {@code false}, flushing to disk happens at
 *                 the jvm's leisure, which can be much faster, but may
 *                 leave the database in an inconsistent state if failures
 *                 are encountered during writing.
 * @param charset Charset to use, if {@code null}, uses default
 * @param timeZone TimeZone to use, if {@code null}, uses default
 * @usage _intermediate_method_//from w  ww  . j  a  v a 2s .c  o m
 */
public static Database create(FileFormat fileFormat, File mdbFile, boolean autoSync, Charset charset,
        TimeZone timeZone) throws IOException {
    if (fileFormat.getJetFormat().READ_ONLY) {
        throw new IOException("jet format '" + fileFormat.getJetFormat() + "' does not support writing");
    }

    FileChannel channel = openChannel(mdbFile, false);
    channel.truncate(0);
    transferFrom(channel, getResourceAsStream(fileFormat._emptyFile));
    channel.force(true);
    return new Database(mdbFile, channel, autoSync, fileFormat, charset, timeZone, null);
}

From source file:com.healthmarketscience.jackcess.impl.DatabaseImpl.java

/**
 * Create a new Database for the given fileFormat
 * @param fileFormat version of new database.
 * @param mdbFile Location to write the new database to.  <b>If this file
 *                already exists, it will be overwritten.</b>
 * @param channel  pre-opened FileChannel.  if provided explicitly, it will
 *                 not be closed by this Database instance
 * @param autoSync whether or not to enable auto-syncing on write.  if
 *                 {@code true}, writes will be immediately flushed to disk.
 *                 This leaves the database in a (fairly) consistent state
 *                 on each write, but can be very inefficient for many
 *                 updates.  if {@code false}, flushing to disk happens at
 *                 the jvm's leisure, which can be much faster, but may
 *                 leave the database in an inconsistent state if failures
 *                 are encountered during writing.  Writes may be flushed at
 *                 any time using {@link #flush}.
 * @param charset  Charset to use, if {@code null}, uses default
 * @param timeZone TimeZone to use, if {@code null}, uses default
 * @usage _advanced_method_// w w w  .  j  av  a  2s .c  o  m
 */
public static DatabaseImpl create(FileFormat fileFormat, File mdbFile, FileChannel channel, boolean autoSync,
        Charset charset, TimeZone timeZone) throws IOException {
    FileFormatDetails details = getFileFormatDetails(fileFormat);
    if (details.getFormat().READ_ONLY) {
        throw new IOException("file format " + fileFormat + " does not support writing");
    }

    boolean closeChannel = false;
    if (channel == null) {
        channel = openChannel(mdbFile, false);
        closeChannel = true;
    }

    boolean success = false;
    try {
        channel.truncate(0);
        transferFrom(channel, getResourceAsStream(details.getEmptyFilePath()));
        channel.force(true);
        DatabaseImpl db = new DatabaseImpl(mdbFile, channel, closeChannel, autoSync, fileFormat, charset,
                timeZone, null);
        success = true;
        return db;
    } finally {
        if (!success && closeChannel) {
            // something blew up, shutdown the channel (quietly)
            ByteUtil.closeQuietly(channel);
        }
    }
}

From source file:com.yobidrive.diskmap.needles.NeedleManager.java

private void truncate(NeedlePointer needlePointer) throws NeedleManagerException {
    // Truncate current file
    if (needlePointer.getNeedleOffset() < 0) {
        logger.error("Request for truncating " + needlePointer.toString() + "!!!");
        throw new NeedleManagerException("Request for truncating " + needlePointer.toString() + "!!!");
    }// w  w w .  jav a 2  s .c om
    // truncates current file
    FileChannel fc = getChannel(needlePointer.getNeedleFileNumber());
    if (fc == null) {
        logger.error("Request for truncating " + needlePointer.toString() + ", no File Channel!!!");
        throw new NeedleManagerException(
                "Request for truncating " + needlePointer.toString() + ", no File Channel!!!");
    }
    try {
        fc.truncate(needlePointer.getNeedleOffset());
        fc.force(true);
    } catch (IOException ie) {
        logger.error("Error truncating " + needlePointer.toString(), ie);
        throw new NeedleManagerException("Error truncating " + needlePointer.toString(), ie);
    }
    // Removes subsequent files. In fact just rename them and alert via logger
    int logNumber = needlePointer.getNeedleFileNumber() + 1;
    while (channelMap.contains(new Integer(logNumber))) {
        closeChannel(logNumber);
        File toClose = getFile(logNumber);
        if (toClose != null) {
            try {
                toClose.renameTo(new File(toClose.getCanonicalFile() + ".bak"));
            } catch (IOException ie) {
                throw new NeedleManagerException(
                        "Could not rename to .bak for file number " + Integer.toHexString(logNumber));
            }
        }
        logNumber++;
    }
}

From source file:com.edgenius.wiki.service.impl.SitemapServiceImpl.java

private void appendSitemapIndex(String sitemap) throws IOException {
    File sitemapIndexFile = new File(mapResourcesRoot.getFile(), SITEMAP_INDEX_NAME);
    if (!sitemapIndexFile.exists()) {
        //if a new sitemap file
        List<String> lines = new ArrayList<String>();
        lines.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        lines.add("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");
        lines.add("</sitemapindex>");
        FileUtils.writeLines(sitemapIndexFile, lines);
    }/*w  ww .j  a v  a  2 s. co m*/

    RandomAccessFile rfile = new RandomAccessFile(sitemapIndexFile, "rw");
    FileChannel channel = rfile.getChannel();

    //this new content will append to end of file before XML end tag
    StringBuilder lines = new StringBuilder();
    lines.append("   <sitemap>\n");
    lines.append("     <loc>" + WebUtil.getHostAppURL() + SITEMAP_URL_CONTEXT + sitemap + "</loc>\n");
    lines.append("     <lastmod>" + TIME_FORMAT.format(new Date()) + " </lastmod>\n");
    lines.append("   </sitemap>\n");
    //the last tag will be overwrite, so append it again to new content. 
    lines.append(SITEMAP_INDEX_TAIL_FLAG);
    byte[] content = lines.toString().getBytes();

    ByteBuffer byteBuf = ByteBuffer.allocate(512);
    // seek first
    int len = 0, headIdx = 0;
    long tailIdx = channel.size() - 512;
    tailIdx = tailIdx < 0 ? 0 : tailIdx;

    long headPos = -1;
    StringBuilder header = new StringBuilder();
    while ((len = channel.read(byteBuf, tailIdx)) > 0) {
        byteBuf.rewind();
        byte[] dst = new byte[len];
        byteBuf.get(dst, 0, len);
        header.append(new String(dst, "UTF8"));
        headIdx = header.indexOf(SITEMAP_INDEX_TAIL_FLAG);
        if (headIdx != -1) {
            headPos = channel.size() - header.substring(headIdx).getBytes().length;
            break;
        }
    }
    FileLock lock = channel.tryLock(headPos, content.length, false);
    try {
        channel.write(ByteBuffer.wrap(content), headPos);
    } finally {
        lock.release();
    }

    channel.force(false);
    rfile.close();

}

From source file:com.cisco.dvbu.ps.common.util.CommonUtils.java

/**
 * The copyFile method is used to copy files from a source to a destination folder.
 * //from  w  w w  . j a  v a  2s  .c  o m
 * @param fromFilePath
 * @param toFilePath
 * @throws ValidationException
 */
public static void copyFile(String fromFilePath, String toFilePath, boolean forceCopy)
        throws ValidationException {

    FileChannel srcChannel = null;
    FileChannel dstChannel = null;
    boolean fileExists = fileExists(toFilePath);

    if (forceCopy && fileExists) {
        removeFile(toFilePath);
        fileExists = fileExists(toFilePath);
    }

    if ((!fileExists) || (forceCopy && fileExists)) {
        try {
            // Create channel on the source
            srcChannel = new FileInputStream(fromFilePath).getChannel();

            // Create channel on the destination
            dstChannel = new FileOutputStream(toFilePath).getChannel();

            // Force the copy - added to overcome copy error
            dstChannel.force(true);

            // Copy file contents from source to destination
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        } catch (IOException e) {
            String message = "Could not copy file " + fromFilePath + ".An error was encountered: "
                    + e.toString();
            throw new ValidationException(message, e);
        } finally {
            try {
                // Close the channels
                if (srcChannel != null)
                    srcChannel.close();
                if (dstChannel != null)
                    dstChannel.close();
                srcChannel = null;
                dstChannel = null;
            } catch (IOException e) {
                String message = "Could not copy file " + fromFilePath
                        + ".  Error encountered while closing source and destination channels: " + e.toString();
                throw new ValidationException(message, e);
            }
        }
    }
}

From source file:com.yobidrive.diskmap.buckets.BucketTableManager.java

private void commitBucketTableToDisk() throws BucketTableManagerException {
    File currentFile = null;/*from w  w  w .  j a va  2  s . c o m*/
    FileChannel fileChannel = null;
    ByteBuffer headerBuffer = null;
    try {
        logger.warn("Start commit bucket table...");
        if (bucketTable.getRequestedCheckPoint() == null || bucketTable.getRequestedCheckPoint().isEmpty())
            throw new BucketTableManagerException("commit requested while there is no requested checkpoint");
        currentFile = getLatestCommitedFile();
        File nextFile = getNextFile(getLatestCommitedFile());
        fileChannel = (new RandomAccessFile(nextFile, "rw")).getChannel();
        // Write header with empty checkpoint 
        headerBuffer = ByteBuffer.allocate(HEADERSIZE);
        fileChannel.position(0L);
        headerBuffer.putInt(MAGICSTART);
        headerBuffer.putLong(mapSize);
        // NeedlePointer lastCheckPoint = bucketTable.getLastCheckPoint() ; // Reset checkpoint to no checkpoint done
        NeedlePointer lastCheckPoint = new NeedlePointer(); // Empty needle
        lastCheckPoint.putNeedlePointerToBuffer(headerBuffer);
        headerBuffer.putInt(MAGICEND);
        headerBuffer.flip(); // truncate buffer
        fileChannel.write(headerBuffer);
        // Now writes buffers
        for (int i = 0; i < nbBuffers; i++) {
            bucketTable.prepareBufferForWriting(i);
            int written = fileChannel.write(bucketTable.getBuffer(i));
            if (written < bucketTable.getBuffer(i).limit())
                throw new BucketTableManagerException("Incomplete write for bucket table file "
                        + nextFile.getName() + ", expected " + mapSize + HEADERSIZE);
            // else
            // logger.info("Bucket table commit: written "+(i+1)*entriesPerBuffer+" buckets"+((i<(nbBuffers-1))?"...":"")) ;
            try {
                Thread.sleep(10);
            } catch (Throwable th) {

            }
        }
        // Writes second magic number
        ByteBuffer buffer = ByteBuffer.allocate(NeedleLogInfo.INFOSIZE);
        buffer.rewind();
        buffer.limit(INTSIZE);
        buffer.putInt(MAGICSTART);
        buffer.rewind();
        fileChannel.write(buffer);
        // Write Needle Log Info
        Iterator<NeedleLogInfo> it = logInfoPerLogNumber.values().iterator();
        while (it.hasNext()) {
            buffer.rewind();
            buffer.limit(NeedleLogInfo.INFOSIZE);
            NeedleLogInfo nli = it.next();
            nli.putNeedleLogInfo(buffer, true);
            int written = fileChannel.write(buffer);
            if (written < NeedleLogInfo.INFOSIZE)
                throw new BucketTableManagerException(
                        "Incomplete write for bucket table file, writing log infos " + nextFile.getName());
        }
        // Writes checkpoint
        headerBuffer = ByteBuffer.allocate(NeedlePointer.POINTERSIZE);
        headerBuffer.rewind();
        headerBuffer.limit(NeedlePointer.POINTERSIZE);
        // System.out.println("Writing checkpoint in index "+bucketTable.getRequestedCheckPoint()) ;
        bucketTable.getRequestedCheckPoint().putNeedlePointerToBuffer(headerBuffer, true); // Flip buffer after write
        headerBuffer.rewind();
        // fileChannel.force(false) ;
        if (fileChannel.write(headerBuffer, CHECKPOINTOFFSET) < NeedlePointer.POINTERSIZE) {
            throw new BucketTableManagerException("Could not write checkpoint to " + nextFile.getName());
        }
        fileChannel.force(true);
        fileChannel.close();
        if (!nextFile.renameTo(getCommittedFile(nextFile)))
            throw new BucketTableManagerException(
                    "Could not rename " + nextFile.getName() + " to " + getCommittedFile(nextFile).getName());

        logger.warn("Committed bucket table.");
    } catch (IOException ie) {
        throw new BucketTableManagerException("Failed writting bucket table", ie);
    } finally {
        headerBuffer = null; //May ease garbage collection
        if (fileChannel != null) {
            try {
                fileChannel.close();
            } catch (Exception ex) {
                throw new BucketTableManagerException("Failed to close file channel", ex);
            }
        }
    }
    try {
        if (currentFile != null) {
            if (!currentFile.delete())
                logger.error("Failed deleting previous bucket table" + currentFile.getName());
        }
    } catch (Throwable th) {
        logger.error("Failed deleting previous bucket table" + currentFile.getName(), th);
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.FSDataset.java

/**
 * Copies a file as fast as possible. Tries to do a hardlink instead of a copy
 * if the hardlink parameter is specified.
 *
 * @param src//from   www.  j a va 2 s .  com
 *          the source file for copying
 * @param dst
 *          the destination file for copying
 * @param hardlink
 *          whether or not to attempt a hardlink
 * @throws IOException
 */
public void copyFile(File src, File dst, boolean hardlink) throws IOException {

    if (src == null || dst == null) {
        throw new IOException("src/dst file is null");
    }

    try {
        if (hardlink && shouldHardLinkBlockCopy) {
            // Remove destination before hard linking, since this file might already
            // exist and a hardlink would fail as a result.
            if (dst.exists()) {
                if (!dst.delete()) {
                    throw new IOException("Deletion of file : " + dst + " failed");
                }
            }
            NativeIO.link(src, dst);
            DataNode.LOG.info("Hard Link Created from : " + src + " to " + dst);
            return;
        }
    } catch (IOException e) {
        DataNode.LOG
                .warn("Hard link failed from : " + src + " to " + dst + " continuing with regular file copy");
    }

    FileChannel input = null;
    FileChannel output = null;
    try {
        // This improves copying performance a lot, it uses native buffers
        // for copying.
        input = new FileInputStream(src).getChannel();
        output = new FileOutputStream(dst).getChannel();
        if (input == null || output == null) {
            throw new IOException("Could not create file channels for src : " + src + " dst : " + dst);
        }
        long bytesLeft = input.size();
        long position = 0;
        while (bytesLeft > 0) {
            long bytesWritten = output.transferFrom(input, position, bytesLeft);
            bytesLeft -= bytesWritten;
            position += bytesWritten;
        }
        if (datanode.syncOnClose) {
            output.force(true);
        }
    } finally {
        if (input != null) {
            input.close();
        }
        if (output != null) {
            output.close();
        }
    }
}

From source file:org.apache.nifi.file.FileUtils.java

/**
 * Copies the given source file to the given destination file. The given
 * destination will be overwritten if it already exists.
 *
 * @param source//from   w w w  .j a  va2  s  .  c o  m
 * @param destination
 * @param lockInputFile if true will lock input file during copy; if false
 * will not
 * @param lockOutputFile if true will lock output file during copy; if false
 * will not
 * @param move if true will perform what is effectively a move operation
 * rather than a pure copy. This allows for potentially highly efficient
 * movement of the file but if not possible this will revert to a copy then
 * delete behavior. If false, then the file is copied and the source file is
 * retained. If a true rename/move occurs then no lock is held during that
 * time.
 * @param logger if failures occur, they will be logged to this logger if
 * possible. If this logger is null, an IOException will instead be thrown,
 * indicating the problem.
 * @return long number of bytes copied
 * @throws FileNotFoundException if the source file could not be found
 * @throws IOException
 * @throws SecurityException if a security manager denies the needed file
 * operations
 */
public static long copyFile(final File source, final File destination, final boolean lockInputFile,
        final boolean lockOutputFile, final boolean move, final Logger logger)
        throws FileNotFoundException, IOException {

    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileLock inLock = null;
    FileLock outLock = null;
    long fileSize = 0L;
    if (!source.canRead()) {
        throw new IOException("Must at least have read permission");

    }
    if (move && source.renameTo(destination)) {
        fileSize = destination.length();
    } else {
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(destination);
            final FileChannel in = fis.getChannel();
            final FileChannel out = fos.getChannel();
            if (lockInputFile) {
                inLock = in.tryLock(0, Long.MAX_VALUE, true);
                if (null == inLock) {
                    throw new IOException("Unable to obtain shared file lock for: " + source.getAbsolutePath());
                }
            }
            if (lockOutputFile) {
                outLock = out.tryLock(0, Long.MAX_VALUE, false);
                if (null == outLock) {
                    throw new IOException(
                            "Unable to obtain exclusive file lock for: " + destination.getAbsolutePath());
                }
            }
            long bytesWritten = 0;
            do {
                bytesWritten += out.transferFrom(in, bytesWritten, TRANSFER_CHUNK_SIZE_BYTES);
                fileSize = in.size();
            } while (bytesWritten < fileSize);
            out.force(false);
            FileUtils.closeQuietly(fos);
            FileUtils.closeQuietly(fis);
            fos = null;
            fis = null;
            if (move && !FileUtils.deleteFile(source, null, 5)) {
                if (logger == null) {
                    FileUtils.deleteFile(destination, null, 5);
                    throw new IOException("Could not remove file " + source.getAbsolutePath());
                } else {
                    logger.warn(
                            "Configured to delete source file when renaming/move not successful.  However, unable to delete file at: "
                                    + source.getAbsolutePath());
                }
            }
        } finally {
            FileUtils.releaseQuietly(inLock);
            FileUtils.releaseQuietly(outLock);
            FileUtils.closeQuietly(fos);
            FileUtils.closeQuietly(fis);
        }
    }
    return fileSize;
}

From source file:org.apache.nifi.file.FileUtils.java

public static long copyFile(final File source, final OutputStream stream, final boolean closeOutputStream,
        final boolean lockInputFile) throws FileNotFoundException, IOException {
    FileInputStream fis = null;/*  www . j a  v a2 s .  co  m*/
    FileLock inLock = null;
    long fileSize = 0L;
    try {
        fis = new FileInputStream(source);
        final FileChannel in = fis.getChannel();
        if (lockInputFile) {
            inLock = in.tryLock(0, Long.MAX_VALUE, true);
            if (inLock == null) {
                throw new IOException("Unable to obtain exclusive file lock for: " + source.getAbsolutePath());
            }

        }

        byte[] buffer = new byte[1 << 18]; //256 KB
        int bytesRead = -1;
        while ((bytesRead = fis.read(buffer)) != -1) {
            stream.write(buffer, 0, bytesRead);
        }
        in.force(false);
        stream.flush();
        fileSize = in.size();
    } finally {
        FileUtils.releaseQuietly(inLock);
        FileUtils.closeQuietly(fis);
        if (closeOutputStream) {
            FileUtils.closeQuietly(stream);
        }
    }
    return fileSize;
}

From source file:org.cyclop.service.common.FileStorage.java

private FileChannel openForWrite(Path histPath) throws IOException {
    FileChannel byteChannel = FileChannel.open(histPath, StandardOpenOption.CREATE,
            StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
    byteChannel.force(true);
    FileChannel lockChannel = lock(histPath, byteChannel);
    return lockChannel;
}