Skip to content

Commit 4e31033

Browse files
committed
代码整理、补充注释
1 parent 60f1f20 commit 4e31033

31 files changed

+441
-328
lines changed

base/process.txt

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ jdk8.SaySomething
22
proxy.ForumServiceImpl
33
proxy.Service2
44
proxy.LoadTest
5-
stack.Stack
5+
other.StackTrace
66
invoke.Impl
77
invoke.MethodHandleTest
88
Jfame_text.PaintOrderTest
@@ -19,11 +19,11 @@ processor.MyProcessor
1919
processor.Main
2020
other.UnsafeTest
2121
Jfame_text.DNDFileTest
22-
thread.ThreadLocalTest
22+
other.ThreadLocalTest
2323
jdk8.SayBye
2424
socket_text.Client
2525
stack.Test
26-
nio.NIO2Test
26+
nio.AsyncChannelTest
2727
nio.NIO2Server
2828
proxy.ForumService
2929
processor.ProcessorClass
@@ -43,8 +43,8 @@ nio.NIO2Client
4343
other.Over
4444
Jfame_text.CustomWindow
4545
Jfame_text.j_02
46-
nio.NIOTest
47-
codeAndDecode.EncodeAndDecode
46+
nio.BufferTest
47+
codeAndDecode.CipherTest
4848
Jfame_text.MyLabel
4949
Jfame_text.Label
5050
invoke.A
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package codeAndDecode;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Base64;
7+
8+
public class Base64Test {
9+
10+
private final String SRC = "Test";
11+
12+
@Test
13+
void base64() {
14+
// 编码
15+
byte[] encode = Base64.getEncoder().encode(SRC.getBytes());
16+
System.out.println("encode=" + new String(encode) + ", length=" + encode.length);
17+
// 解码
18+
byte[] decode = Base64.getDecoder().decode(encode);
19+
Assertions.assertArrayEquals(SRC.getBytes(), decode);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package codeAndDecode;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import javax.crypto.BadPaddingException;
7+
import javax.crypto.Cipher;
8+
import javax.crypto.KeyGenerator;
9+
import javax.crypto.spec.SecretKeySpec;
10+
import java.security.SecureRandom;
11+
12+
@SuppressWarnings({"SameParameterValue", "FieldCanBeLocal"})
13+
public class CipherTest {
14+
15+
private final String SRC = "Test";
16+
private final String KEY = "key";
17+
18+
@Test
19+
void aes() throws Exception {
20+
test("AES", 128);
21+
}
22+
23+
@Test
24+
void aes2() throws Exception {
25+
test("AES", 256);
26+
}
27+
28+
@Test
29+
void des() throws Exception {
30+
test("DES", 56);
31+
}
32+
33+
@Test
34+
void desEde() throws Exception {
35+
test("DESede", 168);
36+
}
37+
38+
private void test(String algorithm, int keySize) throws Exception {
39+
byte[] encode = encode(SRC, KEY, algorithm, keySize);
40+
System.out.printf("code=%s, length=%d\n", new String(encode), encode.length);
41+
42+
byte[] decode = decode(encode, KEY, algorithm, keySize);
43+
Assertions.assertArrayEquals(SRC.getBytes(), decode, "not equals");
44+
45+
Assertions.assertThrows(BadPaddingException.class, () -> decode(encode, "KEY", algorithm, keySize), "false key");
46+
}
47+
48+
/**
49+
* 加密
50+
*/
51+
byte[] encode(String context, String key, String algorithm, int keySize) throws Exception {
52+
SecretKeySpec keySpec = getKey(keySize, algorithm, key.getBytes());
53+
System.out.println("encode = " + keySpec.hashCode());
54+
55+
// 加密
56+
Cipher cipher = Cipher.getInstance(algorithm);
57+
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
58+
System.out.println("block size=" + cipher.getBlockSize());
59+
return cipher.doFinal(context.getBytes());
60+
}
61+
62+
/**
63+
* 解密
64+
*/
65+
byte[] decode(byte[] context, String key, String algorithm, int keySize) throws Exception {
66+
SecretKeySpec keySpec = getKey(keySize, algorithm, key.getBytes());
67+
System.out.println("decode = " + keySpec.hashCode());
68+
69+
// 解密
70+
Cipher cipher = Cipher.getInstance(algorithm);
71+
cipher.init(Cipher.DECRYPT_MODE, keySpec);
72+
System.out.println("block size=" + cipher.getBlockSize());
73+
return cipher.doFinal(context);
74+
}
75+
76+
@Test
77+
void keyTest() throws Exception {
78+
Assertions.assertArrayEquals(
79+
getKey(128, "AES", "AES".getBytes()).getEncoded(),
80+
getKey(128, "AES", "AES".getBytes()).getEncoded());
81+
}
82+
83+
/**
84+
* 生成密钥
85+
* 采用传入密钥为随机算法种子生成实际密钥,可规避不同加密算法要求密钥长度不同
86+
*/
87+
private SecretKeySpec getKey(int keySize, String algorithm, byte[] key) throws Exception {
88+
// 密钥生成器
89+
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
90+
// 随机数算法
91+
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
92+
// 种子
93+
secureRandom.setSeed(key);
94+
// 生成
95+
keyGenerator.init(keySize, secureRandom);
96+
return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), algorithm);
97+
}
98+
99+
// @Test
100+
// void nio() throws Exception {
101+
// // fail
102+
// ByteBuffer src = ByteBuffer.allocate(64);
103+
// src.put("Test1".getBytes());
104+
// src.flip();
105+
// System.out.println("src=" + src);
106+
//
107+
// SecretKeySpec encodeKeySpec = getKey(256, "AES", KEY.getBytes());
108+
// Cipher encodeCipher = Cipher.getInstance("AES");
109+
// encodeCipher.init(Cipher.ENCRYPT_MODE, encodeKeySpec);
110+
//
111+
// ByteBuffer encode = ByteBuffer.allocate(128);
112+
// System.out.println("encode=" + encodeCipher.update(src, encode));
113+
// System.out.println("encode result=" + encode);
114+
// encodeCipher.doFinal();
115+
// System.out.println("encode final=" + encode);
116+
// encode.flip();
117+
//
118+
// SecretKeySpec decodeKeySpec = getKey(256, "AES", KEY.getBytes());
119+
// Cipher decodeCipher = Cipher.getInstance("AES");
120+
// decodeCipher.init(Cipher.DECRYPT_MODE, decodeKeySpec);
121+
//
122+
// ByteBuffer decode = ByteBuffer.allocate(64);
123+
// System.out.println("decode=" + decodeCipher.update(encode, decode));
124+
// System.out.println("getOutputSize=" + decodeCipher.getOutputSize(64));
125+
// decodeCipher.doFinal();
126+
//
127+
// System.out.println(decode);
128+
// decode.flip();
129+
// byte[] data = new byte[decode.limit()];
130+
// decode.get(data);
131+
// System.out.println("decode result=" + new String(data));
132+
// }
133+
134+
}

base/src/main/java/codeAndDecode/EncodeAndDecode.java

-131
This file was deleted.

base/src/main/java/jdk17/InstanceOf.java renamed to base/src/main/java/feature/jdk17/InstanceOf.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk17;
1+
package feature.jdk17;
22

33
public class InstanceOf {
44

base/src/main/java/jdk17/Record.java renamed to base/src/main/java/feature/jdk17/Record.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk17;
1+
package feature.jdk17;
22

33
public class Record {
44

base/src/main/java/jdk17/Sealed.java renamed to base/src/main/java/feature/jdk17/Sealed.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk17;
1+
package feature.jdk17;
22

33
/**
44
* 加sealed必须有子类

base/src/main/java/jdk17/Switch.java renamed to base/src/main/java/feature/jdk17/Switch.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk17;
1+
package feature.jdk17;
22

33
public class Switch {
44

base/src/main/java/jdk17/TextBlock.java renamed to base/src/main/java/feature/jdk17/TextBlock.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk17;
1+
package feature.jdk17;
22

33
public class TextBlock {
44

base/src/main/java/jdk21/SwitchPattern.java renamed to base/src/main/java/feature/jdk21/SwitchPattern.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk21;
1+
package feature.jdk21;
22

33
public class SwitchPattern {
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package feature.jdk21;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.TimeUnit;
7+
8+
public class Thread {
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
long start = System.currentTimeMillis();
12+
CountDownLatch latch = new CountDownLatch(10_000);
13+
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
14+
for (int i = 0; i < 10_000; i++) {
15+
executor.submit(() -> {
16+
try {
17+
TimeUnit.SECONDS.sleep(1);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
} finally {
21+
latch.countDown();
22+
}
23+
});
24+
}
25+
}
26+
latch.await();
27+
// 略大于1000
28+
System.out.println(System.currentTimeMillis() - start);
29+
}
30+
31+
}

base/src/main/java/jdk8/Say.java renamed to base/src/main/java/feature/jdk8/Say.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package jdk8;
1+
package feature.jdk8;
22

33
import java.util.function.Supplier;
44

0 commit comments

Comments
 (0)