Skip to content

Commit 7cc4ba1

Browse files
author
yongjie.zhang@ucarinc.com
committed
stream操作
1 parent 8ec49db commit 7cc4ba1

File tree

17 files changed

+751
-0
lines changed

17 files changed

+751
-0
lines changed

java-base.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<exclude-output />
55
<content url="file://$MODULE_DIR$">
66
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/java-classloader/src" isTestSource="false" />
78
</content>
89
<orderEntry type="inheritedJdk" />
910
<orderEntry type="sourceFolder" forTests="false" />

java-classloader/src/com/zyj/test/Demo02.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,12 @@ public static void main(String[] args) throws ClassNotFoundException {
2121
System.out.println(c2.hashCode());
2222
System.out.println(c3.hashCode());
2323
//JVM认为被同一个加载器加载的同一个类是相同的类
24+
System.out.println(c.getClassLoader());
25+
System.out.println(c1.getClassLoader());
26+
System.out.println(c2.getClassLoader());
27+
System.out.println(c3.getClassLoader());
28+
NetClassLoader netClassLoader = new NetClassLoader("d:/myjava");
29+
Class c5 = netClassLoader.findClass("com.zyj.test.HelloWorld");
30+
System.out.println(c5.getName());
2431
}
2532
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.zyj.test;
2+
3+
/**
4+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
5+
*
6+
* @author : 张勇杰
7+
* @date : 2018/12/14 14:54
8+
* @Version : v1.0
9+
* @description
10+
**/
11+
public class Demo04 {
12+
public static void main(String[] args) throws ClassNotFoundException {
13+
//int a = 3;
14+
//异或操作,。
15+
// System.out.println(Integer.toBinaryString(a^0xff));
16+
FileSystemClassLoader loader = new FileSystemClassLoader("d:/myjava/temp");
17+
Class<?> c = loader.loadClass("HelloWorld");
18+
System.out.println(c);
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.zyj.test;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
7+
*
8+
* @author : 张勇杰
9+
* @date : 2018/12/14 15:01
10+
* @Version : v1.0
11+
* @description 加密工具类
12+
**/
13+
public class EncrptUtil {
14+
public static void main(String[] args) {
15+
encrpt(new File("d://myjava/HelloWorld.class"),new File("d://myjava/temp/HelloWorld.class"));
16+
}
17+
public static void encrpt(File src,File dest){
18+
FileInputStream fis = null;
19+
FileOutputStream fos = null;
20+
try {
21+
fis = new FileInputStream(src);
22+
fos = new FileOutputStream(dest);
23+
int temp = -1;
24+
while((temp = fis.read())!=-1){
25+
fos.write(temp^0xff);
26+
}
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}finally {
30+
try {
31+
if(fos!= null){
32+
fos.close();
33+
}
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
37+
}
38+
39+
}
40+
}
41+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.zyj.test;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.net.URL;
8+
9+
/**
10+
* 版权声明:CopyRight (c) 2018 ucarinc. All Rights Reserved.
11+
*
12+
* @author : 张勇杰
13+
* @date : 2018/12/14 14:30
14+
* @Version : v1.0
15+
* @description
16+
**/
17+
public class NetClassLoader extends ClassLoader{
18+
//d:/myjava/com/zyj/test/User.class -->com.zyj.test.User
19+
private String rootUrl;
20+
21+
public NetClassLoader(String rootUrl){
22+
this.rootUrl = rootUrl;
23+
}
24+
25+
@Override
26+
protected Class<?> findClass(String name) throws ClassNotFoundException {
27+
Class<?> c = findLoadedClass(name);
28+
29+
//应该先查询有没有加载过这个类,如果已经加载,则返回已经加载好的类。如果没有,则加载新的类
30+
if(c != null){
31+
return c;
32+
}else{
33+
34+
if(c != null){
35+
return c;
36+
}else{
37+
byte[] classData = getClassData(name);
38+
if(classData == null){
39+
throw new ClassNotFoundException();
40+
}else{
41+
c = defineClass(name,classData,0,classData.length);
42+
}
43+
}
44+
}
45+
return c;
46+
}
47+
48+
private byte[] getClassData(String className){
49+
String path = rootUrl + "/"+className.replace(".","/")+".class";
50+
51+
//IOUtils,可以使用他将流中的数据转成字节数组
52+
InputStream is = null;
53+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
54+
try {
55+
URL url = new URL(path);
56+
is = url.openStream();
57+
byte[] buffer = new byte[1024];
58+
int temp = 0;
59+
while((temp = is.read(buffer))!= -1){
60+
baos.write(buffer,0,temp);
61+
}
62+
return baos.toByteArray();
63+
} catch (Exception e) {
64+
e.printStackTrace();
65+
return null;
66+
}finally {
67+
if(is != null){
68+
try {
69+
is.close();
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
}
73+
}
74+
if (baos != null) {
75+
76+
try {
77+
baos.close();
78+
} catch (IOException e) {
79+
e.printStackTrace();
80+
}
81+
}
82+
}
83+
}
84+
}

java-jvm/java-jvm.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.asher.learn.jvm;
2+
3+
/**
4+
*
5+
* @author : 张勇杰
6+
* @date : 2019/3/11 10:58
7+
* @Version : v1.0
8+
* @description
9+
* VM Args: -Xss 128K
10+
**/
11+
public class StackLeafOOF {
12+
private int stackLength = 1;
13+
private void stackLeaf() {
14+
stackLength++;
15+
stackLeaf();
16+
}
17+
18+
public static void main(String[] args) {
19+
StackLeafOOF oof = new StackLeafOOF();
20+
try {
21+
oof.stackLeaf();
22+
}catch (Throwable e){
23+
System.out.println("stack length:"+oof.stackLength);
24+
throw e;
25+
}
26+
}
27+
28+
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.asher.stream.test;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
import java.util.function.Predicate;
8+
import java.util.function.UnaryOperator;
9+
import java.util.stream.Stream;
10+
11+
/**
12+
*
13+
* @author : 张勇杰
14+
* @date : 2019/7/2 18:02
15+
* @Version : v1.0
16+
* @description
17+
**/
18+
public class ConsumerTest {
19+
public static void main(String[] args) {
20+
// Consumer f = System.out::println;
21+
// Consumer f2 = (n) -> System.out.println("666"+n);
22+
// f.andThen(f2).andThen(f2).andThen(f2).accept("zhang");
23+
// f2.accept("1");
24+
25+
26+
// List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
27+
// ArrayList<Integer> collect = list.stream().filter(n -> n > 10 && n<15).collect(() -> new ArrayList<>(),
28+
// (list1, item) -> list1.add(item), (list2, list1) -> list2.addAll(list1));
29+
// collect.forEach(System.out::print);
30+
// Stream.iterate(1, new Predicate<Integer>() {
31+
// @Override
32+
// public boolean test(Integer integer) {
33+
// return integer <= 10;
34+
// }
35+
// }, new UnaryOperator<Integer>() {
36+
// @Override
37+
// public Integer apply(Integer integer) {
38+
// return integer*2;
39+
// }
40+
// }).forEach(System.out::println);
41+
Stream.iterate(1, n->n<10,n->n*3).forEach(System.out::println);
42+
}
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.asher.stream.test;
2+
3+
import java.util.Date;
4+
5+
public interface DemoInterface {
6+
default void now(){
7+
System.out.println(new Date());
8+
}
9+
}

0 commit comments

Comments
 (0)