-
Notifications
You must be signed in to change notification settings - Fork 2
/
BytebufPractise.java
188 lines (147 loc) · 5.17 KB
/
BytebufPractise.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.cmcc.syw.practise;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
import java.util.Random;
/**
* 练习Bytebuf的API使用
* <p>
* Created by sunyiwei on 2016/11/23.
*/
public class BytebufPractise {
public static void main(String[] args) {
//sequential
sequentialRW();
//random
randomRW();
//index op
indexOp();
//view op
viewOp();
//others
others();
}
private static void others() {
ByteBuf byteBuf = init(26);
//通过array获取底层的数组,并修改某个位置的值
changeFromByteArr(byteBuf);
//通过byteBuffer获取nio的byteBuffer对象,并修改某个位置的值
changeFromByteBuffer(byteBuf);
}
private static void changeFromByteBuffer(ByteBuf byteBuf) {
ByteBuffer byteBuffer = byteBuf.nioBuffer(0, byteBuf.capacity());
byte newValue = '&';
int index = 15;
byteBuffer.put(index, newValue);
//会同时改变这个bytebuf相应位置的值
assert byteBuffer.get(index) == newValue;
assert byteBuf.getByte(index) == newValue;
}
private static void changeFromByteArr(ByteBuf byteBuf) {
byte[] array = byteBuf.array();
int index = 5;
array[index] = 'z'; //改变这个数组的某个值
//会同时改变这个bytebuf相应位置的值
assert array[index] == 'z';
assert byteBuf.getByte(index) == 'z';
}
private static void viewOp() {
ByteBuf bytebuf = init(26);
//duplicate view
int index = 5;
ByteBuf duplicate = bytebuf.duplicate();
duplicate.setByte(5, 'z');
assert 'z' == bytebuf.getByte(5);
//slice view, 拥有独立的指针,但共享同一份内容
int subLength = 10;
ByteBuf slice = bytebuf.slice(0, subLength);
slice.clear(); //默认slice视图时,默认的readerIndex = 0, writerIndex = length, 即无法写入的状态
for (int i = 0; i < subLength; i++) {
slice.writeByte('&');
}
//bytebuf的内容也被修改了
for (int i = 0; i < subLength; i++) {
assert bytebuf.getByte(i) == '&';
assert slice.getByte(i) == '&';
}
//指针是独立的
assert bytebuf.readerIndex() == 0;
assert bytebuf.readableBytes() == 0;
assert slice.readableBytes() == subLength;
assert slice.writableBytes() == 0;
}
private static void indexOp() {
int length = 26;
ByteBuf byteBuf = Unpooled.buffer(length);
//顺序写入
Random r = new Random();
for (int i = 0; i < length; i++) {
byteBuf.writeByte((byte) (r.nextInt(26) + 'a'));
}
assert byteBuf.writableBytes() == 0;
assert byteBuf.readerIndex() == 0;
//clear前先mark一下
byteBuf.markReaderIndex();
byteBuf.markWriterIndex();
//clear
byteBuf.clear();
assert byteBuf.writerIndex() == 0;
assert byteBuf.readerIndex() == 0;
//reset操作会将之前的指针恢复
byteBuf.resetReaderIndex();
byteBuf.resetWriterIndex();
assert byteBuf.writableBytes() == 0;
assert byteBuf.readerIndex() == 0;
}
private static void sequentialRW() {
int length = 26;
ByteBuf byteBuf = Unpooled.buffer(length);
//顺序写入
Random r = new Random();
for (int i = 0; i < length; i++) {
byteBuf.writeByte((byte) (r.nextInt(26) + 'a'));
}
//当前已经无法再写入了
assert byteBuf.writableBytes() == 0;
assert byteBuf.readerIndex() == 0;
//顺序读出
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append((char) byteBuf.readByte());
}
//当前已经无法再读取了
assert byteBuf.readableBytes() == 0;
assert byteBuf.readerIndex() == byteBuf.writerIndex();
System.out.println(sb);
}
private static void randomRW() {
int length = 26;
ByteBuf byteBuf = Unpooled.buffer(length);
//随机写入
for (int i = 0; i < length; i++) {
byteBuf.setByte(i, (byte) (i + 'a'));
}
//不影响ByteBuf中指标的位置
assert byteBuf.readableBytes() == 0;
assert byteBuf.readerIndex() == 0;
final int COUNT = 100;
StringBuilder stringBuilder = new StringBuilder();
Random r = new Random();
for (int i = 0; i < COUNT; i++) {
stringBuilder.append((char) byteBuf.getByte(r.nextInt(length)));
}
//不影响ByteBuf中指标的位置
assert byteBuf.readableBytes() == 0;
assert byteBuf.readerIndex() == 0;
System.out.println(byteBuf.toString());
System.out.println(stringBuilder);
}
private static ByteBuf init(int length) {
ByteBuf byteBuf = Unpooled.buffer(length);
//随机写入
for (int i = 0; i < length; i++) {
byteBuf.setByte(i, (byte) (i + 'a'));
}
return byteBuf;
}
}