use of org.knime.core.data.DataCellSerializer in project knime-core by knime.
the class DictEncodedDataCellDataInputDelegator method readDataCellImpl.
@Override
protected DataCell readDataCellImpl() throws IOException {
Optional<DataCellSerializer<DataCell>> serializer = Optional.empty();
if (m_classNames.isEmpty()) {
throw new IllegalStateException("No more DataCell type information available for this stream");
}
final var className = m_classNames.remove(0);
if (className.isEmpty()) {
throw new IllegalStateException("Cannot read DataCell with empty type information");
}
final var cellClass = REGISTRY.getCellClass(className);
if (!cellClass.isEmpty()) {
serializer = REGISTRY.getSerializer(cellClass.get());
}
if (serializer.isEmpty()) {
// fall back to java serialization
try {
final ObjectInputStream ois = new ObjectInputStream(this);
return (DataCell) ois.readObject();
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}
return serializer.get().deserialize(this);
}
use of org.knime.core.data.DataCellSerializer in project knime-core by knime.
the class PortObjectRepository method cloneBlobCell.
/**
* Deep-clones a data cell. Most important for blob cell to get rid
* of their blob address.
* @param blobCell The cell to clone.
* @return A clone copy of the arg
* @throws IOException If that fails for any reason.
*/
private static DataCell cloneBlobCell(final DataCell blobCell) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataCellCloneObjectOutputStream out = new DataCellCloneObjectOutputStream(bos);
Optional<DataCellSerializer<DataCell>> cellSerializer = DataTypeRegistry.getInstance().getSerializer(blobCell.getClass());
if (cellSerializer.isPresent()) {
cellSerializer.get().serialize(blobCell, out);
} else {
out.writeObject(blobCell);
}
out.close();
try (DataCellCloneObjectInputStream in = new DataCellCloneObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), blobCell.getClass().getClassLoader())) {
if (cellSerializer.isPresent()) {
return cellSerializer.get().deserialize(in);
} else {
try {
return (DataCell) in.readObject();
} catch (ClassNotFoundException e) {
throw new IOException("Can't read from object input stream: " + e.getMessage(), e);
}
}
}
}
use of org.knime.core.data.DataCellSerializer in project knime-core by knime.
the class LargeBlobCell method getCellSerializer.
public static final DataCellSerializer<LargeBlobCell> getCellSerializer() {
return new DataCellSerializer<LargeBlobCell>() {
private final Random m_random;
{
long time = System.currentTimeMillis();
NodeLogger.getLogger(LargeBlobCell.class).debug("Using seed " + time);
m_random = new Random(time);
}
/**
* {@inheritDoc}
*/
@Override
public LargeBlobCell deserialize(final DataCellDataInput input) throws IOException {
int sizeOfCell = input.readInt();
CheckUtils.checkArgument(sizeOfCell >= 0, "Negative size %d", sizeOfCell);
for (int i = 0; i < sizeOfCell / 2; i++) {
input.readByte();
}
String identifier = input.readUTF();
for (int i = 0; i < sizeOfCell / 2; i++) {
input.readByte();
}
return new LargeBlobCell(identifier, LargeBlobCell.SIZE_OF_CELL);
}
/**
* {@inheritDoc}
*/
@Override
public void serialize(final LargeBlobCell cell, final DataCellDataOutput output) throws IOException {
output.writeInt(cell.m_sizeOfCell);
byte[] ar = new byte[cell.m_sizeOfCell / 2];
m_random.nextBytes(ar);
output.write(ar);
output.writeUTF(cell.m_identifier);
m_random.nextBytes(ar);
output.write(ar);
}
};
}
Aggregations