13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #warn This file is preprocessed before being compiled
27
28 package java.nio;
29
30 import java.io.FileDescriptor;
31 import java.lang.ref.Reference;
32 import java.util.Objects;
33 import jdk.internal.misc.VM;
34 import jdk.internal.ref.Cleaner;
35 import sun.nio.ch.DirectBuffer;
36
37
38 class Direct$Type$Buffer$RW$$BO$
39 #if[rw]
40 extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
41 #else[rw]
42 extends Direct$Type$Buffer$BO$
43 #end[rw]
44 implements DirectBuffer
45 {
46
47 #if[rw]
48
49 // Cached array base offset
50 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
51
52 // Cached unaligned-access capability
95 }
96
97 private final Cleaner cleaner;
98
99 public Cleaner cleaner() { return cleaner; }
100
101 #else[byte]
102
103 public Cleaner cleaner() { return null; }
104
105 #end[byte]
106
107 #end[rw]
108
109 #if[byte]
110
111 // Primary constructor
112 //
113 Direct$Type$Buffer$RW$(int cap) { // package-private
114 #if[rw]
115 super(-1, 0, cap, cap);
116 boolean pa = VM.isDirectMemoryPageAligned();
117 int ps = Bits.pageSize();
118 long size = Math.max(1L, (long)cap + (pa ? ps : 0));
119 Bits.reserveMemory(size, cap);
120
121 long base = 0;
122 try {
123 base = UNSAFE.allocateMemory(size);
124 } catch (OutOfMemoryError x) {
125 Bits.unreserveMemory(size, cap);
126 throw x;
127 }
128 UNSAFE.setMemory(base, size, (byte) 0);
129 if (pa && (base % ps != 0)) {
130 // Round up to page boundary
131 address = base + ps - (base & (ps - 1));
132 } else {
133 address = base;
134 }
135 cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
136 att = null;
137 #else[rw]
138 super(cap);
139 this.isReadOnly = true;
140 #end[rw]
141 }
142
143 #if[rw]
144
145 // Invoked to construct a direct ByteBuffer referring to the block of
146 // memory. A given arbitrary object may also be attached to the buffer.
147 //
148 Direct$Type$Buffer(long addr, int cap, Object ob) {
149 super(-1, 0, cap, cap);
150 address = addr;
151 cleaner = null;
152 att = ob;
153 }
154
155
156 // Invoked only by JNI: NewDirectByteBuffer(void*, long)
157 //
158 private Direct$Type$Buffer(long addr, int cap) {
159 super(-1, 0, cap, cap);
160 address = addr;
161 cleaner = null;
162 att = null;
163 }
164
165 #end[rw]
166
167 // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
168 //
169 protected Direct$Type$Buffer$RW$(int cap, long addr,
170 FileDescriptor fd,
171 Runnable unmapper,
172 boolean isSync)
173 {
174 #if[rw]
175 super(-1, 0, cap, cap, fd, isSync);
176 address = addr;
177 cleaner = Cleaner.create(this, unmapper);
178 att = null;
179 #else[rw]
180 super(cap, addr, fd, unmapper, isSync);
181 this.isReadOnly = true;
182 #end[rw]
183 }
184
185 #end[byte]
186
187 // For duplicates and slices
188 //
189 Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private
190 int mark, int pos, int lim, int cap,
191 int off)
192 {
193 #if[rw]
194 super(mark, pos, lim, cap);
195 address = db.address() + off;
196 #if[byte]
197 cleaner = null;
198 #end[byte]
199 Object attachment = db.attachment();
200 att = (attachment == null ? db : attachment);
201 #else[rw]
202 super(db, mark, pos, lim, cap, off);
203 this.isReadOnly = true;
204 #end[rw]
205 }
206
207 @Override
208 Object base() {
209 return null;
210 }
211
212 public $Type$Buffer slice() {
213 int pos = this.position();
214 int lim = this.limit();
215 assert (pos <= lim);
216 int rem = (pos <= lim ? lim - pos : 0);
217 int off = (pos << $LG_BYTES_PER_VALUE$);
218 assert (off >= 0);
219 return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off);
220 }
221
222 @Override
223 public $Type$Buffer slice(int index, int length) {
224 Objects.checkFromIndexSize(index, length, limit());
225 return new Direct$Type$Buffer$RW$$BO$(this,
226 -1,
227 0,
228 length,
229 length,
230 index);
231 }
232
233 public $Type$Buffer duplicate() {
234 return new Direct$Type$Buffer$RW$$BO$(this,
235 this.markValue(),
236 this.position(),
237 this.limit(),
238 this.capacity(),
239 0);
240 }
241
242 public $Type$Buffer asReadOnlyBuffer() {
243 #if[rw]
244 return new Direct$Type$BufferR$BO$(this,
245 this.markValue(),
246 this.position(),
247 this.limit(),
248 this.capacity(),
249 0);
250 #else[rw]
251 return duplicate();
252 #end[rw]
253 }
254
255 #if[rw]
256
257 public long address() {
258 return address;
259 }
260
261 private long ix(int i) {
262 return address + ((long)i << $LG_BYTES_PER_VALUE$);
263 }
264
265 public $type$ get() {
266 try {
267 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(nextGetIndex()))));
268 } finally {
269 Reference.reachabilityFence(this);
270 }
271 }
272
273 public $type$ get(int i) {
274 try {
275 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(checkIndex(i)))));
276 } finally {
277 Reference.reachabilityFence(this);
278 }
279 }
280
281 #if[streamableType]
282 $type$ getUnchecked(int i) {
283 try {
284 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(i))));
285 } finally {
286 Reference.reachabilityFence(this);
287 }
288 }
289 #end[streamableType]
290
291 public $Type$Buffer get($type$[] dst, int offset, int length) {
292 #if[rw]
293 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
294 Objects.checkFromIndexSize(offset, length, dst.length);
295 int pos = position();
296 int lim = limit();
297 assert (pos <= lim);
298 int rem = (pos <= lim ? lim - pos : 0);
299 if (length > rem)
300 throw new BufferUnderflowException();
301
302 long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
303 try {
304 #if[!byte]
305 if (order() != ByteOrder.nativeOrder())
306 UNSAFE.copySwapMemory(null,
307 ix(pos),
308 dst,
309 dstOffset,
310 (long)length << $LG_BYTES_PER_VALUE$,
311 (long)1 << $LG_BYTES_PER_VALUE$);
312 else
314 UNSAFE.copyMemory(null,
315 ix(pos),
316 dst,
317 dstOffset,
318 (long)length << $LG_BYTES_PER_VALUE$);
319 } finally {
320 Reference.reachabilityFence(this);
321 }
322 position(pos + length);
323 } else {
324 super.get(dst, offset, length);
325 }
326 return this;
327 #else[rw]
328 throw new ReadOnlyBufferException();
329 #end[rw]
330 }
331
332 public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
333 #if[rw]
334 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
335 Objects.checkFromIndexSize(index, length, limit());
336 Objects.checkFromIndexSize(offset, length, dst.length);
337
338 long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
339 try {
340 #if[!byte]
341 if (order() != ByteOrder.nativeOrder())
342 UNSAFE.copySwapMemory(null,
343 ix(index),
344 dst,
345 dstOffset,
346 (long)length << $LG_BYTES_PER_VALUE$,
347 (long)1 << $LG_BYTES_PER_VALUE$);
348 else
349 #end[!byte]
350 UNSAFE.copyMemory(null,
351 ix(index),
352 dst,
353 dstOffset,
354 (long)length << $LG_BYTES_PER_VALUE$);
355 } finally {
356 Reference.reachabilityFence(this);
357 }
358 } else {
359 super.get(index, dst, offset, length);
360 }
361 return this;
362 #else[rw]
363 throw new ReadOnlyBufferException();
364 #end[rw]
365 }
366 #end[rw]
367
368 public $Type$Buffer put($type$ x) {
369 #if[rw]
370 try {
371 UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
372 } finally {
373 Reference.reachabilityFence(this);
374 }
375 return this;
376 #else[rw]
377 throw new ReadOnlyBufferException();
378 #end[rw]
379 }
380
381 public $Type$Buffer put(int i, $type$ x) {
382 #if[rw]
383 try {
384 UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
385 } finally {
386 Reference.reachabilityFence(this);
387 }
388 return this;
389 #else[rw]
390 throw new ReadOnlyBufferException();
391 #end[rw]
392 }
393
394 public $Type$Buffer put($Type$Buffer src) {
395 #if[rw]
396 if (src instanceof Direct$Type$Buffer$BO$) {
397 if (src == this)
398 throw createSameBufferException();
399 Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;
400
401 int spos = sb.position();
402 int slim = sb.limit();
403 assert (spos <= slim);
404 int srem = (spos <= slim ? slim - spos : 0);
405
406 int pos = position();
407 int lim = limit();
408 assert (pos <= lim);
409 int rem = (pos <= lim ? lim - pos : 0);
410
411 if (srem > rem)
412 throw new BufferOverflowException();
413 try {
414 UNSAFE.copyMemory(sb.ix(spos), ix(pos), (long)srem << $LG_BYTES_PER_VALUE$);
415 } finally {
422
423 int spos = src.position();
424 int slim = src.limit();
425 assert (spos <= slim);
426 int srem = (spos <= slim ? slim - spos : 0);
427
428 put(src.hb, src.offset + spos, srem);
429 src.position(spos + srem);
430
431 } else {
432 super.put(src);
433 }
434 return this;
435 #else[rw]
436 throw new ReadOnlyBufferException();
437 #end[rw]
438 }
439
440 public $Type$Buffer put($type$[] src, int offset, int length) {
441 #if[rw]
442 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
443 Objects.checkFromIndexSize(offset, length, src.length);
444 int pos = position();
445 int lim = limit();
446 assert (pos <= lim);
447 int rem = (pos <= lim ? lim - pos : 0);
448 if (length > rem)
449 throw new BufferOverflowException();
450
451 long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
452 try {
453 #if[!byte]
454 if (order() != ByteOrder.nativeOrder())
455 UNSAFE.copySwapMemory(src,
456 srcOffset,
457 null,
458 ix(pos),
459 (long)length << $LG_BYTES_PER_VALUE$,
460 (long)1 << $LG_BYTES_PER_VALUE$);
461 else
463 UNSAFE.copyMemory(src,
464 srcOffset,
465 null,
466 ix(pos),
467 (long)length << $LG_BYTES_PER_VALUE$);
468 } finally {
469 Reference.reachabilityFence(this);
470 }
471 position(pos + length);
472 } else {
473 super.put(src, offset, length);
474 }
475 return this;
476 #else[rw]
477 throw new ReadOnlyBufferException();
478 #end[rw]
479 }
480
481 public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
482 #if[rw]
483 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
484 Objects.checkFromIndexSize(index, length, limit());
485 Objects.checkFromIndexSize(offset, length, src.length);
486
487
488 long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
489 try {
490 #if[!byte]
491 if (order() != ByteOrder.nativeOrder())
492 UNSAFE.copySwapMemory(src,
493 srcOffset,
494 null,
495 ix(index),
496 (long)length << $LG_BYTES_PER_VALUE$,
497 (long)1 << $LG_BYTES_PER_VALUE$);
498 else
499 #end[!byte]
500 UNSAFE.copyMemory(src,
501 srcOffset,
502 null,
560 throw new IndexOutOfBoundsException();
561 }
562 }
563
564
565 // --- Methods to support CharSequence ---
566
567 public CharBuffer subSequence(int start, int end) {
568 int pos = position();
569 int lim = limit();
570 assert (pos <= lim);
571 pos = (pos <= lim ? pos : lim);
572 int len = lim - pos;
573
574 Objects.checkFromToIndex(start, end, len);
575 return new DirectCharBuffer$RW$$BO$(this,
576 -1,
577 pos + start,
578 pos + end,
579 capacity(),
580 offset);
581 }
582
583 #end[char]
584
585
586
587 #if[!byte]
588
589 public ByteOrder order() {
590 #if[boS]
591 return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
592 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
593 #end[boS]
594 #if[boU]
595 return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
596 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
597 #end[boU]
598 }
599
600 #end[!byte]
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 #warn This file is preprocessed before being compiled
27
28 package java.nio;
29
30 import java.io.FileDescriptor;
31 import java.lang.ref.Reference;
32 import java.util.Objects;
33 import jdk.internal.access.foreign.MemorySegmentProxy;
34 import jdk.internal.misc.VM;
35 import jdk.internal.ref.Cleaner;
36 import sun.nio.ch.DirectBuffer;
37
38
39 class Direct$Type$Buffer$RW$$BO$
40 #if[rw]
41 extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
42 #else[rw]
43 extends Direct$Type$Buffer$BO$
44 #end[rw]
45 implements DirectBuffer
46 {
47
48 #if[rw]
49
50 // Cached array base offset
51 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset($type$[].class);
52
53 // Cached unaligned-access capability
96 }
97
98 private final Cleaner cleaner;
99
100 public Cleaner cleaner() { return cleaner; }
101
102 #else[byte]
103
104 public Cleaner cleaner() { return null; }
105
106 #end[byte]
107
108 #end[rw]
109
110 #if[byte]
111
112 // Primary constructor
113 //
114 Direct$Type$Buffer$RW$(int cap) { // package-private
115 #if[rw]
116 super(-1, 0, cap, cap, null);
117 boolean pa = VM.isDirectMemoryPageAligned();
118 int ps = Bits.pageSize();
119 long size = Math.max(1L, (long)cap + (pa ? ps : 0));
120 Bits.reserveMemory(size, cap);
121
122 long base = 0;
123 try {
124 base = UNSAFE.allocateMemory(size);
125 } catch (OutOfMemoryError x) {
126 Bits.unreserveMemory(size, cap);
127 throw x;
128 }
129 UNSAFE.setMemory(base, size, (byte) 0);
130 if (pa && (base % ps != 0)) {
131 // Round up to page boundary
132 address = base + ps - (base & (ps - 1));
133 } else {
134 address = base;
135 }
136 cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
137 att = null;
138 #else[rw]
139 super(cap);
140 this.isReadOnly = true;
141 #end[rw]
142 }
143
144 #if[rw]
145
146 // Invoked to construct a direct ByteBuffer referring to the block of
147 // memory. A given arbitrary object may also be attached to the buffer.
148 //
149 Direct$Type$Buffer(long addr, int cap, Object ob, MemorySegmentProxy segment) {
150 super(-1, 0, cap, cap, segment);
151 address = addr;
152 cleaner = null;
153 att = ob;
154 }
155
156
157 // Invoked only by JNI: NewDirectByteBuffer(void*, long)
158 //
159 private Direct$Type$Buffer(long addr, int cap) {
160 super(-1, 0, cap, cap, null);
161 address = addr;
162 cleaner = null;
163 att = null;
164 }
165
166 #end[rw]
167
168 // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
169 //
170 protected Direct$Type$Buffer$RW$(int cap, long addr,
171 FileDescriptor fd,
172 Runnable unmapper,
173 boolean isSync, MemorySegmentProxy segment)
174 {
175 #if[rw]
176 super(-1, 0, cap, cap, fd, isSync, segment);
177 address = addr;
178 cleaner = Cleaner.create(this, unmapper);
179 att = null;
180 #else[rw]
181 super(cap, addr, fd, unmapper, isSync, segment);
182 this.isReadOnly = true;
183 #end[rw]
184 }
185
186 #end[byte]
187
188 // For duplicates and slices
189 //
190 Direct$Type$Buffer$RW$$BO$(DirectBuffer db, // package-private
191 int mark, int pos, int lim, int cap,
192 int off, MemorySegmentProxy segment)
193 {
194 #if[rw]
195 super(mark, pos, lim, cap, segment);
196 address = db.address() + off;
197 #if[byte]
198 cleaner = null;
199 #end[byte]
200 Object attachment = db.attachment();
201 att = (attachment == null ? db : attachment);
202 #else[rw]
203 super(db, mark, pos, lim, cap, off, segment);
204 this.isReadOnly = true;
205 #end[rw]
206 }
207
208 @Override
209 Object base() {
210 return null;
211 }
212
213 public $Type$Buffer slice() {
214 int pos = this.position();
215 int lim = this.limit();
216 assert (pos <= lim);
217 int rem = (pos <= lim ? lim - pos : 0);
218 int off = (pos << $LG_BYTES_PER_VALUE$);
219 assert (off >= 0);
220 return new Direct$Type$Buffer$RW$$BO$(this, -1, 0, rem, rem, off, segment);
221 }
222
223 @Override
224 public $Type$Buffer slice(int index, int length) {
225 Objects.checkFromIndexSize(index, length, limit());
226 return new Direct$Type$Buffer$RW$$BO$(this,
227 -1,
228 0,
229 length,
230 length,
231 index, segment);
232 }
233
234 public $Type$Buffer duplicate() {
235 return new Direct$Type$Buffer$RW$$BO$(this,
236 this.markValue(),
237 this.position(),
238 this.limit(),
239 this.capacity(),
240 0, segment);
241 }
242
243 public $Type$Buffer asReadOnlyBuffer() {
244 #if[rw]
245 return new Direct$Type$BufferR$BO$(this,
246 this.markValue(),
247 this.position(),
248 this.limit(),
249 this.capacity(),
250 0, segment);
251 #else[rw]
252 return duplicate();
253 #end[rw]
254 }
255
256 #if[rw]
257
258 public long address() {
259 return address;
260 }
261
262 private long ix(int i) {
263 return address + ((long)i << $LG_BYTES_PER_VALUE$);
264 }
265
266 public $type$ get() {
267 try {
268 checkSegment();
269 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(nextGetIndex()))));
270 } finally {
271 Reference.reachabilityFence(this);
272 }
273 }
274
275 public $type$ get(int i) {
276 try {
277 checkSegment();
278 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(checkIndex(i)))));
279 } finally {
280 Reference.reachabilityFence(this);
281 }
282 }
283
284 #if[streamableType]
285 $type$ getUnchecked(int i) {
286 try {
287 return $fromBits$($swap$(UNSAFE.get$Swaptype$(ix(i))));
288 } finally {
289 Reference.reachabilityFence(this);
290 }
291 }
292 #end[streamableType]
293
294 public $Type$Buffer get($type$[] dst, int offset, int length) {
295 #if[rw]
296 checkSegment();
297 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
298 Objects.checkFromIndexSize(offset, length, dst.length);
299 int pos = position();
300 int lim = limit();
301 assert (pos <= lim);
302 int rem = (pos <= lim ? lim - pos : 0);
303 if (length > rem)
304 throw new BufferUnderflowException();
305
306 long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
307 try {
308 #if[!byte]
309 if (order() != ByteOrder.nativeOrder())
310 UNSAFE.copySwapMemory(null,
311 ix(pos),
312 dst,
313 dstOffset,
314 (long)length << $LG_BYTES_PER_VALUE$,
315 (long)1 << $LG_BYTES_PER_VALUE$);
316 else
318 UNSAFE.copyMemory(null,
319 ix(pos),
320 dst,
321 dstOffset,
322 (long)length << $LG_BYTES_PER_VALUE$);
323 } finally {
324 Reference.reachabilityFence(this);
325 }
326 position(pos + length);
327 } else {
328 super.get(dst, offset, length);
329 }
330 return this;
331 #else[rw]
332 throw new ReadOnlyBufferException();
333 #end[rw]
334 }
335
336 public $Type$Buffer get(int index, $type$[] dst, int offset, int length) {
337 #if[rw]
338 checkSegment();
339 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
340 Objects.checkFromIndexSize(index, length, limit());
341 Objects.checkFromIndexSize(offset, length, dst.length);
342
343 long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
344 try {
345 #if[!byte]
346 if (order() != ByteOrder.nativeOrder())
347 UNSAFE.copySwapMemory(null,
348 ix(index),
349 dst,
350 dstOffset,
351 (long)length << $LG_BYTES_PER_VALUE$,
352 (long)1 << $LG_BYTES_PER_VALUE$);
353 else
354 #end[!byte]
355 UNSAFE.copyMemory(null,
356 ix(index),
357 dst,
358 dstOffset,
359 (long)length << $LG_BYTES_PER_VALUE$);
360 } finally {
361 Reference.reachabilityFence(this);
362 }
363 } else {
364 super.get(index, dst, offset, length);
365 }
366 return this;
367 #else[rw]
368 throw new ReadOnlyBufferException();
369 #end[rw]
370 }
371 #end[rw]
372
373 public $Type$Buffer put($type$ x) {
374 #if[rw]
375 try {
376 checkSegment();
377 UNSAFE.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
378 } finally {
379 Reference.reachabilityFence(this);
380 }
381 return this;
382 #else[rw]
383 throw new ReadOnlyBufferException();
384 #end[rw]
385 }
386
387 public $Type$Buffer put(int i, $type$ x) {
388 #if[rw]
389 try {
390 checkSegment();
391 UNSAFE.put$Swaptype$(ix(checkIndex(i)), $swap$($toBits$(x)));
392 } finally {
393 Reference.reachabilityFence(this);
394 }
395 return this;
396 #else[rw]
397 throw new ReadOnlyBufferException();
398 #end[rw]
399 }
400
401 public $Type$Buffer put($Type$Buffer src) {
402 #if[rw]
403 checkSegment();
404 if (src instanceof Direct$Type$Buffer$BO$) {
405 if (src == this)
406 throw createSameBufferException();
407 Direct$Type$Buffer$RW$$BO$ sb = (Direct$Type$Buffer$RW$$BO$)src;
408
409 int spos = sb.position();
410 int slim = sb.limit();
411 assert (spos <= slim);
412 int srem = (spos <= slim ? slim - spos : 0);
413
414 int pos = position();
415 int lim = limit();
416 assert (pos <= lim);
417 int rem = (pos <= lim ? lim - pos : 0);
418
419 if (srem > rem)
420 throw new BufferOverflowException();
421 try {
422 UNSAFE.copyMemory(sb.ix(spos), ix(pos), (long)srem << $LG_BYTES_PER_VALUE$);
423 } finally {
430
431 int spos = src.position();
432 int slim = src.limit();
433 assert (spos <= slim);
434 int srem = (spos <= slim ? slim - spos : 0);
435
436 put(src.hb, src.offset + spos, srem);
437 src.position(spos + srem);
438
439 } else {
440 super.put(src);
441 }
442 return this;
443 #else[rw]
444 throw new ReadOnlyBufferException();
445 #end[rw]
446 }
447
448 public $Type$Buffer put($type$[] src, int offset, int length) {
449 #if[rw]
450 checkSegment();
451 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
452 Objects.checkFromIndexSize(offset, length, src.length);
453 int pos = position();
454 int lim = limit();
455 assert (pos <= lim);
456 int rem = (pos <= lim ? lim - pos : 0);
457 if (length > rem)
458 throw new BufferOverflowException();
459
460 long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
461 try {
462 #if[!byte]
463 if (order() != ByteOrder.nativeOrder())
464 UNSAFE.copySwapMemory(src,
465 srcOffset,
466 null,
467 ix(pos),
468 (long)length << $LG_BYTES_PER_VALUE$,
469 (long)1 << $LG_BYTES_PER_VALUE$);
470 else
472 UNSAFE.copyMemory(src,
473 srcOffset,
474 null,
475 ix(pos),
476 (long)length << $LG_BYTES_PER_VALUE$);
477 } finally {
478 Reference.reachabilityFence(this);
479 }
480 position(pos + length);
481 } else {
482 super.put(src, offset, length);
483 }
484 return this;
485 #else[rw]
486 throw new ReadOnlyBufferException();
487 #end[rw]
488 }
489
490 public $Type$Buffer put(int index, $type$[] src, int offset, int length) {
491 #if[rw]
492 checkSegment();
493 if (((long)length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
494 Objects.checkFromIndexSize(index, length, limit());
495 Objects.checkFromIndexSize(offset, length, src.length);
496
497
498 long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << $LG_BYTES_PER_VALUE$);
499 try {
500 #if[!byte]
501 if (order() != ByteOrder.nativeOrder())
502 UNSAFE.copySwapMemory(src,
503 srcOffset,
504 null,
505 ix(index),
506 (long)length << $LG_BYTES_PER_VALUE$,
507 (long)1 << $LG_BYTES_PER_VALUE$);
508 else
509 #end[!byte]
510 UNSAFE.copyMemory(src,
511 srcOffset,
512 null,
570 throw new IndexOutOfBoundsException();
571 }
572 }
573
574
575 // --- Methods to support CharSequence ---
576
577 public CharBuffer subSequence(int start, int end) {
578 int pos = position();
579 int lim = limit();
580 assert (pos <= lim);
581 pos = (pos <= lim ? pos : lim);
582 int len = lim - pos;
583
584 Objects.checkFromToIndex(start, end, len);
585 return new DirectCharBuffer$RW$$BO$(this,
586 -1,
587 pos + start,
588 pos + end,
589 capacity(),
590 offset, segment);
591 }
592
593 #end[char]
594
595
596
597 #if[!byte]
598
599 public ByteOrder order() {
600 #if[boS]
601 return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
602 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
603 #end[boS]
604 #if[boU]
605 return ((ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
606 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
607 #end[boU]
608 }
609
610 #end[!byte]
|