# kNetty **Repository Path**: coder_ami/k-netty ## Basic Information - **Project Name**: kNetty - **Description**: Netty框架浅学 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-06-07 - **Last Updated**: 2024-06-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Netty #### 1. ByteBuffer使用关键 - flip() 切换为读模式 - clear() 切换为写模式 - compact() 抛弃已经读了的数据,position放置到未读数据末尾,切换为写模式 - rewind() 重置position=0,重头开始读写 ##### 1.1 分类 - HeapByteBuffer: 堆内存,读写效率低:收到GC影响;会多1次拷贝 - DirectByteBuffer:直接内存,读写效率高,不会收到GC影响;但是分配内存的速度比较慢,使用不当,会造成内存泄漏 ##### 1.2 mark & reset > mark 是标记当前position的位置;reset则是重置当前position到mark位置。 ##### 1.3 字符串和ByteBuffer相互转换 - Charset.forName("utf-8"),写入后依旧是写模式 - ByteBuffer buffer = StandardCharsets.UTF_8.encode("hello") , 会在写入后自动切换为读模式 - wrap , 会在写入后自动切换为读模式 - String s = StandardCharsets.UTF_8.decode(buffer).toString() ##### 1.4 scattering read - FileChannelObj.read(ByteBuffer[] bfs): 将数据读取到多个buffer中 ##### 1.5 gathering write - FileChannelObj.write(ByteBuffer[] bfs): 将多个buffer写入文件 #### 2. FileChannel ##### 2.1 transferTo/transferFrom: 1次最多传输2G数据,超过2G的文件,会分多次传输 > 效率高,底层会利用OS的零拷贝技术实现 #### 3. Path & Paths - Paths.get(filePath) - path.normalize(): 路径不会带有./ or ../ #### 4. Files - Files.exists - Files.createDirectory - Files.createDirectories: 创建多级目录 - Files.copy(source, target) - Files.move(source, target) - Files.delete: 删除文件或者删除空目录 - Files.walkFileTree: 实现 遍历/删除/复制 多级目录 #### 5. nio ##### 5.1 blocking ##### 5.2 non-blocking #### 6. multiplexing ![](C:/Users/15305/AppData/Roaming/Typora/typora-user-images/inet-socket.png) #### zero-copy tech ![](C:\Users\15305\AppData\Roaming\Typora\typora-user-images\zero-copy.png)