diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 8ce8da9..daf424f 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -9,7 +9,7 @@ - + diff --git a/src/test/java/com/zerroi/netty/chapter01/TestByteBufferExercise.java b/src/test/java/com/zerroi/netty/chapter01/TestByteBufferExercise.java new file mode 100644 index 0000000..e5ac262 --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestByteBufferExercise.java @@ -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(); + } +} diff --git a/src/test/java/com/zerroi/netty/chapter01/TestFileChannelTransferTo.java b/src/test/java/com/zerroi/netty/chapter01/TestFileChannelTransferTo.java new file mode 100644 index 0000000..01d8f35 --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestFileChannelTransferTo.java @@ -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)); + } + } +} diff --git a/src/test/java/com/zerroi/netty/chapter01/TestFileCopy.java b/src/test/java/com/zerroi/netty/chapter01/TestFileCopy.java new file mode 100644 index 0000000..883a1be --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestFileCopy.java @@ -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); + } + }); + } +} diff --git a/src/test/java/com/zerroi/netty/chapter01/TestFilesWalkFileTree.java b/src/test/java/com/zerroi/netty/chapter01/TestFilesWalkFileTree.java new file mode 100644 index 0000000..0943d38 --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestFilesWalkFileTree.java @@ -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); + } +} diff --git a/src/test/java/com/zerroi/netty/chapter01/TestGatheringWrites.java b/src/test/java/com/zerroi/netty/chapter01/TestGatheringWrites.java new file mode 100644 index 0000000..caec5ee --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestGatheringWrites.java @@ -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) { + } + } +} diff --git a/src/test/java/com/zerroi/netty/chapter01/TestScatteringReads.java b/src/test/java/com/zerroi/netty/chapter01/TestScatteringReads.java new file mode 100644 index 0000000..b80d21b --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter01/TestScatteringReads.java @@ -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)); + } + } +} diff --git a/src/test/java/com/zerroi/netty/chapter02/Client.java b/src/test/java/com/zerroi/netty/chapter02/Client.java new file mode 100644 index 0000000..63a92da --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter02/Client.java @@ -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"); + } +} diff --git a/src/test/java/com/zerroi/netty/chapter02/Server.java b/src/test/java/com/zerroi/netty/chapter02/Server.java new file mode 100644 index 0000000..31fa399 --- /dev/null +++ b/src/test/java/com/zerroi/netty/chapter02/Server.java @@ -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 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); + } + } + } + } + } +} diff --git a/words.txt b/words.txt new file mode 100644 index 0000000..f523005 --- /dev/null +++ b/words.txt @@ -0,0 +1 @@ +onetwothree \ No newline at end of file