private void encode(ScilabString var, VectorOfDouble vec) { // header encodeHeader(var, vec, ScilabTypeEnum.sci_strings); // add the offset table which contains the offset of each UTF-8 encoded strings int offsetTableStart = vec.size(); vec.resize(offsetTableStart + var.getHeight() * var.getWidth()); int offsetTableAccumulated = 0; // encode the strings as UTF-8 and store the associated offset Charset utf8 = Charset.forName("UTF-8"); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { String str = var.getData()[i][j]; byte[] bytes = str.getBytes(utf8); // append the terminal '\0' final int requiredBytes = bytes.length + 1; final int doubleLen = (requiredBytes + Double.BYTES - 1) / Double.BYTES; // set the offset offsetTableAccumulated += doubleLen; vec.set(offsetTableStart++, offsetTableAccumulated); // push the data through a temporary byte buffer int index = vec.size(); vec.resize(index + doubleLen); vec.asByteBuffer(index, doubleLen).put(bytes).put((byte) 0); } } }
private void encode(ScilabBoolean var, VectorOfDouble vec) { // header encodeHeader(var, vec, ScilabTypeEnum.sci_boolean); // put all the boolean as int accordingly to Scilab 6 implementation final int requiredBytes = Integer.BYTES * var.getHeight() * var.getWidth(); final int doubleLen = (requiredBytes + Double.BYTES - 1) / Double.BYTES; int index = vec.size(); vec.resize(index + doubleLen); ByteBuffer buffer = vec.asByteBuffer(index, doubleLen); for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { buffer.putInt(var.getData()[i][j] ? 1 : 0); } } }
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; } } } }