package samplecode;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class SealedClassTest {

    private static final String SEALING_CLASS_NAME = "samplecode.SealedClass";
    private static final String SEALED_CLASS_NAME = "samplecode.SealedClassUpperClass";   

    
    public static void main(String[] args) throws Exception {
    	CustomClassLoader c = new CustomClassLoader();
    	c.findClass(SEALING_CLASS_NAME);
    }     
   
       
    private static class CustomClassLoader extends ClassLoader {
    	
    	@Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            try (InputStream in = getResourceAsStream(name.replace('.', '/') + ".class")) {
                if (in == null) {
                    throw new ClassNotFoundException("Class not found: " + name);
                }
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024*1024];
                int len;
                while ((len = in.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                return defineClass(name, out.toByteArray(), 0, out.toByteArray().length);
            } catch (IOException e) {
                throw new ClassNotFoundException("Error reading class data", e);
            }
        }
    }
}

