下面列出了java.io.FileDescriptor#sync ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
/**
* 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);
}
}
/**
* 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);
}
}
@Override
public void close() throws IOException
{
try
{
FileDescriptor fd = getFD();
if(fd!=null)
fd.sync();
}
catch(SyncFailedException ignored) {}
super.close();
}
/**
* 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);
}
}
/**
* 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());
}
}
/**
* 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());
}
}
/**
* ファイルディスクリプタの同期.
* @param fd ファイルディスクリプタ
* @exception SyncFailedException 同期に失敗
*/
public void sync(FileDescriptor fd) throws SyncFailedException {
fd.sync();
}
/**
* ファイルディスクリプタの同期.
* @param fd ファイルディスクリプタ
* @throws SyncFailedException 同期に失敗
*/
public void sync(FileDescriptor fd) throws SyncFailedException {
fd.sync();
}