use of org.knime.core.node.port.PortObjectSpecZipInputStream 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;
}
use of org.knime.core.node.port.PortObjectSpecZipInputStream in project knime-core by knime.
the class FileNodePersistor method loadPortObject.
/**
* @param portDir
* @param settings
* @param exec
* @param fileStoreHandlerRepository
* @return
* @throws IOException
* @throws InvalidSettingsException
* @throws FileNotFoundException
* @throws CanceledExecutionException
*/
private Optional<PortObject> loadPortObject(final ReferencedFile portDir, final NodeSettingsRO settings, final ExecutionMonitor exec, final FileStoreHandlerRepository fileStoreHandlerRepository) throws IOException, InvalidSettingsException, FileNotFoundException, CanceledExecutionException {
exec.setMessage("Loading port object");
final String specClass = settings.getString("port_spec_class");
final String objectClass = loadPortObjectClassName(settings);
PortObject object = null;
PortObjectSpec spec = null;
if (specClass != null) {
Class<? extends PortObjectSpec> cl = PortTypeRegistry.getInstance().getSpecClass(specClass).orElseThrow(() -> new IOException("Invalid spec class \"" + specClass + "\""));
ReferencedFile specDirRef = new ReferencedFile(portDir, settings.getString("port_spec_location"));
File specFile = specDirRef.getFile();
if (!specFile.isFile()) {
throw new IOException("Can't read spec file " + specFile.getAbsolutePath());
}
try (PortObjectSpecZipInputStream in = PortUtil.getPortObjectSpecZipInputStream(new BufferedInputStream(new FileInputStream(specFile)))) {
PortObjectSpecSerializer<?> serializer = PortTypeRegistry.getInstance().getSpecSerializer(cl).get();
spec = serializer.loadPortObjectSpec(in);
if (spec == null) {
throw new IOException("Serializer \"" + serializer.getClass().getName() + "\" restored null spec ");
}
}
}
if (spec != null && objectClass != null) {
Class<? extends PortObject> cl = PortTypeRegistry.getInstance().getObjectClass(objectClass).orElseThrow(() -> new IOException("Invalid object class \"" + objectClass + "\""));
ReferencedFile objectFileRef = new ReferencedFile(portDir, settings.getString("port_object_location"));
File objectFile = objectFileRef.getFile();
if (!objectFile.isFile()) {
throw new IOException("Can't read file " + objectFile.getAbsolutePath());
}
// buffering both disc I/O and the gzip stream pays off
try (PortObjectZipInputStream in = PortUtil.getPortObjectZipInputStream(new BufferedInputStream(new FileInputStream(objectFile)))) {
PortObjectSerializer<?> serializer = PortTypeRegistry.getInstance().getObjectSerializer(cl).get();
object = serializer.loadPortObject(in, spec, exec);
}
if (object instanceof FileStorePortObject) {
File fileStoreXML = new File(objectFile.getParent(), "filestore.xml");
final ModelContentRO fileStoreModelContent = ModelContent.loadFromXML(new FileInputStream(fileStoreXML));
List<FileStoreKey> fileStoreKeys = new ArrayList<FileStoreKey>();
if (getLoadVersion().isOlderThan(LoadVersion.V2100)) {
// only one filestore in <2.10 (bug 5227)
FileStoreKey fileStoreKey = FileStoreKey.load(fileStoreModelContent);
fileStoreKeys.add(fileStoreKey);
} else {
ModelContentRO keysContent = fileStoreModelContent.getModelContent("filestore_keys");
for (String id : keysContent.keySet()) {
ModelContentRO keyContent = keysContent.getModelContent(id);
fileStoreKeys.add(FileStoreKey.load(keyContent));
}
}
FileStoreUtil.retrieveFileStoreHandlerFrom((FileStorePortObject) object, fileStoreKeys, fileStoreHandlerRepository);
}
}
return Optional.ofNullable(object);
}
Aggregations