Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 59ee2dc

Browse filesBrowse files
committed
[A] 添加Java零拷贝实现case
1 parent 0db96f3 commit 59ee2dc
Copy full SHA for 59ee2dc

File tree

Expand file treeCollapse file tree

2 files changed

+62
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+62
-4
lines changed
Open diff view settings
Collapse file

‎JdkLearn/src/main/java/com/learnjava/io/nio/MappedByteBufferTest.java‎

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/io/nio/MappedByteBufferTest.java
+48-3Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.learnjava.io.nio;
22

3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
36
import java.io.RandomAccessFile;
47
import java.nio.MappedByteBuffer;
58
import java.nio.channels.FileChannel;
@@ -17,13 +20,55 @@ public static void main(String[] args) throws Exception {
1720

1821
MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
1922

20-
mappedByteBuffer.put(0, (byte)'H');
21-
mappedByteBuffer.put(3, (byte)'9');
23+
mappedByteBuffer.put(0, (byte) 'H');
24+
mappedByteBuffer.put(3, (byte) '9');
2225

2326
// IndexOutOfBoundsException
24-
mappedByteBuffer.put(5, (byte)'Y');
27+
mappedByteBuffer.put(5, (byte) 'Y');
2528

2629
randomAccessFile.close();
2730
System.out.println("change success");
2831
}
32+
33+
/**
34+
*
35+
* @param from
36+
* @param to
37+
* @throws IOException
38+
*/
39+
public static void mmap4zeroCopy(String from, String to) throws IOException {
40+
FileChannel source = null;
41+
FileChannel destination = null;
42+
try {
43+
source = new RandomAccessFile(from, "r").getChannel();
44+
destination = new RandomAccessFile(to, "rw").getChannel();
45+
MappedByteBuffer inMappedBuf =
46+
source.map(FileChannel.MapMode.READ_ONLY, 0, source.size());
47+
destination.write(inMappedBuf);
48+
} finally {
49+
if (source != null) {
50+
source.close();
51+
}
52+
if (destination != null) {
53+
destination.close();
54+
}
55+
}
56+
}
57+
58+
public static void sendfile4zeroCopy(String from, String to) throws IOException{
59+
FileChannel source = null;
60+
FileChannel destination = null;
61+
try {
62+
source = new FileInputStream(from).getChannel();
63+
destination = new FileOutputStream(to).getChannel();
64+
source.transferTo(0, source.size(), destination);
65+
} finally {
66+
if (source != null) {
67+
source.close();
68+
}
69+
if (destination != null) {
70+
destination.close();
71+
}
72+
}
73+
}
2974
}
Collapse file

‎note/Netty/零拷贝.md‎

Copy file name to clipboardExpand all lines: note/Netty/零拷贝.md
+14-1Lines changed: 14 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@
44

55
## 正文
66

7-
### 1. mmap和sendFile零拷贝
7+
### 1. 再看IO
8+
9+
在深入零拷贝机制之前,先来了解下传统BIO通信底层发生了什么,为什么会这么“消耗资源”。Linux服务器是现在绝大多数系统的首选,它的优点就不再赘述,下面的分析都基于Linux环境来进行。作为一台服务器,最常见的功能就是
10+
获取客户端发送过来的请求,然后再去查数据库DB获取到想要的数据,再将数据以一定的业务逻辑处理后传回给客户端,这一过程主要会调用Linux内核的以下两个函数:
11+
12+
```
13+
read(file, tmp_buf, len);
14+
15+
write(socket, tmp_buf, len);
16+
```
17+
18+
19+
20+
### 2. mmap和sendFile零拷贝
821

922
在Java程序中,常用的零拷贝有mmap(内存映射)和sendFile。让我们回到IO底层,在Java中,一次IO操作在操作系统底层都做了哪些工作。
1023

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.