From 470958fd96d4d3b71109ca4eb5d9a8fe9bb2587d Mon Sep 17 00:00:00 2001 From: chinaTenCode Date: Tue, 5 Nov 2019 16:29:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?;=E7=94=B1=E5=8F=8C=E4=BA=B2=E5=A7=94?= =?UTF-8?q?=E6=B4=BE=E5=BC=95=E5=8F=91=E7=9A=84=E9=97=AE=E9=A2=98,?= =?UTF-8?q?=E4=BB=BF=E7=83=AD=E5=8A=A0=E8=BD=BD=E5=AE=9E=E7=8E=B0[https://?= =?UTF-8?q?blog.csdn.net/tanga842428/article/details/79195844];?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/mashibing/jvm/heatload/GetPI.java | 9 ++ src/com/mashibing/jvm/heatload/HowswapCL.java | 112 ++++++++++++++++++ src/com/mashibing/jvm/heatload/Test.java | 26 ++++ src/java/lang/Long.java | 16 +++ src/java/lang/Mys.java | 16 +++ 5 files changed, 179 insertions(+) create mode 100644 src/com/mashibing/jvm/heatload/GetPI.java create mode 100644 src/com/mashibing/jvm/heatload/HowswapCL.java create mode 100644 src/com/mashibing/jvm/heatload/Test.java create mode 100644 src/java/lang/Long.java create mode 100644 src/java/lang/Mys.java diff --git a/src/com/mashibing/jvm/heatload/GetPI.java b/src/com/mashibing/jvm/heatload/GetPI.java new file mode 100644 index 0000000..6561172 --- /dev/null +++ b/src/com/mashibing/jvm/heatload/GetPI.java @@ -0,0 +1,9 @@ +package com.mashibing.jvm.heatload; + +public class GetPI { + + // 本热部署实验中,上面的Darts函数没有用到,请忽略 + public static void Output() { + System.out.println("Output"); + } +} \ No newline at end of file diff --git a/src/com/mashibing/jvm/heatload/HowswapCL.java b/src/com/mashibing/jvm/heatload/HowswapCL.java new file mode 100644 index 0000000..fba5f06 --- /dev/null +++ b/src/com/mashibing/jvm/heatload/HowswapCL.java @@ -0,0 +1,112 @@ +package com.mashibing.jvm.heatload; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.HashSet; + +/* + * 实现热部署,自定义ClassLoader,加载的是.class + */ +class HowswapCL extends ClassLoader { + + private String basedir; // 需要该类加载器直接加载的类文件的基目录 + private HashSet dynaclazns; // 需要由该类加载器直接加载的类名 + + public HowswapCL(String basedir, String[] clazns) { + super(null); // 指定父类加载器为 null + this.basedir = basedir; + dynaclazns = new HashSet(); + loadClassByMe(clazns); + } + + private void loadClassByMe(String[] clazns) { + for (int i = 0; i < clazns.length; i++) { + loadDirectly(clazns[i]); + dynaclazns.add(clazns[i]); + } + } + + private Class loadDirectly(String name) { + Class cls = null; + StringBuffer sb = new StringBuffer(basedir); + String classname = name.replace('.', File.separatorChar) + ".class"; + sb.append(File.separator + classname); + File classF = new File(sb.toString()); + try { + cls = instantiateClass(name, new FileInputStream(classF), classF.length()); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return cls; + } + + private Class instantiateClass(String name, InputStream fin, long len) { + byte[] raw = new byte[(int) len]; + try { + fin.read(raw); + fin.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return defineClass(name, raw, 0, raw.length); + } + + protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { + Class cls = null; + cls = findLoadedClass(name); + if (!this.dynaclazns.contains(name) && cls == null) { + cls = getSystemClassLoader().loadClass(name); + } + if (cls == null) { + throw new ClassNotFoundException(name); + } + if (resolve) { + resolveClass(cls); + } + return cls; + } +} + +/* + * 每隔500ms运行一次,不断加载class + */ +class Multirun implements Runnable { + + public void run() { + try { + while (true) { + // 每次都创建出一个新的类加载器 + // class需要放在自己package名字的文件夹下 + String url = "D:\\D\\github\\JVM\\out\\production\\JVM\\";//System.getProperty("user.dir") + "/lib";// "/lib/yerasel/GetPI.jar"; + HowswapCL cl = new HowswapCL(url, new String[]{"com.mashibing.jvm.heatload.GetPI"}); + Class cls = cl.loadClass("com.mashibing.jvm.heatload.GetPI"); + System.out.println(cls.getClassLoader()); + System.out.println(GetPI.class.getClassLoader()); + GetPI foo = (GetPI) cls.newInstance(); + // 被调用函数的参数 + foo.Output(); +// Method m = foo.getClass().getMethod("Output", new Class[]{}); +// m.invoke(foo, new Object[]{}); + Thread.sleep(500); + } + + } catch (Exception ex) { + ex.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/com/mashibing/jvm/heatload/Test.java b/src/com/mashibing/jvm/heatload/Test.java new file mode 100644 index 0000000..b4df43c --- /dev/null +++ b/src/com/mashibing/jvm/heatload/Test.java @@ -0,0 +1,26 @@ +package com.mashibing.jvm.heatload; + +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +public class Test { + + public static Method initAddMethod() { + try { + Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); + add.setAccessible(true); + return add; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + + // 热部署测试代码 + Thread t; + t = new Thread(new Multirun()); + t.start(); + } +} \ No newline at end of file diff --git a/src/java/lang/Long.java b/src/java/lang/Long.java new file mode 100644 index 0000000..4e74a1e --- /dev/null +++ b/src/java/lang/Long.java @@ -0,0 +1,16 @@ +package java.lang; + +/** + * @Title: Long + * @Description: Long + * @Company:www.wrenchdata.com + * @author:shenshilong[shilong_shen@163.com] + * @date:2019-11-05 + * @version:V1.0 + */ +public class Long { + + public static void main(String[] args) { + System.out.println(1); + } +} diff --git a/src/java/lang/Mys.java b/src/java/lang/Mys.java new file mode 100644 index 0000000..5d90cb4 --- /dev/null +++ b/src/java/lang/Mys.java @@ -0,0 +1,16 @@ +package java.lang; + +/** + * @Title: Mys + * @Description: Mys + * @Company:www.wrenchdata.com + * @author:shenshilong[shilong_shen@163.com] + * @date:2019-11-05 + * @version:V1.0 + */ +public class Mys { + + public static void main(String[] args) { + System.out.println(1); + } +} From 31204b13fd9b89716c59f965f15c25c8ebaf9323 Mon Sep 17 00:00:00 2001 From: chinaTenCode <1019358770@qq.com> Date: Tue, 12 Nov 2019 09:57:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?;=E6=96=87=E4=BB=B6=E5=88=87=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/top/fineplug/splitfile/CutDemo.java | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/top/fineplug/splitfile/CutDemo.java diff --git a/src/top/fineplug/splitfile/CutDemo.java b/src/top/fineplug/splitfile/CutDemo.java new file mode 100644 index 0000000..a3544ce --- /dev/null +++ b/src/top/fineplug/splitfile/CutDemo.java @@ -0,0 +1,56 @@ +package top.fineplug.splitfile; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + + +public class CutDemo { + public static void main(String[] args) { + //调用cutFile()函数 传人参数分别为 (原大文件,切割后存放的小文件的路径,切割规定的内存大小) + cutFile("D:\\java\\cut\\ForrestGump.avi", "D:\\java\\cuts",1024 * 1024 * 20); + } + + private static void cutFile(String src, String endsrc, int num) { + FileInputStream fis = null; + File file = null; + try { + fis = new FileInputStream(src); + file = new File(src); + //创建规定大小的byte数组 + byte[] b = new byte[num]; + int len = 0; + //name为以后的小文件命名做准备 + int name = 1; + //遍历将大文件读入byte数组中,当byte数组读满后写入对应的小文件中 + while ((len = fis.read(b)) != -1) { + //分别找到原大文件的文件名和文件类型,为下面的小文件命名做准备 + String name2 = file.getName(); + int lastIndexOf = name2.lastIndexOf("."); + String substring = name2.substring(0, lastIndexOf); + String substring2 = name2.substring(lastIndexOf, name2.length()); + FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2); + //将byte数组写入对应的小文件中 + fos.write(b, 0, len); + //结束资源 + fos.close(); + name++; + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fis != null) { + //结束资源 + fis.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file