Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
personal-project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
xuyang
personal-project
Commits
a890e8b4
提交
a890e8b4
authored
6月 17, 2020
作者:
xuyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
BIO/NIO
上级
b8eb229f
隐藏空白字符变更
内嵌
并排
正在显示
13 个修改的文件
包含
357 行增加
和
33 行删除
+357
-33
pom.xml
algorithm/pom.xml
+4
-13
pom.xml
api/pom.xml
+4
-2
IoTest.java
api/src/main/java/api/io/IoTest.java
+2
-4
Msg.java
api/src/main/java/api/io/bio/Msg.java
+16
-0
SocketClient.java
api/src/main/java/api/io/bio/SocketClient.java
+37
-0
SocketServer.java
api/src/main/java/api/io/bio/SocketServer.java
+54
-0
NMsg.java
api/src/main/java/api/io/nio/NMsg.java
+16
-0
NSocketClient.java
api/src/main/java/api/io/nio/NSocketClient.java
+40
-0
NSocketClient2.java
api/src/main/java/api/io/nio/NSocketClient2.java
+40
-0
NSocketServer.java
api/src/main/java/api/io/nio/NSocketServer.java
+57
-0
IteratorTest.java
api/src/main/java/api/iterator/IteratorTest.java
+4
-3
RegexTest1.java
api/src/main/java/api/regex/RegexTest1.java
+53
-11
pom.xml
pom.xml
+30
-0
没有找到文件。
algorithm/pom.xml
浏览文件 @
a890e8b4
...
@@ -9,20 +9,12 @@
...
@@ -9,20 +9,12 @@
</parent>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<java.version>
1.8
</java.version>
</properties>
<groupId>
com.xy
</groupId>
<groupId>
com.xy
</groupId>
<artifactId>
algorithm
</artifactId>
<artifactId>
algorithm
</artifactId>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
8
</source>
<target>
8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
api/pom.xml
浏览文件 @
a890e8b4
...
@@ -9,8 +9,11 @@
...
@@ -9,8 +9,11 @@
</parent>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<java.version>
1.8
</java.version>
</properties>
<groupId>
com.xy
</groupId>
<groupId>
com.xy
</groupId>
<artifactId>
api
</artifactId>
<artifactId>
api
</artifactId>
</project>
</project>
\ No newline at end of file
api/src/main/java/api/io/IoTest.java
浏览文件 @
a890e8b4
...
@@ -116,14 +116,12 @@ public class IoTest {
...
@@ -116,14 +116,12 @@ public class IoTest {
}
}
}
}
}
class
IoTestMain
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
//IoTest.getTextFile();
//IoTest.getTextFile();
//IoTest.writeTextFile();
//IoTest.writeTextFile();
//IoTest.copyTextFile("D:/a.txt","D:/b.txt");
//IoTest.copyTextFile("D:/a.txt","D:/b.txt");
IoTest
.
inputStreamReader
(
"D:/a.txt"
);
IoTest
.
inputStreamReader
(
"D:/a.txt"
);
}
}
}
}
api/src/main/java/api/io/bio/Msg.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
bio
;
import
lombok.Data
;
@Data
public
class
Msg
{
private
String
ipAddr
;
private
String
port
;
private
String
msg
;
private
int
msgLength
;
}
api/src/main/java/api/io/bio/SocketClient.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
bio
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.Socket
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Scanner
;
public
class
SocketClient
{
public
static
void
main
(
String
[]
args
)
{
try
{
Socket
socket
=
new
Socket
(
"127.0.0.1"
,
8080
);
String
txt
=
""
;
while
(!
txt
.
equals
(
"end"
))
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
txt
=
scanner
.
next
();
Msg
msg
=
new
Msg
();
msg
.
setIpAddr
(
"111.111.111"
);
msg
.
setPort
(
"8081"
);
msg
.
setMsg
(
txt
);
msg
.
setMsgLength
(
txt
.
length
());
String
txtJson
=
JSON
.
toJSONString
(
msg
);
socket
.
getOutputStream
().
write
(
txtJson
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
socket
.
close
();
System
.
out
.
println
(
"客户端断开连接"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
api/src/main/java/api/io/bio/SocketServer.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
bio
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 在不使用多线程的情况下,无法处理并发
*/
public
class
SocketServer
{
public
static
void
main
(
String
[]
args
)
{
try
{
ServerSocket
serverSocket
=
new
ServerSocket
();
serverSocket
.
bind
(
new
InetSocketAddress
(
"127.0.0.1"
,
8080
));
System
.
out
.
println
(
"wait conn"
);
//阻塞,放弃CPU资源,直到有连接使用
Socket
socket
=
serverSocket
.
accept
();
System
.
out
.
println
(
"conn success"
);
while
(
true
)
{
byte
[]
bytes
=
new
byte
[
1024
];
System
.
out
.
println
(
"wait data"
);
//阻塞,read读了多少字节
int
read
=
socket
.
getInputStream
().
read
(
bytes
);
System
.
out
.
println
(
"data success"
);
String
msgJsonText
=
new
String
(
bytes
,
StandardCharsets
.
UTF_8
);
Msg
msg
=
JSON
.
parseObject
(
msgJsonText
,
Msg
.
class
);
System
.
out
.
println
(
msg
.
getMsg
());
if
(
msg
.
getMsg
().
equals
(
"end"
)){
break
;
}
}
serverSocket
.
close
();
System
.
out
.
println
(
"服务端断开连接"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
api/src/main/java/api/io/nio/NMsg.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
nio
;
import
lombok.Data
;
@Data
public
class
NMsg
{
private
String
ipAddr
;
private
String
port
;
private
String
msg
;
private
int
msgLength
;
}
api/src/main/java/api/io/nio/NSocketClient.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
nio
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.InetAddress
;
import
java.net.Socket
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Scanner
;
public
class
NSocketClient
{
public
static
void
main
(
String
[]
args
)
{
try
{
Socket
socket
=
new
Socket
(
"127.0.0.1"
,
8080
);
String
txt
=
""
;
InetAddress
addr
=
InetAddress
.
getLocalHost
();
Scanner
scanner
=
new
Scanner
(
System
.
in
);
while
(!
txt
.
equals
(
"end"
))
{
txt
=
scanner
.
next
();
Msg
msg
=
new
Msg
();
msg
.
setIpAddr
(
addr
.
getHostAddress
());
msg
.
setPort
(
"8081"
);
msg
.
setMsg
(
txt
);
msg
.
setMsgLength
(
txt
.
length
());
String
txtJson
=
JSON
.
toJSONString
(
msg
);
socket
.
getOutputStream
().
write
(
txtJson
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
socket
.
close
();
System
.
out
.
println
(
"客户端断开连接"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
api/src/main/java/api/io/nio/NSocketClient2.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
nio
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.InetAddress
;
import
java.net.Socket
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Scanner
;
public
class
NSocketClient2
{
public
static
void
main
(
String
[]
args
)
{
try
{
Socket
socket
=
new
Socket
(
"127.0.0.1"
,
8080
);
String
txt
=
""
;
InetAddress
addr
=
InetAddress
.
getLocalHost
();
Scanner
scanner
=
new
Scanner
(
System
.
in
);
while
(!
txt
.
equals
(
"end"
))
{
txt
=
scanner
.
next
();
Msg
msg
=
new
Msg
();
msg
.
setIpAddr
(
addr
.
getHostAddress
());
msg
.
setPort
(
"8081"
);
msg
.
setMsg
(
txt
);
msg
.
setMsgLength
(
txt
.
length
());
String
txtJson
=
JSON
.
toJSONString
(
msg
);
socket
.
getOutputStream
().
write
(
txtJson
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
socket
.
close
();
System
.
out
.
println
(
"客户端断开连接"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
api/src/main/java/api/io/nio/NSocketServer.java
0 → 100644
浏览文件 @
a890e8b4
package
api
.
io
.
nio
;
import
api.io.bio.Msg
;
import
com.alibaba.fastjson.JSON
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.net.ServerSocket
;
import
java.net.Socket
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
NSocketServer
{
private
static
List
<
SocketChannel
>
socketChannelList
=
new
ArrayList
<>();
public
static
void
main
(
String
[]
args
)
{
try
{
ServerSocketChannel
serverSocketChannel
=
ServerSocketChannel
.
open
();
serverSocketChannel
.
bind
(
new
InetSocketAddress
(
"127.0.0.1"
,
8080
));
serverSocketChannel
.
configureBlocking
(
false
);
while
(
true
)
{
//是否有连接
SocketChannel
socketChannel
=
serverSocketChannel
.
accept
();
if
(
socketChannel
==
null
)
{
Thread
.
sleep
(
1000
);
//System.out.println("no conn");
}
else
{
System
.
out
.
println
(
socketChannel
.
getRemoteAddress
()
+
" conn success"
);
socketChannel
.
configureBlocking
(
false
);
socketChannelList
.
add
(
socketChannel
);
}
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
(
512
);
for
(
SocketChannel
socketChannelObj
:
socketChannelList
)
{
int
num
=
socketChannelObj
.
read
(
byteBuffer
);
if
(
num
>
0
)
{
byteBuffer
.
flip
();
NMsg
nMsg
=
JSON
.
parseObject
(
Charset
.
forName
(
"utf-8"
).
decode
(
byteBuffer
).
toString
(),
NMsg
.
class
);
System
.
out
.
println
(
nMsg
.
getIpAddr
()
+
":"
+
nMsg
.
getMsg
());
}
}
}
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
api/src/main/java/api/iterator/IteratorTest.java
浏览文件 @
a890e8b4
...
@@ -11,15 +11,16 @@ public class IteratorTest {
...
@@ -11,15 +11,16 @@ public class IteratorTest {
for
(
int
i
=
0
;
i
<
10
;
i
++){
for
(
int
i
=
0
;
i
<
10
;
i
++){
list
.
add
(
i
);
list
.
add
(
i
);
}
}
Iterator
<
Integer
>
iterator
=
list
.
iterator
();
list
.
add
(
1
);
/*Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
while(iterator.hasNext()) {
Integer num = iterator.next();
Integer num = iterator.next();
if(num == 0) {
if(num == 0) {
iterator.remove();
iterator.remove();
}
}
}
}
*/
System
.
out
.
println
(
list
.
toString
(
));
System
.
out
.
println
(
list
.
indexOf
(
1
));
}
}
...
...
api/src/main/java/api/regex/Test1.java
→
api/src/main/java/api/regex/
Regex
Test1.java
浏览文件 @
a890e8b4
...
@@ -5,10 +5,37 @@ import java.util.List;
...
@@ -5,10 +5,37 @@ import java.util.List;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
public
class
Test1
{
/**
* \w 匹配字母、数字、下划线
public
String
regexReplace
(
String
htmlStr
)
{
* \W 匹配非字母、数字、下划线
* \s 匹配任意空白字符,相当于[\t\n\r\f]
* \S 匹配任意非空字符
* \d 匹配任意数字,相当于[0-9]
* \D 匹配非数字的字符
* \A 匹配字符串开头
* \Z 匹配字符串结尾,如果存在换行,只匹配到换行前的结束字符串
* \z 匹配字符串结尾,如果存在换行,同时还会匹配换行符
* \G 匹配最后匹配完成的位置
* \n 匹配一个换行符
* \t 匹配一个制表符
* ^ 匹配一行字符串的开头
* $ 匹配一行字符串的结尾
* . 匹配任意字符,除了换行符
* [^…] 不在[]中的字符,比如[^abc]匹配除了a、b、c之外的字符
* * 匹配0个或多个表达式
* + 匹配1个或多个表达式
* ? 匹配0个或1个前面的正则表达式定义的片段,非贪婪方式
* () 匹配括号内的表达式,也表示一个组
* {n} 精确匹配n个前面的表达式,比如\d{n},代表n个数字
* {n,m} 匹配n到m次由前面正则表达式定义的片段,贪婪方式
* alt + Enter
*/
public
class
RegexTest1
{
public
String
regexReplace
()
{
String
htmlStr
=
"<a href=\"http://www.zhejiang.net/图片测试(游).jpg\" />"
;
//String htmlStr = "<a href=\"http://www.zhejiang.net/图片测试(游))))(.jpg\" />";
//String htmlStr = "<a href=\"http://www.zhejiang.net/图片测试<游<.jpg\" />";
String
localUrl2
=
"http://www.xy.regex/"
;
String
localUrl2
=
"http://www.xy.regex/"
;
String
img
=
""
;
String
img
=
""
;
...
@@ -31,21 +58,36 @@ public class Test1 {
...
@@ -31,21 +58,36 @@ public class Test1 {
if
(
i
.
matches
(
regexp1
))
{
if
(
i
.
matches
(
regexp1
))
{
Pattern
p
=
Pattern
.
compile
(
regexp1
);
Pattern
p
=
Pattern
.
compile
(
regexp1
);
Matcher
matcher
=
p
.
matcher
(
i
);
Matcher
matcher
=
p
.
matcher
(
i
);
matcher
.
find
();
if
(
matcher
.
find
())
{
String
partName
=
matcher
.
group
(
1
);
String
partName
=
matcher
.
group
(
1
);
htmlStr
=
htmlStr
.
replaceFirst
(
i
,
localUrl2
+
partName
);
htmlStr
=
htmlStr
.
replace
(
i
,
localUrl2
+
partName
);
}
}
}
}
}
}
}
return
htmlStr
;
return
htmlStr
;
}
}
public
void
regex_v1
()
{
String
str
=
"hhhellollll"
;
String
regex
=
"hello"
;
Pattern
pattern
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
pattern
.
matcher
(
str
);
//System.out.println(matcher.replaceAll("attr"));
System
.
out
.
println
(
matcher
.
find
());
if
(
matcher
.
find
())
{
System
.
out
.
println
(
matcher
.
find
());
System
.
out
.
println
(
matcher
.
group
());
System
.
out
.
println
(
str
.
replaceAll
(
regex
,
"123"
));
System
.
out
.
println
(
str
.
matches
(
regex
));
}
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
//String htmlStr = "<a href=\"http://www.zhejiang.net/图片测试(游).jpg\" />";
//String htmlStr = "<a href=\"http://www.zhejiang.net/图片测试(游).jpg\" />";
//System.out.println(new RegexTest1().regexReplace());
String
htmlStr
=
"<a href=\"http://www.zhejiang.net/图片测试<游<><.jpg\" />"
;
new
RegexTest1
().
regex_v1
();
Test1
test1
=
new
Test1
();
System
.
out
.
println
(
test1
.
regexReplace
(
htmlStr
));
}
}
}
}
pom.xml
浏览文件 @
a890e8b4
...
@@ -4,6 +4,10 @@
...
@@ -4,6 +4,10 @@
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<java.version>
1.8
</java.version>
</properties>
<groupId>
com.xy
</groupId>
<groupId>
com.xy
</groupId>
<artifactId>
projects
</artifactId>
<artifactId>
projects
</artifactId>
<packaging>
pom
</packaging>
<packaging>
pom
</packaging>
...
@@ -13,5 +17,30 @@
...
@@ -13,5 +17,30 @@
<module>
api
</module>
<module>
api
</module>
</modules>
</modules>
<dependencies>
<dependency>
<groupId>
org.projectlombok
</groupId>
<artifactId>
lombok
</artifactId>
<version>
1.16.10
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.70
</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<configuration>
<source>
8
</source>
<target>
8
</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论