Is there an equivalent memcpy () in Java?

I have byte [] and you want to copy it to another byte []. Maybe I'm showing my simple "C" background here, but is there an equivalent to memcpy () on byte arrays in Java?

+70
java bytearray
Jul 25 '10 at 12:21
source share
8 answers

You can try System.arraycopy or use array functions in Arrays like java.util.Arrays.copyOf . Both should give you your own performance under the hood.

Arrays.copyOf is probably readable, but was introduced only in java 1.6.

  byte[] src = {1, 2, 3, 4}; byte[] dst = Arrays.copyOf(src, src.length); System.out.println(Arrays.toString(dst)); 
+80
Jul 25 '10 at 12:25
source share

Use System.arraycopy ()

 System.arraycopy(sourceArray, sourceStartIndex, targetArray, targetStartIndex, length); 

Example

 String[] source = { "alpha", "beta", "gamma" }; String[] target = new String[source.length]; System.arraycopy(source, 0, target, 0, source.length); 


or use Arrays.copyOf ()
Example,

 target = Arrays.copyOf(source, length); 

java.util.Arrays.copyOf(byte[] source, int length) was added in JDK 1.6.

The copyOf() method uses System.arrayCopy() to create a copy of the array, but more flexible than clone() , since you can create copies of parts of the array.

+88
Jul 25 '10 at 12:28
source share

If you only need an exact copy of a one-dimensional array, use clone() .

 byte[] array = { 0x0A, 0x01 }; byte[] copy = array.clone(); 

For other array copy operations, use System.arrayCopy / Arrays.copyOf as Tom suggests .

In general, clone should be avoided, but this is an exception to the rule.

+7
Jul 25 '10 at 13:16
source share

You can use System.arrayCopy . It copies elements from the source array to the destination array. Sun's implementation uses manually optimized assembler, so it's fast.

+5
Jul 25 '10 at 12:25
source share

Java really has something like memcpy (). The Unsafe class has a copyMemory () method, which is essentially identical to memcpy (). Of course, like memcpy (), it does not protect against memory overlays, data destruction, etc. It is not clear whether this is memcpy () or memmove (). It can be used to copy from actual addresses to actual addresses or from links to links. Please note that if links are used, you must provide an offset (or the JVM will die as soon as possible).

Unsafe.copyMemory () works (up to 2 GB per second on my tired PC). Use at your own risk. Note that the Unsafe class does not exist for all JVM implementations.

+3
Oct. 30 '13 at 16:05
source share

You can use System.arraycopy

+2
Jul 25 '10 at 12:25
source share

Use byteBufferViewVarHandle or byteArrayViewVarHandle.

This will allow you to copy the "long" array directly into the "double" array and similar with something like:

 public long[] toLongs(byte[] buf) { int end = buf.length >> 3; long[] newArray = new long[end]; for (int ii = 0; ii < end; ++ii) { newArray[ii] = (long)AS_LONGS_VH.get(buf, ALIGN_OFFSET + ii << 3); } } private static final ALIGN_OFFSET = ByteBuffer.wrap(new byte[0]).alignmentOffset(8); private static final VarHandle AS_LONGS_VH = MethodHandles.byteArrayViewVarHandle(long[].class, ByteOrder.nativeOrder()); 

This will allow you to hack a bit, like:

 float thefloat = 0.4; int floatBits; _Static_assert(sizeof theFloat == sizeof floatBits, "this bit twiddling hack requires floats to be equal in size to ints"); memcpy(&floatBits, &thefloat, sizeof floatBits); 
0
Jan 05 '19 at 20:49
source share

No, Java does not have the equivalent of memcpy . memmove this Java has the equivalent of memmove .

If the arguments src and dest refer to the same array object, then copying is performed as if the components at positions srcPos - srcPos + length-1 were first copied to a temporary array with length components, and then the contents of the temporary array were copied to destPos positions through destPos + length-1 of the destination array.

Oracle Docs

It is very likely that System.arraycopy will never have the same performance as memcpy if src and dest refer to the same array. Usually it will be fast enough, though.

0
Jan 19 '19 at 19:26
source share



All Articles