- package com.itheima;
- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class MyClassLoader extends ClassLoader {
- public static void main(String[] args) throws Exception {
- String srcPath = args[0];
- String destDir = args[1];
- FileInputStream fis = new FileInputStream(srcPath);
- String destFilename = srcPath.substring(srcPath.lastIndexOf('\\') + 1);// ?
- String destPath = destDir + "\\" + destFilename;
- FileOutputStream fos = new FileOutputStream(destPath);
- cypher(fis, fos);
- }
- private static void cypher(InputStream ips, OutputStream ops)
- throws Exception {
- int b = -1;
- while ((b = ips.read()) != -1) {
- ops.write(b ^ 0xff);
- }
- ips.close();
- ops.close();
- }
- private String classDir;
- @Override
- protected Class<?> findClass(String name) throws ClassNotFoundException {
- // TODO Auto-generated method stub
- // String classFileName = classDir + "\\" + name + ".class";
- String classFileName = classDir + "\\" + name.substring(name.lastIndexOf('.')+1) + ".class";
- try {
- FileInputStream fis = new FileInputStream(classFileName);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- cypher(fis, bos);
- fis.close();
- System.out.println("aaaaaaa");
- byte[] bytes = bos.toByteArray();
- return defineClass(null, bytes, 0, bytes.length);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return super.findClass(name);
- }
- MyClassLoader() {
- }
- MyClassLoader(String classDir) {
- this.classDir = classDir;
- }
- }
复制代码 String destFilename = srcPath.substring(srcPath.lastIndexOf('\\') + 1);这行代码中截取到'\\'后的子串,其中“+1”是为了干什么?看不懂. |