RleProgram.java - import java.lang.reflect.Array import...

This preview shows page 1 - 3 out of 6 pages.

import java.lang.reflect.Array; import java.util.*; public class RleProgram { // METHODS public static String toHexString(byte[] data) { // Translates data (RLE or raw) a hexadecimal string (without delimiters).This method can also aid debugging. // array of bytes (0-15) into hexadecimal value String result = ""; for (byte datum : data) { switch (String.valueOf(datum)) { case "10": result += "a"; break; case "11": result += "b"; break; case "12": result += "c"; break; case "13": result += "d"; break; case "14": result += "e"; break; case "15": result += "f"; break; default: result += datum; break; } } return result; } public static int countRuns(byte[] flatData) { int numRuns = 1; int runLen = 0; int currNum = flatData[0]; for (byte datum : flatData) { if (datum != currNum) { currNum = datum; numRuns += 1; } else { runLen++; if(runLen == 15) { runLen = 0; numRuns++; } } } return numRuns; } public static byte[] encodeRle(byte[] flatData) { // {15, 15, 15, 6, 6} -> {3, 15, 2, 6} int numRuns = countRuns(flatData); int num = 1; int index = 0;
byte[] result = new byte[numRuns * 2]; for (int i = 0; i < flatData.length; i++) { if (i < flatData.length - 1 && flatData[i] == flatData[i + 1]) { num++; } else { result[index] = (byte) num; result[++index] = flatData[i]; num = 1; index++; } } return result; } public static int getDecodedLength(byte[] rleData) { int length = 0; for (int i = 0; i < rleData.length; i++) { if (i % 2 == 0) { length += rleData[i]; } } return length; } public static byte[] decodeRle(byte[] rleData) { byte[] numElementsPerRun = new byte[rleData.length / 2]; byte[] elementsInRuns = new byte[rleData.length / 2]; for (int i = 0; i < rleData.length; i++) { if (i % 2 == 0) {

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture