forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectIntBufferU.java
320 lines (250 loc) · 14.1 KB
/
DirectIntBufferU.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
/*
* 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;
// 可读写、直接缓冲区,采用与平台字节顺序相同的字节序,其他部分与DirectIntBufferS相同
class DirectIntBufferU extends IntBuffer 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(int[].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;
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
// For duplicates and slices
DirectIntBufferU(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 IntBuffer slice() {
int pos = this.position();
int lim = this.limit();
assert (pos<=lim);
int rem = (pos<=lim ? lim - pos : 0);
int off = (pos << 2);
assert (off >= 0);
return new DirectIntBufferU(this, -1, 0, rem, rem, off);
}
// 副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
public IntBuffer duplicate() {
return new DirectIntBufferU(this, this.markValue(), this.position(), this.limit(), this.capacity(), 0);
}
// 只读副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
public IntBuffer asReadOnlyBuffer() {
return new DirectIntBufferRU(this, this.markValue(), this.position(), this.limit(), this.capacity(), 0);
}
/*▲ 创建新缓冲区,新旧缓冲区共享内部的存储容器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ get/读取 ████████████████████████████████████████████████████████████████████████████████┓ */
// 读取position处(可能需要加offset)的int,然后递增position。
public int get() {
try {
return ((UNSAFE.getInt(ix(nextGetIndex()))));
} finally {
Reference.reachabilityFence(this);
}
}
// 读取i处(可能需要加offset)的int(有越界检查)
public int get(int i) {
try {
return ((UNSAFE.getInt(ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
}
// 复制源缓存区的length个元素到dst数组offset索引处
public IntBuffer get(int[] dst, int offset, int length) {
if(((long) length << 2)>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 << 2);
try {
if(order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(null, ix(pos), dst, dstOffset, (long) length << 2, (long) 1 << 2);
else
UNSAFE.copyMemory(null, ix(pos), dst, dstOffset, (long) length << 2);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.get(dst, offset, length);
}
return this;
}
/*▲ get/读取 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ put/写入 ████████████████████████████████████████████████████████████████████████████████┓ */
// 向position处(可能需要加offset)写入int,并将position递增
public IntBuffer put(int x) {
try {
UNSAFE.putInt(ix(nextPutIndex()), ((x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
}
// 向i处(可能需要加offset)写入int
public IntBuffer put(int i, int x) {
try {
UNSAFE.putInt(ix(checkIndex(i)), ((x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
}
// 将源缓冲区src的内容全部写入到当前缓冲区
public IntBuffer put(IntBuffer src) {
if(src instanceof DirectIntBufferU) {
if(src == this)
throw createSameBufferException();
DirectIntBufferU sb = (DirectIntBufferU) 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 << 2);
} 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;
}
// 从源int数组src的offset处开始,复制length个元素,写入到当前缓冲区
public IntBuffer put(int[] src, int offset, int length) {
if(((long) length << 2)>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 << 2);
try {
if(order() != ByteOrder.nativeOrder())
UNSAFE.copySwapMemory(src, srcOffset, null, ix(pos), (long) length << 2, (long) 1 << 2);
else
UNSAFE.copyMemory(src, srcOffset, null, ix(pos), (long) length << 2);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.put(src, offset, length);
}
return this;
}
/*▲ put/写入 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 压缩 ████████████████████████████████████████████████████████████████████████████████┓ */
// 压缩缓冲区,将当前未读完的数据挪到容器起始处,可用于读模式到写模式的切换,但又不丢失之前读入的数据。
public IntBuffer 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 << 2);
} 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);
}
/*▲ 字节顺序 ████████████████████████████████████████████████████████████████████████████████┛ */
// 返回内部存储结构的引用(一般用于非直接缓存区)
@Override
Object base() {
return null;
}
private long ix(int i) {
return address + ((long) i << 2);
}
/*▼ 实现DirectBuffer接口 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回直接缓冲区的【绝对】起始<地址>
public long address() {
return address;
}
// 返回附件,一般是指母体缓冲区的引用
public Object attachment() {
return att;
}
// 返回该缓冲区的清理器
public Cleaner cleaner() {
return null;
}
/*▲ 实现DirectBuffer接口 ████████████████████████████████████████████████████████████████████████████████┛ */
}