mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-25 01:19:02 +00:00
better copy over file
This commit is contained in:
parent
4692ef805b
commit
a6d9c84910
@ -19,10 +19,8 @@
|
||||
|
||||
package org.elasticsearch.util.io;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.io.*;
|
||||
import java.nio.channels.FileChannel;
|
||||
|
||||
/**
|
||||
* @author kimchy (Shay Banon)
|
||||
@ -91,6 +89,33 @@ public class FileSystemUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void copyFile(File sourceFile, File destinationFile) throws IOException {
|
||||
FileInputStream sourceIs = null;
|
||||
FileChannel source = null;
|
||||
FileOutputStream destinationOs = null;
|
||||
FileChannel destination = null;
|
||||
try {
|
||||
sourceIs = new FileInputStream(sourceFile);
|
||||
source = sourceIs.getChannel();
|
||||
destinationOs = new FileOutputStream(destinationFile);
|
||||
destination = destinationOs.getChannel();
|
||||
destination.transferFrom(source, 0, source.size());
|
||||
} finally {
|
||||
if (source != null) {
|
||||
source.close();
|
||||
}
|
||||
if (sourceIs != null) {
|
||||
sourceIs.close();
|
||||
}
|
||||
if (destination != null) {
|
||||
destination.close();
|
||||
}
|
||||
if (destinationOs != null) {
|
||||
destinationOs.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private FileSystemUtils() {
|
||||
|
||||
}
|
||||
|
@ -24,12 +24,12 @@ import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.store.*;
|
||||
import org.elasticsearch.index.store.support.ForceSyncDirectory;
|
||||
import org.elasticsearch.util.SizeValue;
|
||||
import org.elasticsearch.util.io.FileSystemUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.elasticsearch.util.io.FileSystemUtils.*;
|
||||
|
||||
/**
|
||||
* A set of utilities for Lucene {@link Directory}.
|
||||
*
|
||||
@ -85,25 +85,12 @@ public class Directories {
|
||||
if (!copyTo.exists()) {
|
||||
copyTo.createNewFile();
|
||||
}
|
||||
FileChannel source = null;
|
||||
FileChannel destination = null;
|
||||
try {
|
||||
source = new FileInputStream(new File(((FSDirectory) dir).getFile(), fileName)).getChannel();
|
||||
destination = new FileOutputStream(copyTo).getChannel();
|
||||
destination.transferFrom(source, 0, source.size());
|
||||
} finally {
|
||||
if (source != null) {
|
||||
source.close();
|
||||
}
|
||||
if (destination != null) {
|
||||
destination.close();
|
||||
}
|
||||
}
|
||||
copyFile(new File(((FSDirectory) dir).getFile(), fileName), copyTo);
|
||||
} else {
|
||||
copyFromDirectory(dir.openInput(fileName), new FileOutputStream(copyTo));
|
||||
}
|
||||
// sync the file
|
||||
FileSystemUtils.syncFile(copyTo);
|
||||
syncFile(copyTo);
|
||||
}
|
||||
|
||||
public static void copyFromDirectory(IndexInput ii, OutputStream os) throws IOException {
|
||||
@ -142,20 +129,7 @@ public class Directories {
|
||||
if (!destinationFile.exists()) {
|
||||
destinationFile.createNewFile();
|
||||
}
|
||||
FileChannel source = null;
|
||||
FileChannel destination = null;
|
||||
try {
|
||||
source = new FileInputStream(copyFrom).getChannel();
|
||||
destination = new FileOutputStream(destinationFile).getChannel();
|
||||
destination.transferFrom(source, 0, source.size());
|
||||
} finally {
|
||||
if (source != null) {
|
||||
source.close();
|
||||
}
|
||||
if (destination != null) {
|
||||
destination.close();
|
||||
}
|
||||
}
|
||||
copyFile(copyFrom, destinationFile);
|
||||
} else {
|
||||
copyToDirectory(new FileInputStream(copyFrom), dir.createOutput(fileName));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user