Last active
July 17, 2016 09:33
-
-
Save peter-lawrey/f9b0bb3d3042acb4a7a5012e94c415bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MemoryCopy { | |
private static final Unsafe UNSAFE; | |
static { | |
Unsafe unsafe = null; | |
try { | |
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); | |
theUnsafe.setAccessible(true); | |
unsafe = (Unsafe) theUnsafe.get(null); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
UNSAFE = unsafe; | |
} | |
public static void write(ByteBuffer dest, long[] src) { | |
if (UNSAFE != null && dest instanceof DirectBuffer) { | |
if (dest.capacity() < src.length * 8) | |
throw new IllegalArgumentException(); | |
long address = ((DirectBuffer) dest).address(); | |
int i = 0; | |
for (i = 0; i < src.length; i += 2) { | |
UNSAFE.putLong(address, Long.reverseBytes(src[i])); // Big endian | |
UNSAFE.putLong(address + 8, Long.reverseBytes(src[i + 1]));// Big endian | |
address += Long.BYTES * 2; | |
} | |
if (i < src.length) { | |
UNSAFE.putLong(address, Long.reverseBytes(src[i]));// Big endian | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment