diff --git a/src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java b/src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java deleted file mode 100644 index 5e0ab3443cf8818f56cb762a260506cfb3cec4f8..0000000000000000000000000000000000000000 --- a/src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.kwan.shuyu.heima; - -/** - * 分割数据 - * - * @author : qinyingjie - * @version : 2.2.0 - * @date : 2023/4/18 17:15 - */ -public class ByteBuffer_03_split { - public static void main(String[] args) { - } -} diff --git a/src/main/java/com/kwan/shuyu/heima/ByteBuffer_01_Write.java b/src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_12_Test_Write.java similarity index 91% rename from src/main/java/com/kwan/shuyu/heima/ByteBuffer_01_Write.java rename to src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_12_Test_Write.java index 99233b17c1f1c1bc294b194ef12e596ee4034a34..ed871c389928c867cf79b57af8b91cff10a1e5ea 100644 --- a/src/main/java/com/kwan/shuyu/heima/ByteBuffer_01_Write.java +++ b/src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_12_Test_Write.java @@ -1,4 +1,4 @@ -package com.kwan.shuyu.heima; +package com.kwan.shuyu.heima.netty_01_bytebuffer; import java.io.RandomAccessFile; import java.nio.ByteBuffer; @@ -12,7 +12,7 @@ import java.nio.charset.StandardCharsets; * @version : 2.2.0 * @date : 2023/4/18 17:15 */ -public class ByteBuffer_01_Write { +public class ByteBuffer_12_Test_Write { public static void main(String[] args) { final ByteBuffer b1 = StandardCharsets.UTF_8.encode("hello"); final ByteBuffer b2 = StandardCharsets.UTF_8.encode("world"); diff --git a/src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java b/src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java new file mode 100644 index 0000000000000000000000000000000000000000..c97332753ca9494b84854c583a59c8e78b5d0217 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java @@ -0,0 +1,40 @@ +package com.kwan.shuyu.heima.netty_01_bytebuffer; + +import java.nio.ByteBuffer; + +import static com.kwan.shuyu.until.ByteBufferUtil.debugAll; + +/** + * 分割数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class ByteBuffer_13_Test_split { + public static void main(String[] args) { + final 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(); + final int limit = source.limit(); + for (int i = 0; i < limit; i++) { + //找到完整的消息 + if (source.get(i) == '\n') { + final int len = i + 1 - source.position(); + final ByteBuffer target = ByteBuffer.allocate(len); + for (int j = 0; j < len; j++) { + //从source读取,向target写入 + target.put(source.get()); + } + debugAll(target); + } + } + source.compact(); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java new file mode 100644 index 0000000000000000000000000000000000000000..7a7c7a4727047b49aaabb1b2edc1bef2946f1f6e --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java @@ -0,0 +1,22 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * 创建目录 + * 如果目录已存在,会抛异常FileAlreadyExistsException + * 不能一次创建多级目录,否则会抛异常NoSuchFileException + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_02_Test_create { + public static void main(String[] args) throws IOException { + final Path path = Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/1111"); + Files.createDirectories(path); + } +} diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_03_Test_create_more.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_03_Test_create_more.java new file mode 100644 index 0000000000000000000000000000000000000000..9b15a8bc84d198cb7bd763d0017fd51543db29ce --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_03_Test_create_more.java @@ -0,0 +1,22 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * 创建多级目录 + * 如果目录已存在,会抛异常FileAlreadyExistsException + * 不能一次创建多级目录,否则会抛异常NoSuchFileException + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_03_Test_create_more { + public static void main(String[] args) throws IOException { + final Path path = Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/222/333"); + Files.createDirectories(path); + } +} diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_04_Test_transferTo.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_04_Test_transferTo.java new file mode 100644 index 0000000000000000000000000000000000000000..5e067bf85cb12ba2dd880af27c5ff67cf50d0198 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_04_Test_transferTo.java @@ -0,0 +1,31 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; + +/** + * 将一个文件中的数据复制到另一个文件,只传一次 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_04_Test_transferTo { + public static void main(String[] args) { + long start = System.nanoTime(); + try (final FileChannel from = new FileInputStream("data.txt").getChannel(); + final FileChannel to = new FileOutputStream("to.txt").getChannel(); + ) { + final long size = from.size(); + //效率高,底层会利用操作系统的季零拷贝进行优化 + from.transferTo(0, size, to); + + } catch (IOException e) { + e.printStackTrace(); + } + long end = System.nanoTime(); + System.out.println("transferTo 用时:" + (end - start) / 1000_000.0); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_05_Test_transferTo.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_05_Test_transferTo.java new file mode 100644 index 0000000000000000000000000000000000000000..c8063141d0acb05b438f5b9a87e9f7c68461f5ab --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_05_Test_transferTo.java @@ -0,0 +1,29 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; + +/** + * 将一个文件中的数据复制到另一个文件 传多次 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_05_Test_transferTo { + public static void main(String[] args) { + try (final FileChannel from = new FileInputStream("data.txt").getChannel(); + final FileChannel to = new FileOutputStream("to.txt").getChannel(); + ) { + final long size = from.size(); + //left代表还剩多少字节 + for (long left = size; left > 0; ) { + left -= from.transferTo((size - left), left, to); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java new file mode 100644 index 0000000000000000000000000000000000000000..b4b82776e9569e5b4c7793b88ae0a2226ed97af4 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java @@ -0,0 +1,26 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +/** + * 将一个文件中的数据复制到另一个文件 传多次 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_06_Test_copy { + public static void main(String[] args) { + try { + Path source = Paths.get("data.txt"); + Path target = Paths.get("to.txt"); + Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java new file mode 100644 index 0000000000000000000000000000000000000000..cecd62d7d4efb72109012c9a547575e37a395571 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java @@ -0,0 +1,35 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_07_Test_walkFileTree { + public static void main(String[] args) throws IOException { + final AtomicInteger dirCount = new AtomicInteger(); + final AtomicInteger fileCount = new AtomicInteger(); + Files.walkFileTree(Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo"), new SimpleFileVisitor() { + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + System.out.println("==========>>>" + dir); + dirCount.incrementAndGet(); + return super.preVisitDirectory(dir, attrs); + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + System.out.println("==========>>>" + file); + fileCount.incrementAndGet(); + return super.visitFile(file, attrs); + } + }); + System.out.println("文件夹数量=" + dirCount); + System.out.println("文件数量=" + fileCount); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java new file mode 100644 index 0000000000000000000000000000000000000000..5370ea1fcae675e9217e3527876134e883680740 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java @@ -0,0 +1,28 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_08_Test_jar { + public static void main(String[] args) throws IOException { + final AtomicInteger jarCount = new AtomicInteger(); + Files.walkFileTree(Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo"), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.toString().endsWith(".jar")) { + System.out.println("==========>>>" + file); + jarCount.incrementAndGet(); + } + return super.visitFile(file, attrs); + } + }); + System.out.println("jar包数量=" + jarCount); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java new file mode 100644 index 0000000000000000000000000000000000000000..8aebdd25f816b14a9d3b5430884821ae2111de1a --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java @@ -0,0 +1,28 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +/** + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_09_Test_delete { + public static void main(String[] args) throws IOException { + Files.walkFileTree(Paths.get("/Users/qinyingjie/Downloads/ConcurrntHashMap扩容源码分析_副本"), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return super.visitFile(file, attrs); + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + Files.delete(dir); + return super.postVisitDirectory(dir, exc); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java new file mode 100644 index 0000000000000000000000000000000000000000..12299e19df9028ea344ad5a220558ba1b31d10c3 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java @@ -0,0 +1,32 @@ +package com.kwan.shuyu.heima.netty_02_file; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +/** + * 拷贝一个文件夹中的数据到另一个文件夹(有子文件夹) + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 17:15 + */ +public class File_10_Test_copy { + public static void main(String[] args) throws IOException { + String from = "/Users/qinyingjie/Documents/idea-workspace/netty-demo"; + String to = "/Users/qinyingjie/Downloads/未命名文件夹"; + Files.walk(Paths.get(from)).forEach(path -> { + try { + String targetName = path.toString().replace(from, to); + if (Files.isDirectory(path)) { + Files.createDirectories(Paths.get(targetName)); + } + if (Files.isRegularFile(path)) { + Files.copy(path, Paths.get(targetName)); + } + } catch (Exception e) { + e.printStackTrace(); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServerAttachment.java b/src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServerAttachment.java new file mode 100644 index 0000000000000000000000000000000000000000..870a1dab61c6e78e711c6e840478324ff5da2878 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServerAttachment.java @@ -0,0 +1,78 @@ +package com.kwan.shuyu.heima.netty_03_nio.nio_03_selector; + +import com.kwan.shuyu.until.ByteBufferUtil; +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; + +/** + * NioServer 服务端 NioServerAttachment 附件传递 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/18 18:22 + */ +@Slf4j +public class NioServerAttachment { + public static void main(String[] args) throws IOException { + final Selector selector = Selector.open(); + //预设ByteBuffer,并分配空间,为了读取数据 + final ByteBuffer buffer = ByteBuffer.allocate(16); + //创建ServerSocketChannel + final ServerSocketChannel ssc = ServerSocketChannel.open(); + //绑定端口号 + ssc.configureBlocking(false); + //ServerSocketChannel注册到Selector中 + final SelectionKey sscKey = ssc.register(selector, 0, null); + sscKey.interestOps(SelectionKey.OP_ACCEPT); + log.info("sscKey={}", sscKey); + ssc.bind(new InetSocketAddress(8080)); + + while (true) { + //select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行 + //select在事件未处理时,它不会阻塞,会一直请求处理 + selector.select(); + final Iterator iterator = selector.selectedKeys().iterator(); + while (iterator.hasNext()) { + final SelectionKey key = iterator.next(); + iterator.remove(); + log.info("key={}", key); + if (key.isAcceptable()) { + final ServerSocketChannel channel = (ServerSocketChannel) key.channel(); + final SocketChannel sc = channel.accept(); + sc.configureBlocking(false); + ByteBuffer bf = ByteBuffer.allocate(16); + //将一个ByteBuffer作为附件绑定到SelectionKey + final SelectionKey scKey = sc.register(selector, 0, bf); + scKey.interestOps(SelectionKey.OP_READ); + log.info("sc={}", sc); + log.info("scKey={}", scKey); + } else if (key.isReadable()) { + try { + final SocketChannel channel = (SocketChannel) key.channel(); + //获取附件的ByteBuffer + final ByteBuffer bf = (ByteBuffer) key.attachment(); + final int read = channel.read(bf); + if (read == -1){ + + }else{ + + } + bf.flip(); + ByteBufferUtil.debugRead(bf); + } catch (IOException e) { + e.printStackTrace(); + key.cancel();//取消事件处理 + } + } + } + } + } +} \ No newline at end of file diff --git a/to.txt b/to.txt new file mode 100644 index 0000000000000000000000000000000000000000..5b7d1cb02bcf34124927493c5d631a3142730104 --- /dev/null +++ b/to.txt @@ -0,0 +1 @@ +1234567890abc \ No newline at end of file