@@ -419,6 +419,7 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
419419 {
420420 int count = Math .min (length , capacity - index );
421421 count = Math .min (count , dst .length - offset );
422+
422423 if (hasArray )
423424 {
424425 System .arraycopy (byteArray , this .offset + index , dst , offset , count );
@@ -428,6 +429,7 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
428429 final long address = effectiveDirectAddress + index ;
429430 MEMORY_ACCESS .peekByteArray (address , dst , offset , count );
430431 }
432+
431433 return count ;
432434 }
433435
@@ -442,44 +444,34 @@ public int getBytes(final int index, final byte[] dst, final int offset, final i
442444 */
443445 public int getBytes (final int index , final DirectBuffer dst , final int offset , final int length )
444446 {
447+ int count = Math .min (length , capacity - index );
448+ count = Math .min (count , dst .capacity - offset );
449+
445450 if (hasArray )
446451 {
447- int srcOffset = this .offset + index ;
448- int count = Math .min (length , dst .capacity - offset );
449- count = Math .min (count , capacity - index );
450-
451452 if (dst .hasArray )
452453 {
453- System .arraycopy (byteArray , srcOffset , dst .byteArray , dst .offset + offset , count );
454+ System .arraycopy (byteArray , this . offset + index , dst .byteArray , dst .offset + offset , count );
454455 }
455456 else
456457 {
457458 final long address = dst .effectiveDirectAddress + offset ;
458- DirectBuffer .MEMORY_ACCESS .pokeByteArray (address , byteArray , srcOffset , count );
459+ DirectBuffer .MEMORY_ACCESS .pokeByteArray (address , byteArray , this . offset + index , count );
459460 }
460-
461- return count ;
462461 }
463- else if (dst .hasArray )
464- {
465- byte [] dstArray = dst .byteArray ;
466- int dstOffset = dst .offset + offset ;
467-
468- int count = Math .min (length , capacity - index );
469- count = Math .min (count , dst .capacity - offset );
470- final long address = effectiveDirectAddress + index ;
471- MEMORY_ACCESS .peekByteArray (address , dstArray , dstOffset , count );
472-
473- return count ;
462+ else {
463+ if (dst .hasArray )
464+ {
465+ final long address = effectiveDirectAddress + index ;
466+ MEMORY_ACCESS .peekByteArray (address , dst .byteArray , dst .offset + offset , count );
467+ }
468+ else
469+ {
470+ Memory .memmove (dst .byteBuffer , dst .offset + offset , byteBuffer , this .offset + index , count );
471+ }
474472 }
475- else
476- {
477- int count = Math .min (length , capacity - index );
478- count = Math .min (count , dst .capacity - offset );
479- Memory .memmove (dst .byteBuffer , dst .offset + offset , byteBuffer , this .offset + index , count );
480473
481- return count ;
482- }
474+ return count ;
483475 }
484476
485477 /**
@@ -492,8 +484,9 @@ else if (dst.hasArray)
492484 */
493485 public int getBytes (final int index , final ByteBuffer dstBuffer , final int length )
494486 {
495- int count = Math .min (dstBuffer .remaining (), capacity - index );
496- count = Math .min (count , length );
487+ int count = Math .min (length , capacity - index );
488+ count = Math .min (count , dstBuffer .remaining ());
489+
497490 if (hasArray )
498491 {
499492 getBytesFromByteArray (index , dstBuffer , count );
@@ -502,7 +495,9 @@ public int getBytes(final int index, final ByteBuffer dstBuffer, final int lengt
502495 {
503496 getBytesFromMemory (index , dstBuffer , count );
504497 }
498+
505499 BitUtil .setBufferPosition (dstBuffer , dstBuffer .position () + count );
500+
506501 return count ;
507502 }
508503
@@ -531,6 +526,7 @@ public int putBytes(final int index, final byte[] src, final int offset, final i
531526 {
532527 int count = Math .min (length , capacity - index );
533528 count = Math .min (count , src .length - offset );
529+
534530 if (hasArray )
535531 {
536532 System .arraycopy (src , offset , byteArray , this .offset + index , count );
@@ -569,8 +565,8 @@ public int putBytes(final int index, final DirectBuffer src, final int offset, f
569565 */
570566 public int putBytes (final int index , final ByteBuffer srcBuffer , final int length )
571567 {
572- int count = Math .min (srcBuffer . remaining () , capacity - index );
573- count = Math .min (count , length );
568+ int count = Math .min (length , capacity - index );
569+ count = Math .min (count , srcBuffer . remaining () );
574570
575571 if (hasArray )
576572 {
@@ -580,71 +576,68 @@ public int putBytes(final int index, final ByteBuffer srcBuffer, final int lengt
580576 {
581577 putBytesToMemory (index , srcBuffer , count );
582578 }
579+
583580 BitUtil .setBufferPosition (srcBuffer , srcBuffer .position () + count );
584581
585582 return count ;
586583 }
587584
588585 private void getBytesFromMemory (final int index , final ByteBuffer dstBuffer , final int count )
589586 {
590- final int dstBufferPosition = dstBuffer .position ();
591587 if (dstBuffer .hasArray ())
592588 {
593589 final byte [] dst = dstBuffer .array ();
594- final int dstOffset = dstBuffer .arrayOffset () + dstBufferPosition ;
590+ final int dstOffset = dstBuffer .arrayOffset () + dstBuffer . position () ;
595591 final long address = effectiveDirectAddress + index ;
596592 MEMORY_ACCESS .peekByteArray (address , dst , dstOffset , count );
597593 }
598594 else
599595 {
600- Memory .memmove (dstBuffer , dstBufferPosition , byteBuffer , index , count );
596+ Memory .memmove (dstBuffer , dstBuffer . position () , byteBuffer , index , count );
601597 }
602598 }
603599
604600 private void putBytesToMemory (final int index , final ByteBuffer srcBuffer , final int count )
605601 {
606- final int srcBufferPosition = srcBuffer .position ();
607602 if (srcBuffer .hasArray ())
608603 {
609604 final byte [] src = srcBuffer .array ();
610- final int srcOffset = srcBuffer .arrayOffset () + srcBufferPosition ;
605+ final int srcOffset = srcBuffer .arrayOffset () + srcBuffer . position () ;
611606 final long address = effectiveDirectAddress + index ;
612607 MEMORY_ACCESS .pokeByteArray (address , src , srcOffset , count );
613608 }
614609 else
615610 {
616- Memory .memmove (byteBuffer , index , srcBuffer , srcBufferPosition , count );
611+ Memory .memmove (byteBuffer , index , srcBuffer , srcBuffer . position () , count );
617612 }
618613 }
619614
620615 private void getBytesFromByteArray (final int index , final ByteBuffer dstBuffer , final int count )
621616 {
622- final int dstBufferPosition = dstBuffer .position ();
623617 if (dstBuffer .hasArray ())
624618 {
625619 final byte [] dst = dstBuffer .array ();
626- final int dstOffset = dstBuffer .arrayOffset () + dstBufferPosition ;
620+ final int dstOffset = dstBuffer .arrayOffset () + dstBuffer . position () ;
627621 System .arraycopy (byteArray , this .offset + index , dst , dstOffset , count );
628622 }
629623 else
630624 {
631- final long address = BitUtil .getEffectiveDirectAddress (dstBuffer ) + dstBufferPosition ;
625+ final long address = BitUtil .getEffectiveDirectAddress (dstBuffer ) + dstBuffer . position () ;
632626 MEMORY_ACCESS .pokeByteArray (address , byteArray , offset + index , count );
633627 }
634628 }
635629
636630 private void putBytesToByteArray (final int index , final ByteBuffer srcBuffer , final int count )
637631 {
638- final int srcBufferPosition = srcBuffer .position ();
639632 if (srcBuffer .hasArray ())
640633 {
641634 final byte [] src = srcBuffer .array ();
642- final int srcOffset = srcBuffer .arrayOffset () + srcBufferPosition ;
635+ final int srcOffset = srcBuffer .arrayOffset () + srcBuffer . position () ;
643636 System .arraycopy (src , srcOffset , byteArray , this .offset + index , count );
644637 }
645638 else
646639 {
647- final long address = BitUtil .getEffectiveDirectAddress (srcBuffer ) + srcBufferPosition ;
640+ final long address = BitUtil .getEffectiveDirectAddress (srcBuffer ) + srcBuffer . position () ;
648641 MEMORY_ACCESS .peekByteArray (address , byteArray , offset + index , count );
649642 }
650643 }
0 commit comments