/** * Helper method to add an header of the detected type * * @param var the scilab matrix type to encode * @param vec the raw encoded data container * @param as the type to encode * @param detected the detected type */ @SuppressWarnings({"unchecked", "fallthrough"}) private void encodeHeader(Object var, VectorOfDouble vec, final ScilabTypeEnum as) { ScilabType matrix = null; ArrayList<ScilabType> list = null; // defensive programming switch (as) { case sci_boolean: case sci_ints: case sci_matrix: case sci_strings: matrix = (ScilabType) var; break; case sci_list: case sci_mlist: case sci_tlist: list = (ArrayList<ScilabType>) var; break; default: throw new IllegalArgumentException(); } vec.add(as.swigValue()); if (matrix instanceof ScilabInteger) { vec.add(((ScilabInteger) matrix).getPrec().swigValue()); } if (matrix != null) { vec.add(2); vec.add(matrix.getHeight()); vec.add(matrix.getWidth()); } else if (list != null) { vec.add(list.size()); } else { throw new IllegalArgumentException(); } }
private void decodeToJava(VectorOfDouble vec, ArrayList<Object> arguments) { int nativeScilabType = (int) vec.get(position++); // specific integer sub-type int precision = 0; // for data[][]-based type int height = 0; int width = 0; // for ArrayList-based type int listLen = 0; final ScilabTypeEnum type = ScilabTypeEnum.swigToEnum(nativeScilabType); switch (type) { case sci_ints: // special case for integer precision precision = (int) vec.get(position++); case sci_matrix: case sci_boolean: case sci_strings: position++; // n-Dims not managed height = (int) vec.get(position++); width = (int) vec.get(position++); break; case sci_list: case sci_mlist: case sci_tlist: listLen = (int) vec.get(position++); break; default: throw new IllegalArgumentException(); } // special case for complex double matrix boolean isComplex = false; if (type == ScilabTypeEnum.sci_matrix) { isComplex = vec.get(position++) != 0; } // allocate the right type with the decoded properties switch (type) { case sci_matrix: for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(vec.get(position++)); } } if (isComplex) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(vec.get(position++)); } } } break; case sci_boolean: { final int doubleLen = (Integer.BYTES * height * width) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(view.getInt() != 0); } } position += doubleLen; } break; case sci_ints: switch (ScilabIntegerTypeEnum.swigToEnum(precision)) { case sci_int8: case sci_uint8: { final int sizeof = Byte.BYTES; final int doubleLen = (sizeof * height * width) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(view.get()); } } } break; case sci_int16: case sci_uint16: { final int sizeof = Short.BYTES; final int doubleLen = (sizeof * height * width) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(view.getShort()); } } } break; case sci_int32: case sci_uint32: { final int sizeof = Integer.BYTES; final int doubleLen = (sizeof * height * width) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(view.getInt()); } } } break; case sci_int64: case sci_uint64: { final int sizeof = Long.BYTES; final int doubleLen = (sizeof * height * width) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { arguments.add(view.getLong()); } } } break; } break; case sci_strings: decodeString(height, width, vec, (i, j, v) -> arguments.add(v)); break; case sci_list: case sci_mlist: case sci_tlist: { for (int i = 0; i < listLen; i++) { decodeToJava(vec, arguments); } } break; default: throw new IllegalArgumentException(); } }
@SuppressWarnings("fallthrough") private ScilabType decodeHeader(VectorOfDouble vec) { int nativeScilabType = (int) vec.get(position++); // specific integer sub-type int precision = 0; // for data[][]-based type int height = 0; int width = 0; // for ArrayList-based type int listLen = 0; final ScilabTypeEnum type = ScilabTypeEnum.swigToEnum(nativeScilabType); switch (type) { case sci_ints: // special case for integer precision precision = (int) vec.get(position++); case sci_matrix: case sci_boolean: case sci_strings: position++; // n-Dims not managed height = (int) vec.get(position++); width = (int) vec.get(position++); break; case sci_list: case sci_mlist: case sci_tlist: listLen = (int) vec.get(position++); break; default: throw new IllegalArgumentException(); } // special case for complex double matrix double[][] imagData = null; if (type == ScilabTypeEnum.sci_matrix) { boolean isComplex = vec.get(position++) != 0; if (isComplex) { imagData = new double[height][width]; } } // allocate the right type with the decoded properties switch (type) { case sci_matrix: if (height * width == 0) { return new ScilabDouble(); } else { return new ScilabDouble(new double[height][width], imagData); } case sci_boolean: if (height * width == 0) { return new ScilabBoolean(); } else { return new ScilabBoolean(new boolean[height][width]); } case sci_ints: if (height * width == 0) { return new ScilabInteger(); } else { switch (ScilabIntegerTypeEnum.swigToEnum(precision)) { case sci_int8: return new ScilabInteger(new byte[height][width], false); case sci_int16: return new ScilabInteger(new short[height][width], false); case sci_int32: return new ScilabInteger(new int[height][width], false); case sci_int64: return new ScilabInteger(new long[height][width], false); case sci_uint8: return new ScilabInteger(new byte[height][width], true); case sci_uint16: return new ScilabInteger(new short[height][width], true); case sci_uint32: return new ScilabInteger(new int[height][width], true); case sci_uint64: return new ScilabInteger(new long[height][width], true); } } case sci_strings: if (height * width == 0) { return new ScilabString(); } else { return new ScilabString(new String[height][width]); } case sci_list: return new ScilabList(Collections.nCopies(listLen, null)); case sci_mlist: return new ScilabMList(new String[listLen - 1], Collections.nCopies(listLen - 1, null)); case sci_tlist: return new ScilabTList(new String[listLen - 1], Collections.nCopies(listLen - 1, null)); default: throw new IllegalArgumentException(); } }