public final void swapBytes ( byte [ ] tempBuffer , MemorySegment seg2 , int offset1 , int offset2 , int len ) { if ( ( offset1 | offset2 | len | ( tempBuffer . length - len ) ) >= 0 ) { final long thisPos = this . address + offset1 ; final long otherPos = seg2 . address + offset2 ; if ( thisPos <= this . addressLimit - len && otherPos <= seg2 . addressLimit - len ) { // this -> temp buffer UNSAFE . copyMemory ( this . heapMemory , thisPos , tempBuffer , BYTE_ARRAY_BASE_OFFSET , len ) ; // other -> this UNSAFE . copyMemory ( seg2 . heapMemory , otherPos , this . heapMemory , thisPos , len ) ; // temp buffer -> other UNSAFE . copyMemory ( tempBuffer , BYTE_ARRAY_BASE_OFFSET , seg2 . heapMemory , otherPos , len ) ; return ; } else if ( this . address > this . addressLimit ) { throw new IllegalStateException ( "this memory segment has been freed." ) ; } else if ( seg2 . address > seg2 . addressLimit ) { throw new IllegalStateException ( "other memory segment has been freed." ) ; } } // index is in fact invalid throw new IndexOutOfBoundsException ( String . format ( "offset1=%d, offset2=%d, len=%d, bufferSize=%d, address1=%d, address2=%d" , offset1 , offset2 , len , tempBuffer . length , this . address , seg2 . address ) ) ; }
Swaps bytes between two memory segments using the given auxiliary buffer .