网路类装载器客户端(0825写的)

网路类装载器客户端(0825写的)

 

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+");
  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());
 }
 
}

 

 

 

 

 

 

 

 

 

 

 

 


/*
public class ClassLoaderTest extends ClassLoader
{
 private String path;
 
 public ClassLoaderTest( String path )  //明确指定某个目录
 {
  this.path = path;
 }
 
 private byte[] loadClassData(String name ) throws Exception
 {
  File f = new File( path , name + ".class");
  FileInputStream fin = new FileInputStream( f );
  ByteArrayOutputStream bas = new ByteArrayOutputStream();
  //Read File...and put in bas
  byte[] datas = bas.toByteArray();
  bas.close();
  fin.close();
  return datas;
 }
 public  Class findClass( String name ) throws ClassNotFoundException
 {
  try
  {
   byte[] datas = loadClassData(name);
   return defineClass(name, datas, 0, datas.length );  //通过调用父类函数产生Class
  }
  catch(Exception e)
  {
   return null;
  }
 }
 
 //覆盖loadClass的时候就截断了父层委托,并呼叫findClass 
 public Class loadClass(String name ) throws ClassNotFoundException
 {
  return findClass(name);
 }
}*/

 

/*import java.util.*;
import java.io.*;

public class ClassLoaderTest extends ClassLoader
{
 private String directory = ".";
 
 public ClassLoaderTest( String dir )
 {
  directory = dir;
 }
 
 protected Class findClass(String name ) throws ClassNotFoundException
 {
  File  inFile = new File( directory , name + ".class" );
  
  if( !inFile.exists() )
  {
   throw new ClassNotFoundException("Can't find file:"+inFile.getPath());
  }
  
  try
  {
   FileInputStream fin = new FileInputStream( inFile );
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
  
   doLockAndUnLock( bout , fin );
   
   byte [] bytes = bout.toByteArray() ;
   
   fin.close();
   bout.close();
   
   return defineClass( name , bytes , 0 , bytes.length);
  }
  catch( Exception e )
  {
   return null;
  }

 }
 
 public static void doLockAndUnLock(OutputStream out , InputStream in )  throws Exception
 {
  short b = 0;
  
  while( (b = (short)in.read()) != -1 )
  {
   out.write( ((byte)b)^0xff );
  }
  
 }
 
 public static void main(String [] args)  throws Exception
 {
  if( args[0].endsWith(".class") )   //加密过程  args[0]-->文件 args[1]-->目的文件夹
  {
   File f = new File(args[0]);
   File dir = new File( args[1] );
   
   if( !f.exists())
   {
    throw new Exception("Can't find "+ args[0] );
   }
   if( !dir.isDirectory() )
   {
    throw new Exception(args[1] + "is not a directory!");
   }
   FileInputStream fin = new FileInputStream( f );
   FileOutputStream fout = new FileOutputStream( new File( dir , f.getName()));
   
   doLockAndUnLock( fout , fin );
   
   fin.close();
   fout.close();
   
  } 
  else //解密并且运行,通过装载器。。。 args[0]-->执行类名  args[1]-->类所在目录
  {
   ClassLoader loader = new ClassLoaderTest( args[1] );
   Class cls = loader.loadClass( args[0] );
   System.out.println(cls.getClassLoader().getClass().getName());
   System.out.println(cls.getName());
  }
 }
 
}
*/

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值