1
25
26
27
28 package java.nio;
29
30 import java.io.FileDescriptor;
31 import java.lang.ref.Reference;
32 import jdk.internal.misc.VM;
33 import jdk.internal.ref.Cleaner;
34 import sun.nio.ch.DirectBuffer;
35
36
37 class DirectLongBufferU
38
39 extends LongBuffer
40
41
42
43 implements DirectBuffer
44 {
45
46
47
48
49 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(long[].class);
50
51
52 protected static final boolean UNALIGNED = Bits.unaligned();
53
54
55
56
57
58
59
60
61 private final Object att;
62
63 public Object attachment() {
64 return att;
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 public Cleaner cleaner() { return null; }
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 DirectLongBufferU(DirectBuffer db,
188 int mark, int pos, int lim, int cap,
189 int off)
190 {
191
192 super(mark, pos, lim, cap);
193 address = db.address() + off;
194
195
196
197 att = db;
198
199
200
201
202 }
203
204 @Override
205 Object base() {
206 return null;
207 }
208
209 public LongBuffer slice() {
210 int pos = this.position();
211 int lim = this.limit();
212 assert (pos <= lim);
213 int rem = (pos <= lim ? lim - pos : 0);
214 int off = (pos << 3);
215 assert (off >= 0);
216 return new DirectLongBufferU(this, -1, 0, rem, rem, off);
217 }
218
219
220
221
222
223
224
225
226
227
228 public LongBuffer duplicate() {
229 return new DirectLongBufferU(this,
230 this.markValue(),
231 this.position(),
232 this.limit(),
233 this.capacity(),
234 0);
235 }
236
237 public LongBuffer asReadOnlyBuffer() {
238
239 return new DirectLongBufferRU(this,
240 this.markValue(),
241 this.position(),
242 this.limit(),
243 this.capacity(),
244 0);
245
246
247
248 }
249
250
251
252 public long address() {
253 return address;
254 }
255
256 private long ix(int i) {
257 return address + ((long)i << 3);
258 }
259
260 public long get() {
261 try {
262 return ((UNSAFE.getLong(ix(nextGetIndex()))));
263 } finally {
264 Reference.reachabilityFence(this);
265 }
266 }
267
268 public long get(int i) {
269 try {
270 return ((UNSAFE.getLong(ix(checkIndex(i)))));
271 } finally {
272 Reference.reachabilityFence(this);
273 }
274 }
275
276
277
278
279
280
281
282
283
284
285
286 public LongBuffer get(long[] dst, int offset, int length) {
287
288 if (((long)length << 3) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
289 checkBounds(offset, length, dst.length);
290 int pos = position();
291 int lim = limit();
292 assert (pos <= lim);
293 int rem = (pos <= lim ? lim - pos : 0);
294 if (length > rem)
295 throw new BufferUnderflowException();
296
297 long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << 3);
298 try {
299
300 if (order() != ByteOrder.nativeOrder())
301 UNSAFE.copySwapMemory(null,
302 ix(pos),
303 dst,
304 dstOffset,
305 (long)length << 3,
306 (long)1 << 3);
307 else
308
309 UNSAFE.copyMemory(null,
310 ix(pos),
311 dst,
312 dstOffset,
313 (long)length << 3);
314 } finally {
315 Reference.reachabilityFence(this);
316 }
317 position(pos + length);
318 } else {
319 super.get(dst, offset, length);
320 }
321 return this;
322
323
324
325 }
326
327
328
329 public LongBuffer put(long x) {
330
331 try {
332 UNSAFE.putLong(ix(nextPutIndex()), ((x)));
333 } finally {
334 Reference.reachabilityFence(this);
335 }
336 return this;
337
338
339
340 }
341
342 public LongBuffer put(int i, long x) {
343
344 try {
345 UNSAFE.putLong(ix(checkIndex(i)), ((x)));
346 } finally {
347 Reference.reachabilityFence(this);
348 }
349 return this;
350
351
352
353 }
354
355 public LongBuffer put(LongBuffer src) {
356
357 if (src instanceof DirectLongBufferU) {
358 if (src == this)
359 throw createSameBufferException();
360 DirectLongBufferU sb = (DirectLongBufferU)src;
361
362 int spos = sb.position();
363 int slim = sb.limit();
364 assert (spos <= slim);
365 int srem = (spos <= slim ? slim - spos : 0);
366
367 int pos = position();
368 int lim = limit();
369 assert (pos <= lim);
370 int rem = (pos <= lim ? lim - pos : 0);
371
372 if (srem > rem)
373 throw new BufferOverflowException();
374 try {
375 UNSAFE.copyMemory(sb.ix(spos), ix(pos), (long)srem << 3);
376 } finally {
377 Reference.reachabilityFence(sb);
378 Reference.reachabilityFence(this);
379 }
380 sb.position(spos + srem);
381 position(pos + srem);
382 } else if (src.hb != null) {
383
384 int spos = src.position();
385 int slim = src.limit();
386 assert (spos <= slim);
387 int srem = (spos <= slim ? slim - spos : 0);
388
389 put(src.hb, src.offset + spos, srem);
390 src.position(spos + srem);
391
392 } else {
393 super.put(src);
394 }
395 return this;
396
397
398
399 }
400
401 public LongBuffer put(long[] src, int offset, int length) {
402
403 if (((long)length << 3) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
404 checkBounds(offset, length, src.length);
405 int pos = position();
406 int lim = limit();
407 assert (pos <= lim);
408 int rem = (pos <= lim ? lim - pos : 0);
409 if (length > rem)
410 throw new BufferOverflowException();
411
412 long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << 3);
413 try {
414
415 if (order() != ByteOrder.nativeOrder())
416 UNSAFE.copySwapMemory(src,
417 srcOffset,
418 null,
419 ix(pos),
420 (long)length << 3,
421 (long)1 << 3);
422 else
423
424 UNSAFE.copyMemory(src,
425 srcOffset,
426 null,
427 ix(pos),
428 (long)length << 3);
429 } finally {
430 Reference.reachabilityFence(this);
431 }
432 position(pos + length);
433 } else {
434 super.put(src, offset, length);
435 }
436 return this;
437
438
439
440 }
441
442 public LongBuffer compact() {
443
444 int pos = position();
445 int lim = limit();
446 assert (pos <= lim);
447 int rem = (pos <= lim ? lim - pos : 0);
448 try {
449 UNSAFE.copyMemory(ix(pos), ix(0), (long)rem << 3);
450 } finally {
451 Reference.reachabilityFence(this);
452 }
453 position(rem);
454 limit(capacity());
455 discardMark();
456 return this;
457
458
459
460 }
461
462 public boolean isDirect() {
463 return true;
464 }
465
466 public boolean isReadOnly() {
467 return false;
468 }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516 public ByteOrder order() {
517
518
519
520
521
522 return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
523 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
524
525 }
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544 }
545