Index: src/java/org/apache/lucene/store/RAMDirectory.java =================================================================== --- src/java/org/apache/lucene/store/RAMDirectory.java (revision 497169) +++ src/java/org/apache/lucene/store/RAMDirectory.java (working copy) @@ -74,29 +74,7 @@ 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); } /** Index: src/java/org/apache/lucene/store/Directory.java =================================================================== --- src/java/org/apache/lucene/store/Directory.java (revision 497169) +++ src/java/org/apache/lucene/store/Directory.java (working copy) @@ -124,4 +124,39 @@ public String getLockID() { return this.toString(); } + + /** + * Deeply copy content of a directory src to a directory dest. + * It is assumed the dest directory is empty. + * + * @param src source directory + * @param dest destination directory + * @param closeDirSrc if true, 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++) { + // create file in dest directory + IndexOutput os = dest.createOutput(files[i]); + // read current file + IndexInput 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; + } + + // graceful cleanup + is.close(); + os.close(); + } + if(closeDirSrc) + src.close(); + } }