Search in sources :

Example 1 with DataCellSerializer

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);
}
Also used : DataCell(org.knime.core.data.DataCell) IOException(java.io.IOException) DataCellSerializer(org.knime.core.data.DataCellSerializer) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with DataCellSerializer

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);
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BlobDataCell(org.knime.core.data.container.BlobDataCell) DataCell(org.knime.core.data.DataCell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataCellSerializer(org.knime.core.data.DataCellSerializer)

Example 3 with DataCellSerializer

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);
        }
    };
}
Also used : Random(java.util.Random) DataCellDataInput(org.knime.core.data.DataCellDataInput) DataCellDataOutput(org.knime.core.data.DataCellDataOutput) DataCellSerializer(org.knime.core.data.DataCellSerializer)

Aggregations

DataCellSerializer (org.knime.core.data.DataCellSerializer)3 IOException (java.io.IOException)2 DataCell (org.knime.core.data.DataCell)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 Random (java.util.Random)1 DataCellDataInput (org.knime.core.data.DataCellDataInput)1 DataCellDataOutput (org.knime.core.data.DataCellDataOutput)1 BlobDataCell (org.knime.core.data.container.BlobDataCell)1