private ScilabType decode(VectorOfDouble vec, ScilabInteger var) { final int sizeof; long[][] longData = null; short[][] shortData = null; int[][] intData = null; byte[][] byteData = null; switch (var.getPrec()) { case sci_int64: case sci_uint64: sizeof = Long.BYTES; longData = var.getDataAsLong(); break; case sci_int32: case sci_uint32: sizeof = Integer.BYTES; intData = var.getDataAsInt(); break; case sci_int16: case sci_uint16: sizeof = Short.BYTES; shortData = var.getDataAsShort(); break; case sci_int8: case sci_uint8: sizeof = Byte.BYTES; byteData = var.getDataAsByte(); break; default: throw new IllegalArgumentException(); } final int doubleLen = (sizeof * var.getHeight() * var.getWidth()) / Double.BYTES + 1; ByteBuffer view = vec.asByteBuffer(position, doubleLen); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { switch (var.getPrec()) { case sci_int64: case sci_uint64: longData[i][j] = view.getLong(); break; case sci_int32: case sci_uint32: intData[i][j] = view.getInt(); break; case sci_int16: case sci_uint16: shortData[i][j] = view.getShort(); break; case sci_int8: case sci_uint8: byteData[i][j] = view.get(); break; } } } position += doubleLen; return var; }
private void encode(ScilabInteger var, VectorOfDouble vec) { // pre-processing: retrieve the raw data per type int sizeof; long[][] longData = null; short[][] shortData = null; int[][] intData = null; byte[][] byteData = null; switch (var.getPrec()) { case sci_int64: case sci_uint64: sizeof = Long.BYTES; longData = var.getDataAsLong(); break; case sci_int32: case sci_uint32: sizeof = Integer.BYTES; intData = var.getDataAsInt(); break; case sci_int16: case sci_uint16: sizeof = Short.BYTES; shortData = var.getDataAsShort(); break; case sci_int8: case sci_uint8: sizeof = Byte.BYTES; byteData = var.getDataAsByte(); break; default: throw new IllegalArgumentException(); } // Header encodeHeader(var, vec, ScilabTypeEnum.sci_ints); // push the data on a pre-allocated space final int requiredBytes = sizeof * var.getHeight() * var.getWidth(); final int doubleLen = (requiredBytes + Double.BYTES - 1) / Double.BYTES; final int index = vec.size(); vec.resize(index + doubleLen); ByteBuffer view = vec.asByteBuffer(index, doubleLen); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { switch (var.getPrec()) { case sci_int64: case sci_uint64: view.putLong(longData[i][j]); break; case sci_int32: case sci_uint32: view.putInt(intData[i][j]); break; case sci_int16: case sci_uint16: view.putShort(shortData[i][j]); break; case sci_int8: case sci_uint8: view.put(byteData[i][j]); break; } } } }