-
Notifications
You must be signed in to change notification settings - Fork 668
/
DirectCharBufferU.java
370 lines (297 loc) · 16.2 KB
/
DirectCharBufferU.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio;
import jdk.internal.ref.Cleaner;
import sun.nio.ch.DirectBuffer;
import java.lang.ref.Reference;
// 可读写、直接缓冲区,采用与平台字节顺序相同的字节序,其他部分与DirectCharBufferS相同
class DirectCharBufferU extends CharBuffer implements DirectBuffer {
// Cached unaligned-access capability
protected static final boolean UNALIGNED = Bits.unaligned();
// Cached array base offset
private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(char[].class);
// Base address, used in all indexing calculations
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress protected long address;
// An object attached to this buffer.
// If this buffer is a view of another buffer then we use this field to keep a reference to that buffer to ensure that its memory isn't freed before we are done with it.
private final Object att; // 指向母体缓冲区
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
// 用于#duplicates和#slices
DirectCharBufferU(DirectBuffer db, int mark, int pos, int lim, int cap, int off) {
super(mark, pos, lim, cap);
address = db.address() + off; // 新缓冲区【绝对】起始地址 = 旧缓冲区的【绝对】起始地址+新缓冲区相对于旧缓冲区的位移
att = db;
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 可读写/直接 ████████████████████████████████████████████████████████████████████████████████┓ */
// 只读/可读写
public boolean isReadOnly() {
return false;
}
// 直接缓冲区/非直接缓冲区
public boolean isDirect() {
return true;
}
/*▲ 可读写/直接 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 创建新缓冲区,新旧缓冲区共享内部的存储容器 ████████████████████████████████████████████████████████████████████████████████┓ */
// 切片,截取旧缓冲区的【活跃区域】,作为新缓冲区的【原始区域】。两个缓冲区标记独立。
public CharBuffer slice() {
int pos = this.position();
int lim = this.limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
int off = (pos << 1); // 每个char是两个字节,所以这里要乘以2
assert (off >= 0);
return new DirectCharBufferU(this, -1, 0, rem, rem, off);
}
// 副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
public CharBuffer duplicate() {
return new DirectCharBufferU(this, this.markValue(), this.position(), this.limit(), this.capacity(), 0);
}
// 只读副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
public CharBuffer asReadOnlyBuffer() {
return new DirectCharBufferRU(this, this.markValue(), this.position(), this.limit(), this.capacity(), 0);
}
// 子副本,新缓冲区的【活跃区域】取自旧缓冲区【活跃区域】的[start,end)部分
public CharBuffer subSequence(int start, int end) {
int pos = position();
int lim = limit();
assert (pos <= lim);
pos = (pos <= lim ? pos : lim);
int len = lim - pos;
if((start < 0) || (end > len) || (start > end))
throw new IndexOutOfBoundsException();
return new DirectCharBufferU(this, -1, pos + start, pos + end, capacity(), offset);
}
/*▲ 创建新缓冲区,新旧缓冲区共享内部的存储容器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ get/读取 ████████████████████████████████████████████████████████████████████████████████┓ */
// 读取position处的char,然后递增position。
public char get() {
try {
// 直接从本地内存中访问
return (UNSAFE.getChar(ix(nextGetIndex())));
} finally {
Reference.reachabilityFence(this);
}
}
// 读取i处的char(有越界检查)
public char get(int i) {
try {
return ((UNSAFE.getChar(ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
}
// 读取i处的char(无越界检查)
char getUnchecked(int i) {
try {
return ((UNSAFE.getChar(ix(i))));
} finally {
Reference.reachabilityFence(this);
}
}
// 复制当前缓存区的length个元素到dst数组offset索引处
public CharBuffer get(char[] dst, int offset, int length) {
// 复制的元素数量超过某个性能阙值才执行此段
if(((long) length << 1) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
checkBounds(offset, length, dst.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if(length > rem)
throw new BufferUnderflowException();
long dstOffset = ARRAY_BASE_OFFSET + ((long) offset << 1);
try {
// null参数代表直接从内存地址拷贝
if(order() != ByteOrder.nativeOrder()) { // 缓冲区与本地字节序不一致,拷贝字节时涉及到大小端转换
UNSAFE.copySwapMemory(null, ix(pos), dst, dstOffset, (long) length << 1, (long) 1 << 1);
} else { // 缓冲区与本地字节序一致,直接拷贝
UNSAFE.copyMemory(null, ix(pos), dst, dstOffset, (long) length << 1);
}
} finally {
Reference.reachabilityFence(this);
}
// 更新当前缓冲区的position
position(pos + length);
} else {
// 不值当用上面那一堆方法
super.get(dst, offset, length);
}
return this;
}
/*▲ get/读取 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ put/写入 ████████████████████████████████████████████████████████████████████████████████┓ */
// 向position处写入char,并将position递增
public CharBuffer put(char x) {
try {
UNSAFE.putChar(ix(nextPutIndex()), x);
} finally {
Reference.reachabilityFence(this);
}
return this;
}
// 向i处写入char
public CharBuffer put(int i, char x) {
try {
UNSAFE.putChar(ix(checkIndex(i)), ((x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
}
// 将源缓冲区src的内容全部写入到当前缓冲区
public CharBuffer put(CharBuffer src) {
if(src instanceof DirectCharBufferU) {
if(src == this)
throw createSameBufferException();
DirectCharBufferU sb = (DirectCharBufferU) src;
int spos = sb.position();
int slim = sb.limit();
assert (spos <= slim);
int srem = (spos <= slim ? slim - spos : 0);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if(srem > rem)
throw new BufferOverflowException();
try {
UNSAFE.copyMemory(sb.ix(spos), ix(pos), (long) srem << 1);
} finally {
Reference.reachabilityFence(sb);
Reference.reachabilityFence(this);
}
sb.position(spos + srem);
position(pos + srem);
} else if(src.hb != null) { // 不同类型的缓冲区
int spos = src.position();
int slim = src.limit();
assert (spos <= slim);
int srem = (spos <= slim ? slim - spos : 0);
put(src.hb, src.offset + spos, srem);
src.position(spos + srem);
} else {
super.put(src);
}
return this;
}
// 从源字符数组src的offset处开始,复制length个元素,写入到当前缓冲区【活跃区域】内(考虑偏移量)
public CharBuffer put(char[] src, int offset, int length) {
if(((long) length << 1) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
checkBounds(offset, length, src.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if(length > rem)
throw new BufferOverflowException();
long srcOffset = ARRAY_BASE_OFFSET + ((long) offset << 1);
try {
if(order() != ByteOrder.nativeOrder()) {
UNSAFE.copySwapMemory(src, srcOffset, null, ix(pos), (long) length << 1, (long) 1 << 1);
} else {
UNSAFE.copyMemory(src, srcOffset, null, ix(pos), (long) length << 1);
}
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.put(src, offset, length);
}
return this;
}
/*▲ put/写入 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 压缩 ████████████████████████████████████████████████████████████████████████████████┓ */
// 压缩缓冲区,将当前未读完的数据挪到容器起始处,可用于读模式到写模式的切换,但又不丢失之前读入的数据。
public CharBuffer compact() {
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
try {
UNSAFE.copyMemory(ix(pos), ix(0), (long) rem << 1);
} finally {
Reference.reachabilityFence(this);
}
position(rem);
limit(capacity());
discardMark();
return this;
}
/*▲ 压缩 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 字节顺序 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回与平台字节顺序相同的字节序
public ByteOrder order() {
return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN) ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
}
// 返回‘char’的字节序(此类中与平台字节序相同)
ByteOrder charRegionOrder() {
return order();
}
/*▲ 字节顺序 ████████████████████████████████████████████████████████████████████████████████┛ */
// 返回内部存储结构的引用(一般用于非直接缓存区)
@Override
Object base() {
return null;
}
// 返回索引i处的元素的<地址>
private long ix(int i) {
// 转换为本地内存地址
return address + ((long) i << 1);
}
// 构造新的子串
public String toString(int start, int end) {
if((end > limit()) || (start > end))
throw new IndexOutOfBoundsException();
try {
int len = end - start;
char[] ca = new char[len];
CharBuffer cb = CharBuffer.wrap(ca);
CharBuffer db = this.duplicate();
db.position(start);
db.limit(end);
cb.put(db);
return new String(ca);
} catch(StringIndexOutOfBoundsException x) {
throw new IndexOutOfBoundsException();
}
}
/*▼ 实现DirectBuffer接口 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回直接缓冲区的起始<地址>
public long address() {
return address;
}
// 返回指向母体缓冲区的引用
public Object attachment() {
return att;
}
// 返回该缓冲区的清理器
public Cleaner cleaner() {
return null;
}
/*▲ 实现DirectBuffer接口 ████████████████████████████████████████████████████████████████████████████████┛ */
}