use of org.knime.core.data.util.NonClosableOutputStream in project knime-core by knime.
the class PMMLPortObjectSpec method saveTo.
/**
* @param out zipped stream to write the entries to
* @throws IOException if something goes wrong
*/
public void saveTo(final PortObjectSpecZipOutputStream out) throws IOException {
NodeSettings settings = new NodeSettings(DTS_KEY);
m_dataTableSpec.save(settings);
NonClosableOutputStream noCloseOut = new NonClosableOutputStream(out);
out.putNextEntry(new ZipEntry(DTS_FILE));
settings.saveToXML(noCloseOut);
NodeSettings miningSchema = new NodeSettings(MINING_SCHEMA_KEY);
miningSchema.addStringArray(LEARNING_KEY, m_learningFields.toArray(new String[0]));
miningSchema.addStringArray(TARGET_KEY, m_predictedFields.toArray(new String[0]));
out.putNextEntry(new ZipEntry(MINING_SCHEMA_FILE));
miningSchema.saveToXML(noCloseOut);
out.putNextEntry(new ZipEntry(PREPROC_COL_FILE));
NodeSettings preprocessing = new NodeSettings(PREPROC_KEY);
preprocessing.addStringArray(PREPROC_COL_KEY, m_preprocFields.toArray(new String[0]));
preprocessing.saveToXML(noCloseOut);
out.close();
}
use of org.knime.core.data.util.NonClosableOutputStream in project knime-core by knime.
the class Buffer method addToZipFile.
/**
* Method that's been called from the {@link ContainerTable} to save the content. It will add zip entries to the
* <code>zipOut</code> argument and not close the output stream when done, allowing to add additional content
* elsewhere (for instance the <code>DataTableSpec</code>).
*
* @param zipOut To write to.
* @param exec For progress/cancel
* @throws IOException If it fails to write to a file.
* @throws CanceledExecutionException If canceled.
* @see org.knime.core.node.BufferedDataTable.KnowsRowCountTable #saveToFile(File, NodeSettingsWO, ExecutionMonitor)
*/
synchronized void addToZipFile(final ZipOutputStream zipOut, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
if (m_spec == null) {
throw new IOException("Can't save an open Buffer.");
}
// binary data is already deflated
if (ZLIB_SUPPORTS_LEVEL_SWITCH_AP8083) {
zipOut.setLevel(Deflater.NO_COMPRESSION);
}
zipOut.putNextEntry(new ZipEntry(ZIP_ENTRY_DATA));
if (!usesOutFile() || m_version < IVERSION) {
// need to use new buffer since we otherwise write properties
// of this buffer, which prevents it from further reading (version
// conflict) - see bug #1364
Buffer copy = createLocalCloneForWriting();
File tempFile = null;
try {
copy.initOutputWriter(new NonClosableOutputStream.Zip(zipOut));
} catch (UnsupportedOperationException notSupported) {
tempFile = DataContainer.createTempFile(copy.m_outputFormat.getFilenameSuffix());
copy.initOutputWriter(tempFile);
}
int count = 1;
for (RowIterator it = iterator(); it.hasNext(); ) {
final BlobSupportDataRow row = (BlobSupportDataRow) it.next();
final int countCurrent = count;
exec.setProgress(count / (double) size(), () -> "Writing row " + countCurrent + " (\"" + row.getKey() + "\")");
exec.checkCanceled();
// make a deep copy of blobs if we have a version hop
copy.addRow(row, m_version < IVERSION, false);
count++;
}
synchronized (copy) {
copy.closeInternal();
}
if (tempFile != null) {
try (InputStream in = new FileInputStream(tempFile)) {
IOUtils.copyLarge(in, new NonClosableOutputStream(zipOut));
} finally {
tempFile.delete();
}
}
// in this if-statement
if (usesOutFile()) {
// can safely be set to null because it wrote to stream already
copy.m_list = null;
}
File blobDir = m_blobDir;
// (otherwise its blob dir will be empty
if (m_version < IVERSION) {
blobDir = copy.m_blobDir;
} else {
assert copy.m_blobDir == null;
}
if (blobDir != null) {
addToZip(ZIP_ENTRY_BLOBS, zipOut, blobDir);
}
if (hasOwnFileStoreCells()) {
addToZip(ZIP_ENTRY_FILESTORES, zipOut, getOwnFileStoreCellsDirectory());
}
zipOut.closeEntry();
if (ZLIB_SUPPORTS_LEVEL_SWITCH_AP8083) {
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
}
zipOut.putNextEntry(new ZipEntry(ZIP_ENTRY_META));
copy.writeMetaToFile(new NonClosableOutputStream.Zip(zipOut));
} else {
// does the buffering itself
try (InputStream is = new FileInputStream(m_binFile)) {
FileUtil.copy(is, zipOut);
}
if (m_blobDir != null) {
addToZip(ZIP_ENTRY_BLOBS, zipOut, m_blobDir);
}
if (hasOwnFileStoreCells()) {
addToZip(ZIP_ENTRY_FILESTORES, zipOut, getOwnFileStoreCellsDirectory());
}
zipOut.closeEntry();
if (ZLIB_SUPPORTS_LEVEL_SWITCH_AP8083) {
zipOut.setLevel(Deflater.DEFAULT_COMPRESSION);
}
zipOut.putNextEntry(new ZipEntry(ZIP_ENTRY_META));
writeMetaToFile(new NonClosableOutputStream.Zip(zipOut));
}
}
Aggregations