Search in sources :

Example 1 with NonCloseableOutputStream

use of org.apache.nifi.stream.io.NonCloseableOutputStream in project nifi by apache.

the class CompressableRecordWriter method resetWriteStream.

/**
 * Resets the streams to prepare for a new block
 *
 * @param eventId the first id that will be written to the new block
 * @throws IOException if unable to flush/close the current streams properly
 */
protected void resetWriteStream(final Long eventId) throws IOException {
    try {
        if (out != null) {
            out.flush();
        }
        final long byteOffset = (byteCountingOut == null) ? rawOutStream.getBytesWritten() : byteCountingOut.getBytesWritten();
        final TocWriter tocWriter = getTocWriter();
        if (compressed) {
            // We don't have to check if the writer is dirty because we will have already checked before calling this method.
            if (out != null) {
                out.close();
            }
            if (tocWriter != null && eventId != null) {
                tocWriter.addBlockOffset(rawOutStream.getBytesWritten(), eventId);
            }
            final OutputStream writableStream = new BufferedOutputStream(new GZIPOutputStream(new NonCloseableOutputStream(rawOutStream), 1), 65536);
            this.byteCountingOut = new ByteCountingOutputStream(writableStream, byteOffset);
        } else {
            if (tocWriter != null && eventId != null) {
                tocWriter.addBlockOffset(rawOutStream.getBytesWritten(), eventId);
            }
            this.byteCountingOut = rawOutStream;
        }
        this.out = new DataOutputStream(byteCountingOut);
        resetDirtyFlag();
    } catch (final IOException ioe) {
        markDirty();
        throw ioe;
    }
}
Also used : GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) TocWriter(org.apache.nifi.provenance.toc.TocWriter) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream) DataOutputStream(java.io.DataOutputStream) GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) IOException(java.io.IOException) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream)

Example 2 with NonCloseableOutputStream

use of org.apache.nifi.stream.io.NonCloseableOutputStream in project nifi by apache.

the class EventFileCompressor method compress.

public static void compress(final File input, final TocReader tocReader, final File output, final TocWriter tocWriter) throws IOException {
    try (final InputStream fis = new FileInputStream(input);
        final OutputStream fos = new FileOutputStream(output);
        final ByteCountingOutputStream byteCountingOut = new ByteCountingOutputStream(fos)) {
        int blockIndex = 0;
        while (true) {
            // Determine the min and max byte ranges for the current block.
            final long blockStart = tocReader.getBlockOffset(blockIndex);
            if (blockStart == -1) {
                break;
            }
            long blockEnd = tocReader.getBlockOffset(blockIndex + 1);
            if (blockEnd < 0) {
                blockEnd = input.length();
            }
            final long firstEventId = tocReader.getFirstEventIdForBlock(blockIndex);
            final long blockStartOffset = byteCountingOut.getBytesWritten();
            try (final OutputStream ncos = new NonCloseableOutputStream(byteCountingOut);
                final OutputStream gzipOut = new GZIPOutputStream(ncos, 1)) {
                StreamUtils.copy(fis, gzipOut, blockEnd - blockStart);
            }
            tocWriter.addBlockOffset(blockStartOffset, firstEventId);
            blockIndex++;
        }
    }
    // Close the TOC Reader and TOC Writer
    CloseableUtil.closeQuietly(tocReader, tocWriter);
}
Also used : GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream) GZIPOutputStream(org.apache.nifi.stream.io.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ByteCountingOutputStream(org.apache.nifi.stream.io.ByteCountingOutputStream) FileInputStream(java.io.FileInputStream) NonCloseableOutputStream(org.apache.nifi.stream.io.NonCloseableOutputStream)

Aggregations

FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ByteCountingOutputStream (org.apache.nifi.stream.io.ByteCountingOutputStream)2 GZIPOutputStream (org.apache.nifi.stream.io.GZIPOutputStream)2 NonCloseableOutputStream (org.apache.nifi.stream.io.NonCloseableOutputStream)2 BufferedOutputStream (java.io.BufferedOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TocWriter (org.apache.nifi.provenance.toc.TocWriter)1