Search in sources :

Example 1 with PortObjectZipOutputStream

use of org.knime.core.node.port.PortObjectZipOutputStream in project knime-core by knime.

the class Node method copyPortObject.

/**
 * Copies the PortObject so that the copy can be given to the node model
 * implementation (and potentially modified). The copy is carried out by
 * means of the respective serializer (via streams).
 *
 * <p> Note that this method is meant to be used by the framework only.
 * @param portObject The object to be copied.
 * @param exec For progress/cancel
 * @return The (deep) copy.
 * @throws IOException In case of exceptions while accessing the stream or
 * if the argument is an instance of {@link BufferedDataTable}.
 * @throws CanceledExecutionException If canceled.
 */
public static PortObject copyPortObject(final PortObject portObject, final ExecutionContext exec) throws IOException, CanceledExecutionException {
    if (portObject instanceof BufferedDataTable) {
        throw new IOException("Can't copy BufferedDataTable objects");
    }
    // first copy the spec, then copy the object
    final PortObjectSpec s = portObject.getSpec();
    PortObjectSpec.PortObjectSpecSerializer ser = PortTypeRegistry.getInstance().getSpecSerializer(s.getClass()).get();
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(10 * 1024);
    PortObjectSpecZipOutputStream specOut = PortUtil.getPortObjectSpecZipOutputStream(byteOut);
    specOut.setLevel(0);
    ser.savePortObjectSpec(s, specOut);
    specOut.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    PortObjectSpecZipInputStream specIn = PortUtil.getPortObjectSpecZipInputStream(byteIn);
    PortObjectSpec specCopy = ser.loadPortObjectSpec(specIn);
    specIn.close();
    DeferredFileOutputStream deferredOutputStream = new DeferredFileOutputStream(/* 10 MB */
    10 * 1024 * 1024, "knime-portobject-copy-", ".bin", new File(KNIMEConstants.getKNIMETempDir()));
    PortObject.PortObjectSerializer obSer = PortTypeRegistry.getInstance().getObjectSerializer(portObject.getClass()).get();
    PortObjectZipOutputStream objOut = PortUtil.getPortObjectZipOutputStream(deferredOutputStream);
    objOut.setLevel(0);
    obSer.savePortObject(portObject, objOut, exec);
    objOut.close();
    InputStream inStream;
    if (deferredOutputStream.isInMemory()) {
        inStream = new ByteArrayInputStream(deferredOutputStream.getData());
    } else {
        inStream = new BufferedInputStream(new FileInputStream(deferredOutputStream.getFile()));
    }
    PortObjectZipInputStream objIn = PortUtil.getPortObjectZipInputStream(inStream);
    PortObject result = obSer.loadPortObject(objIn, specCopy, exec);
    objIn.close();
    if (portObject instanceof FileStorePortObject) {
        FileStorePortObject sourceFSObj = (FileStorePortObject) portObject;
        FileStorePortObject resultFSObj = (FileStorePortObject) result;
        FileStoreUtil.retrieveFileStoreHandlers(sourceFSObj, resultFSObj, exec.getFileStoreHandler());
    }
    if (!deferredOutputStream.isInMemory()) {
        deferredOutputStream.getFile().delete();
    }
    return result;
}
Also used : PortObjectSpecZipOutputStream(org.knime.core.node.port.PortObjectSpecZipOutputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) PortObjectSpecZipInputStream(org.knime.core.node.port.PortObjectSpecZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PortObjectZipInputStream(org.knime.core.node.port.PortObjectZipInputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FileInputStream(java.io.FileInputStream) PortObjectZipOutputStream(org.knime.core.node.port.PortObjectZipOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) PortObjectSpecZipInputStream(org.knime.core.node.port.PortObjectSpecZipInputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Example 2 with PortObjectZipOutputStream

use of org.knime.core.node.port.PortObjectZipOutputStream in project knime-core by knime.

the class FileNodePersistor method savePortObject.

private static void savePortObject(final PortObjectSpec spec, final PortObject object, final File portDir, final NodeSettingsWO settings, final ExecutionMonitor exec) throws IOException, FileNotFoundException, CanceledExecutionException {
    settings.addString("port_spec_class", spec.getClass().getName());
    settings.addString("port_object_class", object.getClass().getName());
    String specDirName = "spec";
    String specFileName = "spec.zip";
    String specPath = specDirName + "/" + specFileName;
    File specDir = createDirectory(new File(portDir, specDirName));
    File specFile = new File(specDir, specFileName);
    settings.addString("port_spec_location", specPath);
    try (PortObjectSpecZipOutputStream out = PortUtil.getPortObjectSpecZipOutputStream(new BufferedOutputStream(new FileOutputStream(specFile)))) {
        PortObjectSpecSerializer serializer = PortTypeRegistry.getInstance().getSpecSerializer(spec.getClass()).get();
        serializer.savePortObjectSpec(spec, out);
    }
    String objectDirName = null;
    objectDirName = "object";
    File objectDir = createDirectory(new File(portDir, objectDirName));
    String objectPath;
    String objectFileName = "portobject.zip";
    objectPath = objectDirName + "/" + objectFileName;
    settings.addString("port_object_location", objectPath);
    File file = new File(objectDir, objectFileName);
    try (PortObjectZipOutputStream out = PortUtil.getPortObjectZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
        PortObjectSerializer serializer = PortTypeRegistry.getInstance().getObjectSerializer(object.getClass()).get();
        serializer.savePortObject(object, out, exec);
        if (object instanceof FileStorePortObject) {
            List<FileStoreKey> fileStoreKeys = FileStoreUtil.getFileStores((FileStorePortObject) object).stream().map(FileStoreUtil::getFileStoreKey).collect(Collectors.toList());
            File fileStoreXML = new File(objectDir, "filestore.xml");
            final ModelContent fileStoreModelContent = new ModelContent("filestore");
            ModelContentWO keysContent = fileStoreModelContent.addModelContent("filestore_keys");
            for (int i = 0; i < fileStoreKeys.size(); i++) {
                FileStoreKey key = fileStoreKeys.get(i);
                ModelContentWO keyContent = keysContent.addModelContent("filestore_key_" + i);
                key.save(keyContent);
            }
            fileStoreModelContent.saveToXML(new FileOutputStream(fileStoreXML));
        }
    }
}
Also used : PortObjectSpecZipOutputStream(org.knime.core.node.port.PortObjectSpecZipOutputStream) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) PortObjectZipOutputStream(org.knime.core.node.port.PortObjectZipOutputStream) FileStoreKey(org.knime.core.data.filestore.FileStoreKey) FileOutputStream(java.io.FileOutputStream) PortObjectSpecSerializer(org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer) ReferencedFile(org.knime.core.internal.ReferencedFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PortObjectSerializer(org.knime.core.node.port.PortObject.PortObjectSerializer)

Aggregations

File (java.io.File)2 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)2 ReferencedFile (org.knime.core.internal.ReferencedFile)2 PortObjectSpecZipOutputStream (org.knime.core.node.port.PortObjectSpecZipOutputStream)2 PortObjectZipOutputStream (org.knime.core.node.port.PortObjectZipOutputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1 FileStoreKey (org.knime.core.data.filestore.FileStoreKey)1 PortObject (org.knime.core.node.port.PortObject)1 PortObjectSerializer (org.knime.core.node.port.PortObject.PortObjectSerializer)1 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)1 PortObjectSpecSerializer (org.knime.core.node.port.PortObjectSpec.PortObjectSpecSerializer)1 PortObjectSpecZipInputStream (org.knime.core.node.port.PortObjectSpecZipInputStream)1