/*
Copyright 2007 David Spencer.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
package com.tropo.lucene;

import java.util.*;

import org.apache.lucene.store.*;
import org.apache.lucene.index.*;

/**
 * Ram directory conversion.
 */
public final class Rammer
{
	private Rammer()
	{
	}
	
	/**
	 * Create a RAMDirectory from a disk rep.
	 * @param path the path to the dir that stores the index
	 */
	public static RAMDirectory convert( String path)
		throws java.io.IOException
	{
		final RAMDirectory ram = new RAMDirectory();
		final Directory d = FSDirectory.getDirectory( path, false);
		final String[] ar = d.list();
		for ( int i = 0; i< ar.length; i++)
		{
			// make place on ram disk
			OutputStream os = ram.createFile( ar[ i]);
			// read current file
			InputStream is = d.openFile( ar[ i]);
			// and copy to ram disk
			int len = (int) is.length();			
			byte[] buf = new byte[ len];
			is.readBytes( buf, 0, len);
			os.writeBytes( buf, len);
			// graceful cleanup
			is.close();
			os.close();
		}
		return ram;
	}

	private static java.io.PrintStream o = System.out;
}
