package org.jboss.cache.marshall;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FooClassLoader extends ClassLoader
{
   private Class foo;
   public FooClassLoader(ClassLoader parent)
   {
      super(parent);
   }
   
   public Class loadFoo() throws ClassNotFoundException
   {
      if (foo == null)
      {
         try
         {
            InputStream is = getResourceAsStream("org/jboss/cache/marshall/Foo.clazz");
            byte[] bytes = new byte[1024];                
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            int read;
            while ((read = is.read(bytes)) > -1) {
                baos.write(bytes, 0, read);
            }
            bytes = baos.toByteArray();
            foo = this.defineClass("org.jboss.cache.marshall.Foo", bytes, 0, bytes.length);
         }
         catch (IOException e)
         {
            throw new ClassNotFoundException("cannot read org/jboss/cache/marshall/Foo.clazz", e);
         }
      }
      return foo;
   }

}
