Java Code Examples for java.io.FileDescriptor#sync()

The following examples show how to use java.io.FileDescriptor#sync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileBasedStoreObjectAccessor.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private void syncFileSystem(final FileDescriptor fd) throws SyncFailedException {
    // Simple retry a number of times; avoids Repeater to avoid complications of timeouts and separate threads
    int maxTries = 3;

    SyncFailedException sfe = null;
    for (int c = 0 ; c < maxTries ; c++) {
        try {
            fd.sync();
            sfe = null;
            break;
        } catch (SyncFailedException e) {
            sfe = e;
        }
    }
    if (sfe != null) {
        throw sfe;
    }
}
 
Example 2
Source File: FileUtils.java    From dttv-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Copies bytes from the URL <code>source</code> to a file
 * <code>destination</code>. The directories up to <code>destination</code>
 * will be created if they don't already exist. <code>destination</code>
 * will be overwritten if it already exists.
 *
 * @param source  the <code>URL</code> to copy bytes from, must not be <code>null</code>
 * @param destination  the non-directory <code>File</code> to write bytes to
 *  (possibly overwriting), must not be <code>null</code>
 * @throws IOException if <code>source</code> URL cannot be opened
 * @throws IOException if <code>destination</code> is a directory
 * @throws IOException if <code>destination</code> cannot be written
 * @throws IOException if <code>destination</code> needs creating but can't be
 * @throws IOException if an IO error occurs during copying
 */
public static void copyURLToFile(URL source, File destination) throws IOException {
    InputStream input = source.openStream();
    try {
        FileOutputStream output = openOutputStream(destination);
        try {
            IOUtils.copy(input, output);
            FileDescriptor fd = output.getFD();
            if(fd.valid())
                fd.sync();
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }
}
 
Example 3
Source File: FileUtils.java    From dttv-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Copies bytes from the URL <code>source</code> to a file
 * <code>destination</code>. The directories up to <code>destination</code>
 * will be created if they don't already exist. <code>destination</code>
 * will be overwritten if it already exists.
 *
 * @param source  the <code>URL</code> to copy bytes from, must not be <code>null</code>
 * @param destination  the non-directory <code>File</code> to write bytes to
 *  (possibly overwriting), must not be <code>null</code>
 * @throws IOException if <code>source</code> URL cannot be opened
 * @throws IOException if <code>destination</code> is a directory
 * @throws IOException if <code>destination</code> cannot be written
 * @throws IOException if <code>destination</code> needs creating but can't be
 * @throws IOException if an IO error occurs during copying
 */
public static void copyURLToFile(URL source, File destination) throws IOException {
    InputStream input = source.openStream();
    try {
        FileOutputStream output = openOutputStream(destination);
        try {
            IOUtils.copy(input, output);
            FileDescriptor fd = output.getFD();
            if(fd.valid())
                fd.sync();
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }
}
 
Example 4
Source File: StdFsFileIO.java    From edslite with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void close() throws IOException
{
	try
	{
		FileDescriptor fd = getFD();
		if(fd!=null)
			fd.sync();
	}
	catch(SyncFailedException ignored) {}
	super.close();
}
 
Example 5
Source File: NativeIO.java    From vespa with Apache License 2.0 5 votes vote down vote up
/**
 * Will hint the OS that data read so far will not be accessed again and should hence be dropped from the buffer cache.
 * @param fd The file descriptor to drop from buffer cache.
 */
public void dropPartialFileFromCache(FileDescriptor fd, long offset, long len, boolean sync) {
    if (sync) {
        try {
            fd.sync();
        } catch (SyncFailedException e) {
            logger.warning("Sync failed while dropping cache: " + e.getMessage());
        }
    }
    if (initialized) {
        posix_fadvise(getNativeFD(fd), offset, len, POSIX_FADV_DONTNEED);
    }
}
 
Example 6
Source File: FileUtils.java    From dttv-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Internal copy file method.
 *
 * @param srcFile  the validated source file, must not be <code>null</code>
 * @param destFile  the validated destination file, must not be <code>null</code>
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream input = new FileInputStream(srcFile);
    try {
        FileOutputStream output = new FileOutputStream(destFile);
        try {
            IOUtils.copy(input, output);
            FileDescriptor fd = output.getFD();
            if(fd.valid())
                fd.sync();
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" +
                srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}
 
Example 7
Source File: FileUtils.java    From dttv-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Internal copy file method.
 *
 * @param srcFile  the validated source file, must not be <code>null</code>
 * @param destFile  the validated destination file, must not be <code>null</code>
 * @param preserveFileDate  whether to preserve the file date
 * @throws IOException if an error occurs
 */
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
    if (destFile.exists() && destFile.isDirectory()) {
        throw new IOException("Destination '" + destFile + "' exists but is a directory");
    }

    FileInputStream input = new FileInputStream(srcFile);
    try {
        FileOutputStream output = new FileOutputStream(destFile);
        try {
            IOUtils.copy(input, output);
            FileDescriptor fd = output.getFD();
            if(fd.valid())
                fd.sync();
        } finally {
            IOUtils.closeQuietly(output);
        }
    } finally {
        IOUtils.closeQuietly(input);
    }

    if (srcFile.length() != destFile.length()) {
        throw new IOException("Failed to copy full contents from '" +
                srcFile + "' to '" + destFile + "'");
    }
    if (preserveFileDate) {
        destFile.setLastModified(srcFile.lastModified());
    }
}
 
Example 8
Source File: BinaryDataAccessor.java    From io with Apache License 2.0 2 votes vote down vote up
/**
 * ファイルディスクリプタの同期.
 * @param fd ファイルディスクリプタ
 * @exception SyncFailedException 同期に失敗
 */
public void sync(FileDescriptor fd) throws SyncFailedException {
    fd.sync();
}
 
Example 9
Source File: BarFileInstaller.java    From io with Apache License 2.0 2 votes vote down vote up
/**
 * ファイルディスクリプタの同期.
 * @param fd ファイルディスクリプタ
 * @throws SyncFailedException 同期に失敗
 */
public void sync(FileDescriptor fd) throws SyncFailedException {
    fd.sync();
}