Porting Arrays.copyOfRange from Java 6 to Java 5

I have the source code that I need to make runnable for Java 5. Undoubtedly, this code uses the Arrays.copyOfRange function , which was introduced only in Java 6. What would be the most efficient way to implement the same utility using only the Java 5 API?

+1
source share
3 answers

The OpenJDK 6 page will open - it is Java open source. You can download and read the source code yourself, find out how it is implemented, and add functionality to the application manually.

+1
source

Here is the code from OpenJDK for those who are interested:

public static byte[] copyOfRange(byte[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); byte[] copy = new byte[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; } 
+4
source

The fastest way is to use System.arraycopy . This is what is done by the Array class, BTW.

0
source

All Articles