package prolog.resource;

import java.io.*;
import java.net.*;

/**
 * this class encapsulates resources like textfiles
 * that should be placed in the resource package.
 */
public class Resource
{
	/**
	 * singleton instance
	 */
	protected static Resource gvInstance = null;

	public Resource()
	{
	}

	/**
	 * returns the singleton instance
	 */
	public static Resource getSharedInstance()
	{
		if( gvInstance == null )
			gvInstance = new Resource();
		return gvInstance;
	}

	/**
	 * loads a text file located in the resource package
	 */
	public String getTextResource( String fileName )
		throws Exception
	{
		StringBuffer b = new StringBuffer();
		InputStream in = getClass().getResourceAsStream( fileName );
		BufferedInputStream bin = new BufferedInputStream( in );
		byte[] buffer = new byte[4096];
		int read = 0;
		while( (read = bin.read( buffer )) > 0 )
			b.append( new String( buffer, 0, read ) );
		bin.close();
		in.close();
		return b.toString();
	}
}
