LUCENE-780: add static Directory.copy() method to copy files from one Directory to another

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@498755 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2007-01-22 19:21:56 +00:00
parent 02d6b05cd6
commit 3b13126bf5
3 changed files with 50 additions and 24 deletions

View File

@ -189,7 +189,10 @@ API Changes
take a boolean "create" argument. Instead you should use
IndexWriter's "create" argument to create a new index.
(Mike McCandless)
17. LUCENE-780: Add a static Directory.copy() method to copy files
from one Directory to another. (Jiri Kuhn via Mike McCandless)
Bug fixes
1. Fixed the web application demo (built with "ant war-demo") which

View File

@ -124,4 +124,49 @@ public abstract class Directory {
public String getLockID() {
return this.toString();
}
/**
* Copy contents of a directory src to a directory dest.
* If a file in src already exists in dest then the
* one in dest will be blindly overwritten.
*
* @param src source directory
* @param dest destination directory
* @param closeDirSrc if <code>true</code>, call {@link #close()} method on source directory
* @throws IOException
*/
public static void copy(Directory src, Directory dest, boolean closeDirSrc) throws IOException {
final String[] files = src.list();
byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
for (int i = 0; i < files.length; i++) {
IndexOutput os = null;
IndexInput is = null;
try {
// create file in dest directory
os = dest.createOutput(files[i]);
// read current file
is = src.openInput(files[i]);
// and copy to dest directory
long len = is.length();
long readCount = 0;
while (readCount < len) {
int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? (int)(len - readCount) : BufferedIndexOutput.BUFFER_SIZE;
is.readBytes(buf, 0, toRead);
os.writeBytes(buf, toRead);
readCount += toRead;
}
} finally {
// graceful cleanup
try {
if (os != null)
os.close();
} finally {
if (is != null)
is.close();
}
}
}
if(closeDirSrc)
src.close();
}
}

View File

@ -74,29 +74,7 @@ public class RAMDirectory extends Directory implements Serializable {
private RAMDirectory(Directory dir, boolean closeDir) throws IOException {
this();
final String[] files = dir.list();
byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
for (int i = 0; i < files.length; i++) {
// make place on ram disk
IndexOutput os = createOutput(files[i]);
// read current file
IndexInput is = dir.openInput(files[i]);
// and copy to ram disk
long len = is.length();
long readCount = 0;
while (readCount < len) {
int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? (int)(len - readCount) : BufferedIndexOutput.BUFFER_SIZE;
is.readBytes(buf, 0, toRead);
os.writeBytes(buf, toRead);
readCount += toRead;
}
// graceful cleanup
is.close();
os.close();
}
if(closeDir)
dir.close();
Directory.copy(dir, this, closeDir);
}
/**