@@ -367,15 +367,18 @@ public void get(int offset, ByteBuffer target, int numBytes) {
367367 final int targetPos = target .position ();
368368 if (target .isDirect ()) {
369369 final long sourceOffset = address + offset ;
370- if (sourceOffset > addressLimit - numBytes ) throwOOBException ();
370+ if (sourceOffset > addressLimit - numBytes ) {
371+ throwOOBException ();
372+ }
371373 final long targetAddr = ByteBufferUtil .getAddress (target ) + targetPos ;
372374 if (Platform .IS_ANDROID ) {
373375 if (onHeap ) {
374376 target .put (heapMemory , memoryOffset + offset , numBytes ); // 此操作会改变target的position,无需再调整了
375377 return ;
378+ } else {
379+ // Android只支持这个三个参数的copyMemory
380+ Platform .UNSAFE .copyMemory (sourceOffset , targetAddr , numBytes );
376381 }
377- // Android只支持这个三个参数的copyMemory
378- else Platform .UNSAFE .copyMemory (sourceOffset , targetAddr , numBytes );
379382 } else {
380383 Platform .copyMemory (heapMemory , sourceOffset , null , targetAddr , numBytes );
381384 }
@@ -395,7 +398,9 @@ public void put(int offset, ByteBuffer source, int numBytes) {
395398 if (source .isDirect ()) {
396399 final long sourceAddr = ByteBufferUtil .getAddress (source ) + sourcePos ;
397400 final long targetAddr = address + offset ;
398- if (targetAddr > addressLimit - numBytes ) throwOOBException ();
401+ if (targetAddr > addressLimit - numBytes ) {
402+ throwOOBException ();
403+ }
399404 if (Platform .IS_ANDROID ) {
400405 if (onHeap ) {
401406 source .get (heapMemory , memoryOffset + offset , numBytes ); // 此操作会改变source的position,无需再调整了
@@ -1287,7 +1292,9 @@ public void coverMemoryWithArray(
12871292 Types .JavaArray eleType ,
12881293 boolean autoFill ) {
12891294 boolean writeMode = bufOffset < 0 ;
1290- if (writeMode ) bufOffset = writerIndex ;
1295+ if (writeMode ) {
1296+ bufOffset = writerIndex ;
1297+ }
12911298 int numBytes = length * eleType .bytesPerEle ;
12921299 if (autoFill ) {
12931300 ensure (bufOffset + numBytes );
@@ -1329,10 +1336,14 @@ public void coverMemoryWithArray(
13291336 case DOUBLE :
13301337 buffer .asDoubleBuffer ().put ((double []) arr , offset , length );
13311338 break ;
1339+ default :
1340+ throw new IllegalStateException ("Unexpected value: " + eleType );
13321341 }
13331342 }
13341343 }
1335- if (writeMode ) writerIndex += numBytes ;
1344+ if (writeMode ) {
1345+ writerIndex += numBytes ;
1346+ }
13361347 }
13371348
13381349 public void writeArray (
@@ -2554,7 +2565,9 @@ public void copyTo(
25542565 Types .JavaArray eleType ,
25552566 boolean checkSize ) {
25562567 boolean readMode = bufOffset < 0 ;
2557- if (readMode ) bufOffset = readerIndex ;
2568+ if (readMode ) {
2569+ bufOffset = readerIndex ;
2570+ }
25582571 int numBytes = length * eleType .bytesPerEle ;
25592572 if (checkSize ) {
25602573 int remaining = size - bufOffset ;
@@ -2599,10 +2612,39 @@ public void copyTo(
25992612 case DOUBLE :
26002613 buffer .asDoubleBuffer ().get ((double []) target , offset , length );
26012614 break ;
2615+ default :
2616+ throw new IllegalStateException ("Unsupported element type: " + eleType );
26022617 }
26032618 }
26042619 }
2605- if (readMode ) readerIndex += numBytes ;
2620+ if (readMode ) {
2621+ readerIndex += numBytes ;
2622+ }
2623+ }
2624+
2625+ public void copyTo (int offset , MemoryBuffer target , int targetOffset , int numBytes ) {
2626+ final long thisPointer = this .address + offset ;
2627+ final long otherPointer = target .address + targetOffset ;
2628+ if ((numBytes | offset | targetOffset ) >= 0
2629+ && thisPointer <= this .addressLimit - numBytes
2630+ && otherPointer <= target .addressLimit - numBytes ) {
2631+ if (Platform .IS_ANDROID ) {
2632+ int thisOldLimit = this .buffer .limit ();
2633+ this .buffer .position (offset );
2634+ this .buffer .limit (offset + numBytes );
2635+ target .buffer .position (targetOffset );
2636+ target .buffer .put (this .buffer );
2637+ this .buffer .limit (thisOldLimit );
2638+ } else {
2639+ Platform .copyMemory (
2640+ this .heapMemory , thisPointer , target .heapMemory , otherPointer , numBytes );
2641+ }
2642+ } else {
2643+ throw new IndexOutOfBoundsException (
2644+ String .format (
2645+ "offset=%d, targetOffset=%d, numBytes=%d, address=%d, targetAddress=%d" ,
2646+ offset , targetOffset , numBytes , this .address , target .address ));
2647+ }
26062648 }
26072649
26082650 public void readTo (Object target , int offset , int length , Types .JavaArray eleType ) {
@@ -2685,31 +2727,6 @@ public void copyFromDirectUnsafe(long offset, long sourcePointer, long numBytes)
26852727 Platform .copyMemory (null , sourcePointer , this .heapMemory , thisPointer , numBytes );
26862728 }
26872729
2688- public void copyTo (int offset , MemoryBuffer target , int targetOffset , int numBytes ) {
2689- final long thisPointer = this .address + offset ;
2690- final long otherPointer = target .address + targetOffset ;
2691- if ((numBytes | offset | targetOffset ) >= 0
2692- && thisPointer <= this .addressLimit - numBytes
2693- && otherPointer <= target .addressLimit - numBytes ) {
2694- if (Platform .IS_ANDROID ) {
2695- int thisOldLimit = this .buffer .limit ();
2696- this .buffer .position (offset );
2697- this .buffer .limit (offset + numBytes );
2698- target .buffer .position (targetOffset );
2699- target .buffer .put (this .buffer );
2700- this .buffer .limit (thisOldLimit );
2701- } else {
2702- Platform .copyMemory (
2703- this .heapMemory , thisPointer , target .heapMemory , otherPointer , numBytes );
2704- }
2705- } else {
2706- throw new IndexOutOfBoundsException (
2707- String .format (
2708- "offset=%d, targetOffset=%d, numBytes=%d, address=%d, targetAddress=%d" ,
2709- offset , targetOffset , numBytes , this .address , target .address ));
2710- }
2711- }
2712-
27132730 public void copyFrom (int offset , MemoryBuffer source , int sourcePointer , int numBytes ) {
27142731 source .copyTo (sourcePointer , this , offset , numBytes );
27152732 }
@@ -2826,9 +2843,10 @@ public void pointTo(byte[] buffer, int offset, int length) {
28262843 // TODO: support android
28272844 @ NotForAndroid (reason = "Android does not support support off-heap memory only by address" )
28282845 public static MemoryBuffer fromNativeAddress (long address , int size ) {
2829- if (Platform .IS_ANDROID )
2846+ if (Platform .IS_ANDROID ) {
28302847 throw new UnsupportedOperationException (
28312848 "Android does not support support off-heap memory only by address" );
2849+ }
28322850 return new MemoryBuffer (address , null , size , null , false );
28332851 }
28342852
@@ -2839,6 +2857,16 @@ public static MemoryBuffer wrap(byte[] buffer) {
28392857 return new MemoryBuffer (buffer , 0 , buffer .length );
28402858 }
28412859
2860+ /**
2861+ * Creates a new memory segment that represents the memory backing the given byte buffer section
2862+ * of [buffer.position(), buffer,limit()].
2863+ *
2864+ * @param byteBuffer a direct buffer or heap buffer
2865+ */
2866+ public static MemoryBuffer wrap (ByteBuffer byteBuffer ) {
2867+ return fromByteBuffer (byteBuffer , byteBuffer .remaining ());
2868+ }
2869+
28422870 public static MemoryBuffer buffer (int size ) {
28432871 return newHeapBuffer (size );
28442872 }
@@ -2855,16 +2883,6 @@ public static MemoryBuffer bufferDirect(int size) {
28552883 return new MemoryBuffer (-1 , ByteBuffer .allocateDirect (size ), size , null , true );
28562884 }
28572885
2858- /**
2859- * Creates a new memory segment that represents the memory backing the given byte buffer section
2860- * of [buffer.position(), buffer,limit()].
2861- *
2862- * @param byteBuffer a direct buffer or heap buffer
2863- */
2864- public static MemoryBuffer wrap (ByteBuffer byteBuffer ) {
2865- return fromByteBuffer (byteBuffer , byteBuffer .remaining ());
2866- }
2867-
28682886 /**
28692887 * Creates a new memory buffer that represents the native memory at the absolute address given by
28702888 * the pointer.
0 commit comments