package org.rcsb.mmtf.decoder;

import java.util.ArrayList;
import java.util.List;

import org.rcsb.mmtf.utils.CodecUtils;

/**
 * A class of array decoders.
 * @author Anthony Bradley
 *
 */
public class ArrayDecoders {

	/**
	 * Delta decode an array of integers.
	 * @param intArray the input array of integers
	 * @return a decoded integer array.
	 */
	public static int[] deltaDecode(int[] intArray) {
		int[] outArray = new int[intArray.length];
		System.arraycopy(intArray, 0, outArray, 0, intArray.length);
		for (int i = 1; i < outArray.length; i++)  {
			outArray[i] = outArray[i-1] + outArray[i];
		}
		return outArray;

	}

	/**
	 * Run length decode an array of integers.
	 * @param integerArray the input data
	 * @return the decoded integer array
	 */
	public static int[] runlengthDecode(int[] integerArray) {
		// We don't know the length so need a List 
		List<Integer> outList = new ArrayList<>();
		for (int i=0; i<integerArray.length; i+=2) {
			int currentInt = integerArray[i];
			int currentCount = integerArray[i+1];
			for (int j=0; j<currentCount;j++){
				outList.add(currentInt);
			}
		}
		return CodecUtils.convertToIntArray(outList);
	}
}

@@ -828,92 +828,181 @@
 %09// 
-We don't know the length so need a List %0A%09%09List%3CInteger%3E outList = new ArrayList%3C%3E()
+Calculate the length%0A%09%09int totCount =0;%0A%09%09for(int i=0; i%3CintegerArray.length;i+=2)%7B%0A%09%09%09totCount+=integerArray%5Bi+1%5D;%0A%09%09%7D%0A%09%09int%5B%5D outArray = new int%5BtotCount%5D;%0A%09%09int index = 0
 ;%0A%09%09
@@ -1173,85 +1173,74 @@
 %09out
-List.add(currentInt);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return CodecUtils.convertToIntArray(outList)
+Array%5Bindex%5D = currentInt;%0A%09%09%09%09index++;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return outArray
 ;%0A%09%7D

Use array for run lenght decoding