提交 a890e8b4 authored 作者: xuyang's avatar xuyang

BIO/NIO

上级 b8eb229f
......@@ -9,20 +9,12 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
</properties>
<groupId>com.xy</groupId>
<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>
\ No newline at end of file
......@@ -9,8 +9,11 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<java.version>1.8</java.version>
</properties>
<groupId>com.xy</groupId>
<artifactId>api</artifactId>
</project>
\ No newline at end of file
......@@ -116,14 +116,12 @@ public class IoTest {
}
}
}
class IoTestMain{
public static void main(String[] args) {
//IoTest.getTextFile();
//IoTest.writeTextFile();
//IoTest.copyTextFile("D:/a.txt","D:/b.txt");
IoTest.inputStreamReader("D:/a.txt");
}
}
package api.io.bio;
import lombok.Data;
@Data
public class Msg {
private String ipAddr;
private String port;
private String msg;
private int msgLength;
}
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();
}
}
}
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();
}
}
}
package api.io.nio;
import lombok.Data;
@Data
public class NMsg {
private String ipAddr;
private String port;
private String msg;
private int msgLength;
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
......@@ -11,15 +11,16 @@ public class IteratorTest {
for(int i = 0; i < 10; i++){
list.add(i);
}
Iterator<Integer> iterator = list.iterator();
list.add(1);
/*Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
Integer num = iterator.next();
if(num == 0) {
iterator.remove();
}
}
}*/
System.out.println(list.toString());
System.out.println(list.indexOf(1));
}
......
......@@ -5,10 +5,37 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test1 {
public String regexReplace(String htmlStr) {
/**
* \w 匹配字母、数字、下划线
* \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 img = "";
......@@ -31,21 +58,36 @@ public class Test1 {
if (i.matches(regexp1)) {
Pattern p = Pattern.compile(regexp1);
Matcher matcher = p.matcher(i);
matcher.find();
String partName = matcher.group(1);
htmlStr = htmlStr.replaceFirst(i, localUrl2 + partName);
if(matcher.find()) {
String partName = matcher.group(1);
htmlStr = htmlStr.replace(i, localUrl2 + partName);
}
}
}
}
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) {
//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\" />";
Test1 test1 = new Test1();
System.out.println(test1.regexReplace(htmlStr));
//System.out.println(new RegexTest1().regexReplace());
new RegexTest1().regex_v1();
}
}
......@@ -4,6 +4,10 @@
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>
<properties>
<java.version>1.8</java.version>
</properties>
<groupId>com.xy</groupId>
<artifactId>projects</artifactId>
<packaging>pom</packaging>
......@@ -13,5 +17,30 @@
<module>api</module>
</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>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论