--- /dev/null
+package net.sf.openrocket.util;
+
+import java.lang.reflect.Array;
+
+public class ArrayUtils {
+
+ /**
+ * Implementation of java.util.Arrays.copyOfRange
+ *
+ * Since Froyo does not include this function it must be implemented here.
+ *
+ * @param original
+ * @param start
+ * @param end
+ * @return
+ */
+ public static <T> T[] copyOfRange( T[] original, int start, int end ) {
+
+ if ( original == null ) {
+ throw new NullPointerException();
+ }
+
+ if ( start < 0 || start > original.length ) {
+ throw new ArrayIndexOutOfBoundsException();
+ }
+
+ if ( start > end ) {
+ throw new IllegalArgumentException();
+ }
+
+ T[] result = (T[]) Array.newInstance( original.getClass().getComponentType(), end-start );
+
+ int index = 0;
+ int stop = original.length < end ? original.length : end;
+ for ( int i = start; i < stop; i ++ ) {
+ if ( i < original.length ) {
+ result[index] = original[i];
+ }
+ index++;
+ }
+
+ return result;
+
+ }
+
+}
--- /dev/null
+package net.sf.openrocket.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+public class ArrayUtilsTest {
+
+ @Test(expected=NullPointerException.class)
+ public void testCopyOfRange_NullArg() {
+ ArrayUtils.copyOfRange( (Byte[]) null, 0 , 14);
+ }
+
+ @Test(expected=ArrayIndexOutOfBoundsException.class)
+ public void testCopyOfRange_StartTooBig() {
+ Integer[] original = new Integer[5];
+ ArrayUtils.copyOfRange( original, 8 , 14);
+ }
+
+ @Test(expected=ArrayIndexOutOfBoundsException.class)
+ public void testCopyOfRange_StartTooSmall() {
+ Integer[] original = new Integer[5];
+ ArrayUtils.copyOfRange( original, -1 , 14);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testCopyOfRange_IllegalRange() {
+ Integer[] original = new Integer[5];
+ ArrayUtils.copyOfRange( original, 5, 0 );
+ }
+
+ @Test
+ public void testCopyOfRange() {
+ Integer[] original = new Integer[5];
+ for ( int i =0; i < 5; i++ ) {
+ original[i] = i;
+ }
+ Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
+ assertEquals( 0, copy.length );
+
+ copy = ArrayUtils.copyOfRange( original, 2, 2 );
+ assertEquals( 0, copy.length );
+
+ copy = ArrayUtils.copyOfRange( original, 0, 2 );
+ assertEquals( 2, copy.length );
+ for( int i =0; i< 2; i++ ) {
+ assertEquals( original[i], copy[i] );
+ }
+
+ copy = ArrayUtils.copyOfRange( original, 2, 5 );
+ assertEquals( 3, copy.length );
+ for( int i =0; i< 3; i++ ) {
+ assertEquals( original[i+2], copy[i] );
+ }
+
+ copy = ArrayUtils.copyOfRange( original, 2, 15 );
+ assertEquals( 13, copy.length );
+ for( int i =0; i< 3; i++ ) {
+ assertEquals( original[i+2], copy[i] );
+ }
+ for ( int i=3; i< 13; i++ ) {
+ assertNull(copy[i]);
+ }
+
+ }
+
+ @Test
+ public void testCopyOfRange_ZeroSize() {
+ Integer[] original = new Integer[0];
+
+ Integer[] copy = ArrayUtils.copyOfRange( original, 0, 0 );
+ assertEquals( 0, copy.length );
+
+ copy = ArrayUtils.copyOfRange( original, 0, 2 );
+ assertEquals( 2, copy.length );
+ for( int i =0; i< 2; i++ ) {
+ assertEquals( null, copy[i] );
+ }
+
+ }
+
+
+}