/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * 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 org.activemq.util;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;

/**
 * This provides InputStream that delegates to com.sleepycat.util.FastInputStream 
 * if it is available and if it is not it delegates to java.io.ByteArrayInputStream.
 * 
 * This class allows ActiveMQ to not be dependent on the bdb lib.  It might 
 * be worth it to just fully implement a FastInputStream ourselfs.  I think
 * it's just a ByteArrayInputStream what is not thread safe.
 * 
 * @version $Revision: 1.1.1.1 $
 */
public class FastInputStream extends FilterInputStream {

	public FastInputStream(byte data[]) {
		super(createInputStream(data));
	}

	/**
	 * @return
	 */
	private static InputStream createInputStream(byte data[]) {
		try {
			Class c = FastInputStream.class.getClassLoader().loadClass("com.sleepycat.util.FastInputStream");
			Constructor constructor = c.getConstructor(new Class[]{byte[].class});
			return (InputStream)constructor.newInstance(new Object[]{data});
		} catch (Throwable e) {
			return new ByteArrayInputStream(data); 
		}
	}

}
