Skip to main content

REXS V1.6

Java examples

Encoding of binary arrays

public static String encodeInt32Array(int[] array) {
	ByteBuffer buf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE * array.length);
	buf.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().put(array);
	byte[] byteArray = buf.array();
	return Base64.getEncoder().encodeToString(byteArray);
}
public static String encodeFloat64Array(double[] array) {
	ByteBuffer buf = ByteBuffer.allocate(Double.SIZE / Byte.SIZE * array.length);
	buf.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer().put(array);
	byte[] byteArray = buf.array();
	return Base64.getEncoder().encodeToString(byteArray);
}
public static String encodeFloat32Array(float[] array) {
	ByteBuffer buf = ByteBuffer.allocate(Float.SIZE / Byte.SIZE * array.length);
	buf.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().put(array);
	byte[] byteArray = buf.array();
	return Base64.getEncoder().encodeToString(byteArray);
}

Decoding of binary arrays

public static int[] decodeInt32Array(String base64Encoded) {
	byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
	IntBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
	int[] array = new int[buf.limit()];
	buf.get(array);
	return array;
}
public static double[] decodeFloat64Array(String base64Encoded) {
	byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
	DoubleBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer();
	double[] array = new double[buf.limit()];
	buf.get(array);
	return array;
}
public static float[] decodeFloat32Array(String base64Encoded) {
	byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
	FloatBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
	float[] array = new float[buf.limit()];
	buf.get(array);
	return array;
}

Encoding of binary matrices

public static String encodeInt32Matrix(int[][] matrix) {
    int[] array = flatIntMatrix(matrix);
    return encodeInt32Array(array);
}

private static int[] flatIntMatrix(int[][] matrix) {
    if (matrix.length==0)
	return new int[0]; 
    int arrayLength = 0;
    int numRows = matrix.length;
    int numColumns =  matrix[0].length;
    for (int i = 0; i < numRows; i++) {
	if (matrix[i].length!=numColumns)
        	throw new RexsModelAccessException("tried to encode non-rectangular matrix in base64");
	arrayLength += numColumns;
    }
    int[] array = new int[arrayLength];
    int arrayIndex = 0;
    for (int col = 0; col < numColumns; col++) {
	for (int row = 0; row < numRows; row++) {
		array[arrayIndex++] = matrix[row][col];
	}
    }
    return array;
}

public static String encodeInt32Array(int[] array) {
    ByteBuffer buf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE * array.length);
    buf.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().put(array);
    return Base64.getEncoder().encodeToString(buf.array());
}
public static String encodeFloat64Matrix(double[][] matrix) {
    double[] array = flatDoubleMatrix(matrix);
    return encodeFloat64Array(array);
}

private static double[] flatDoubleMatrix(double[][] matrix) {
    if (matrix.length==0)
	return new double[0]; 
    int arrayLength = 0;
    int numRows = matrix.length;
    int numColumns =  matrix[0].length;
    for (int i = 0; i < numRows; i++) {
	if (matrix[i].length!=numColumns)
        	throw new RexsModelAccessException("tried to encode non-rectangular matrix in base64");
	arrayLength += numColumns;
    }
    double[] array = new double[arrayLength];
    int arrayIndex = 0;
    for (int col = 0; col < numColumns; col++) {
	for (int row = 0; row < numRows; row++) {
		array[arrayIndex++] = matrix[row][col];
	}
    }
    return array;
}

public static String encodeFloat64Array(double[] array) {
    ByteBuffer buf = ByteBuffer.allocate(Double.SIZE / Byte.SIZE * array.length);
    buf.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer().put(array);
    return Base64.getEncoder().encodeToString(buf.array());
}
public static String encodeFloat32Matrix(double[][] matrix) {
    float[] array = flatFloatMatrix(matrix);
    return encodeFloat32Array(array);
}

private static float[] flatFloatMatrix(float[][] matrix) {
    if (matrix.length==0)
	return new float[0]; 
    int arrayLength = 0;
    int numRows = matrix.length;
    int numColumns =  matrix[0].length;
    for (int i = 0; i < numRows; i++) {
	if (matrix[i].length!=numColumns)
        	throw new RexsModelAccessException("tried to encode non-rectangular matrix in base64");
	arrayLength += numColumns;
    }
    float[] array = new float[arrayLength];
    int arrayIndex = 0;
    for (int col = 0; col < numColumns; col++) {
	for (int row = 0; row < numRows; row++) {
		array[arrayIndex++] = matrix[row][col];
	}
    }
    return array;
}

public static String encodeFloat32Array(float[] array) {
    ByteBuffer buf = ByteBuffer.allocate(Float.SIZE / Byte.SIZE * array.length);
    buf.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().put(array);
    return Base64.getEncoder().encodeToString(buf.array());
}

Decoding of binary matrices

public static int[][] decodeInt32Matrix(String base64Encoded, int rows, int cols) {
    if (isEmpty(base64Encoded))
	return null;
    int[] intArray = decodeInt32Array(base64Encoded);
    return unflatIntMatrix(intArray, rows, cols);
}

public static int[] decodeInt32Array(String base64Encoded) {
    if (isEmpty(base64Encoded))
	return null;
    byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
    IntBuffer buf = ByteBuffer.wrap(byteArray).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
    int[] array = new int[buf.limit()];
    buf.get(array);
    return array;
}

private static int[][] unflatIntMatrix(int[] array, int rows, int cols) {
    int[][] matrix = new int[rows][cols];
    for (int i = 0; i < array.length; i++) {
	int rowIndex = i % rows;
	int colIndex = i / rows;
	matrix[rowIndex][colIndex] = array[i];
    }
    return matrix;
}
public static double[][] decodeFloat64Matrix(String base64Encoded, int rows, int cols) {
    if (isEmpty(base64Encoded))
	return null;
    double[] doubleArray = decodeFloat64Array(base64Encoded);
    return unflatDoubleMatrix(doubleArray, rows, cols);
}

public static double[] decodeFloat64Array(String base64Encoded) {
    if (isEmpty(base64Encoded))
	return null;
    byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
    DoubleBuffer buf = ByteBuffer.wrap(byteArray).order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer();
    double[] array = new double[buf.limit()];
    buf.get(array);
    return array;
}

private static double[][] unflatDoubleMatrix(double[] array, int rows, int cols) {
    double[][] matrix = new double[rows][cols];
    for (int i = 0; i < array.length; i++) {
	int rowIndex = i % rows;
	int colIndex = i / rows;
	matrix[rowIndex][colIndex] = array[i];
    }
    return matrix;
}
public static double[][] decodeFloat32Matrix(String base64Encoded, int rows, int cols) {
    if (isEmpty(base64Encoded))
	return null;
    float[] floatArray = decodeFloat32Array(base64Encoded);
    return unflatfloatMatrix(floatArray, rows, cols);
}

public static float[] decodeFloat32Array(String base64Encoded) {
    if (isEmpty(base64Encoded))
	return null;
    byte[] byteArray = Base64.getDecoder().decode(base64Encoded);
    FloatBuffer buf = ByteBuffer.wrap(byteArray).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();
    float[] array = new float[buf.limit()];
    buf.get(array);
    return array;
}

private static float[][] unflatFloatMatrix(float[] array, int rows, int cols) {
    float[][] matrix = new float[rows][cols];
    for (int i = 0; i < array.length; i++) {
	int rowIndex = i % rows;
	int colIndex = i / rows;
	matrix[rowIndex][colIndex] = array[i];
    }
    return matrix;
}