2

Example

short size = 1234;
    byte[] payload = {12,43, 55,123, 11, 55};

byte [] shortSize = ByteBuffer.allocate(2).putShort(size).array();
byte[] entirePayload = new byte[shortSize.length+payload.length];   

System.arraycopy(shortSize, 0, entirePayload, 0, 2);
System.arraycopy(payload, 0, entirePayload, 2, payload.length);

Is there a underlying reason this is a Java System call?

stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • Just because a function resides in the `System` class doesn't make it a "system call". `System` is just another Java class. – Marko Topolnik Feb 08 '13 at 20:35

1 Answers1

6

Perhaps arraycopy would have fitted better on java.util.Arrays, but that class wasn't added until Java 1.2, whereas arraycopy was there at the very beginning of Java. System seems like the next best place to have put it.

Note too that this method is implemented in native code to make it faster, so it is a bit special.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • I generally don't like native methods that much. The Math.sin() is ridiculously slow. –  Feb 08 '13 at 19:52
  • 1
    @Legend: Okay, well think back to Java 1.1, when the VM was nothing but a lowly bytecode interpreter and all array accesses were always bounds-checked. You'd like System.arrayCopy plenty. – Boann Feb 08 '13 at 20:00
  • @Boann +1 Not to mention block copy operations that are completely outside of reach of regular Java code. – Marko Topolnik Feb 08 '13 at 20:37