网路类装载器客户端
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.regex.*;
// Client
public class ClassLoaderTest extends ClassLoader
{
private String IPAddr = null;
private int port;
public ClassLoaderTest( String ip , int port )
{
ip = ip.trim();
Pattern ptn = Pattern.compile("//d+.//d+.//d+.//d+"); //Regular Expression
Matcher mtc = ptn.matcher(ip);
if( !mtc.matches() || port > 65535 || port <= 0)
{
throw new IllegalArgumentException("Argument Error!");
}
IPAddr = ip;
this.port = port;
}
protected Class findClass( String Name ) throws ClassNotFoundException
{
try
{
Socket sock = new Socket( IPAddr , port );
InputStream sin = sock.getInputStream();
BufferedInputStream bin = new BufferedInputStream( sin );
ByteArrayOutputStream baout = new ByteArrayOutputStream();
int b;
while( (b=bin.read()) != -1 )
{
baout.write(b);
}
byte[] bts = baout.toByteArray();
bin.close();
baout.close();
return defineClass( Name , bts , 0 , bts.length );
}
catch(Exception e )
{
return null;
}
}
public static void main(String [] args)throws Exception
{
ClassLoaderTest ldr = new ClassLoaderTest("162.105.81.165",80);
Class cls = ldr.loadClass("ClassFilter");
System.out.println(cls.getName());
System.out.println(cls.getClassLoader().getClass().getName());
}
}