feat(Netty学习第一天): NO
This commit is contained in:
parent
578b0f553b
commit
6abf985ba2
|
@ -9,7 +9,7 @@
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
<!-- 日志级别 -->
|
<!-- 日志级别 -->
|
||||||
<root level="INFO">
|
<root level="DEBUG">
|
||||||
<appender-ref ref="CONSOLE" />
|
<appender-ref ref="CONSOLE" />
|
||||||
</root>
|
</root>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
import static com.zerroi.netty.chapter01.ByteBufferUtil.debugAll;
|
||||||
|
|
||||||
|
public class TestByteBufferExercise {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ByteBuffer source = ByteBuffer.allocate(32);
|
||||||
|
source.put("Hello,world\nI'm zhangsan\nHo".getBytes());
|
||||||
|
split(source);
|
||||||
|
source.put("w are you?\n".getBytes());
|
||||||
|
split(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void split(ByteBuffer source) {
|
||||||
|
source.flip();
|
||||||
|
for (int i = 0; i < source.limit(); i++) {
|
||||||
|
if (source.get(i) == '\n') {
|
||||||
|
int length = i - source.position() + 1;
|
||||||
|
ByteBuffer target = ByteBuffer.allocate(length);
|
||||||
|
for (int j = 0; j < length; j++) {
|
||||||
|
target.put(source.get());
|
||||||
|
}
|
||||||
|
debugAll(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
source.compact();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class TestFileChannelTransferTo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (
|
||||||
|
FileChannel fromChannel = new FileInputStream("text.txt").getChannel();
|
||||||
|
FileChannel toChannel = new FileOutputStream("to.txt").getChannel()
|
||||||
|
) {
|
||||||
|
// 效率更高,利用操作系统的零拷贝最大2g数据
|
||||||
|
long size = fromChannel.size();
|
||||||
|
for (long left = size; left > 0;) {
|
||||||
|
left -= fromChannel.transferTo(size - left, left, toChannel);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.info(String.valueOf(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
public class TestFileCopy {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
String source = "D:\\jetbra";
|
||||||
|
String target = "D:\\jetbra123";
|
||||||
|
Files.walk(Paths.get(source)).forEach(path -> {
|
||||||
|
try {
|
||||||
|
String targetPath = path.toString().replace(source, target);
|
||||||
|
if (Files.isDirectory(path)) {
|
||||||
|
Files.createDirectory(Paths.get(targetPath));
|
||||||
|
} else if (Files.isRegularFile(path)){
|
||||||
|
Files.copy(path, Paths.get(targetPath));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
public class TestFilesWalkFileTree {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Files.walkFileTree(Paths.get("D:\\Drivers"), new SimpleFileVisitor<>() {
|
||||||
|
@Override
|
||||||
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
|
System.out.println("进入dir==>" + dir);
|
||||||
|
return super.preVisitDirectory(dir, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
System.out.println("访问文件==>" + file);
|
||||||
|
Files.delete(file);
|
||||||
|
return super.visitFile(file, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||||
|
System.out.println("<==退出" + dir);
|
||||||
|
Files.delete(dir);
|
||||||
|
return super.postVisitDirectory(dir, exc);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void m2() throws IOException {
|
||||||
|
AtomicInteger count = new AtomicInteger();
|
||||||
|
Files.walkFileTree(Paths.get("D:\\Bandizip"), new SimpleFileVisitor<>() {
|
||||||
|
@Override
|
||||||
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
if (file.toString().endsWith(".dll")) {
|
||||||
|
System.out.println(file);
|
||||||
|
count.incrementAndGet();
|
||||||
|
}
|
||||||
|
return super.visitFile(file, attrs);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
System.out.println(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void m1() throws IOException {
|
||||||
|
AtomicInteger dirCount = new AtomicInteger();
|
||||||
|
AtomicInteger fileCount = new AtomicInteger();
|
||||||
|
Files.walkFileTree(Paths.get("D:\\Bandizip"), new SimpleFileVisitor<>() {
|
||||||
|
@Override
|
||||||
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
|
System.out.println("dir ===> " + dir);
|
||||||
|
dirCount.incrementAndGet();
|
||||||
|
return super.preVisitDirectory(dir, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
System.out.println("file ===> " + file);
|
||||||
|
fileCount.incrementAndGet();
|
||||||
|
return super.visitFile(file, attrs);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
System.out.println("dirCount = " + dirCount);
|
||||||
|
System.out.println("fileCount = " + fileCount);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class TestGatheringWrites {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ByteBuffer b1 = StandardCharsets.UTF_8.encode("hello");
|
||||||
|
ByteBuffer b2 = StandardCharsets.UTF_8.encode("world");
|
||||||
|
ByteBuffer b3 = StandardCharsets.UTF_8.encode("你好");
|
||||||
|
|
||||||
|
try (FileChannel channel = new RandomAccessFile("words2.txt", "rw").getChannel()) {
|
||||||
|
channel.write(new ByteBuffer[]{b1, b2, b3});
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.zerroi.netty.chapter01;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
|
||||||
|
import static com.zerroi.netty.chapter01.ByteBufferUtil.debugAll;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class TestScatteringReads {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (FileChannel channel = new RandomAccessFile("words.txt", "r").getChannel()) {
|
||||||
|
ByteBuffer b1 = ByteBuffer.allocate(3);
|
||||||
|
ByteBuffer b2 = ByteBuffer.allocate(3);
|
||||||
|
ByteBuffer b3 = ByteBuffer.allocate(5);
|
||||||
|
channel.read(new ByteBuffer[]{b1, b2, b3});
|
||||||
|
b1.flip();
|
||||||
|
b2.flip();
|
||||||
|
b3.flip();
|
||||||
|
|
||||||
|
debugAll(b1);
|
||||||
|
debugAll(b2);
|
||||||
|
debugAll(b3);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.info(String.valueOf(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.zerroi.netty.chapter02;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
SocketChannel socketChannel = SocketChannel.open();
|
||||||
|
|
||||||
|
socketChannel.connect(new InetSocketAddress("localhost", 8080));
|
||||||
|
System.out.println("Waiting");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
package com.zerroi.netty.chapter02;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.SelectionKey;
|
||||||
|
import java.nio.channels.Selector;
|
||||||
|
import java.nio.channels.ServerSocketChannel;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import static com.zerroi.netty.chapter01.ByteBufferUtil.debugRead;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class Server {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Selector selector = Selector.open();
|
||||||
|
|
||||||
|
ServerSocketChannel ssc = ServerSocketChannel.open();
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocate(16);
|
||||||
|
ssc.bind(new InetSocketAddress(8080));
|
||||||
|
ssc.configureBlocking(false);
|
||||||
|
|
||||||
|
SelectionKey sscKey = ssc.register(selector, 0, null);
|
||||||
|
sscKey.interestOps(SelectionKey.OP_ACCEPT);
|
||||||
|
|
||||||
|
log.info("REGISTER KEY:{}", sscKey);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// 没有事件发生,线程阻塞,有事件线程恢复运行 ,事件发生后要么处理要么取消不能不管,不处理就不会阻塞一直循环
|
||||||
|
selector.select();
|
||||||
|
|
||||||
|
// 处理事件,selectedKeys包含了所有发生的事件
|
||||||
|
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
SelectionKey key = iterator.next();
|
||||||
|
// 处理key时,要从 selectedKeys集合中删除,否则下次处理会出现异常,因为本次已经将sscKey处理过了下次取获取的时候去accept()时返回的是空已经处理过了
|
||||||
|
iterator.remove();
|
||||||
|
log.info("KEY:{}", key);
|
||||||
|
// 区分事件类型
|
||||||
|
if (key.isAcceptable()) { // 如果是accept事件
|
||||||
|
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
|
||||||
|
SocketChannel sc = channel.accept();
|
||||||
|
sc.configureBlocking(false);
|
||||||
|
SelectionKey scKey = sc.register(selector, 0, null);
|
||||||
|
scKey.interestOps(SelectionKey.OP_READ);
|
||||||
|
log.info("SERVER SOCKET : {}", channel);
|
||||||
|
} else if (key.isReadable()) {
|
||||||
|
try {
|
||||||
|
SocketChannel channel = (SocketChannel) key.channel();
|
||||||
|
int read = channel.read(buffer);
|
||||||
|
if (read == -1) {
|
||||||
|
key.cancel();
|
||||||
|
}
|
||||||
|
buffer.flip();
|
||||||
|
debugRead(buffer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
key.cancel();
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue