Search in sources :

Example 1 with NonClosableOutputStream

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();
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) ZipEntry(java.util.zip.ZipEntry) NonClosableOutputStream(org.knime.core.data.util.NonClosableOutputStream)

Example 2 with NonClosableOutputStream

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));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) TableStoreCloseableRowIterator(org.knime.core.data.container.storage.AbstractTableStoreReader.TableStoreCloseableRowIterator) RowIterator(org.knime.core.data.RowIterator) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) NonClosableOutputStream(org.knime.core.data.util.NonClosableOutputStream)

Aggregations

ZipEntry (java.util.zip.ZipEntry)2 NonClosableOutputStream (org.knime.core.data.util.NonClosableOutputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 RowIterator (org.knime.core.data.RowIterator)1 TableStoreCloseableRowIterator (org.knime.core.data.container.storage.AbstractTableStoreReader.TableStoreCloseableRowIterator)1 NodeSettings (org.knime.core.node.NodeSettings)1