Java Code Examples for org.apache.nifi.stream.io.StreamUtils#skip()

The following examples show how to use org.apache.nifi.stream.io.StreamUtils#skip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSystemRepository.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public long exportTo(final ContentClaim claim, final OutputStream destination, final long offset, final long length) throws IOException {
    if (offset < 0) {
        throw new IllegalArgumentException("offset cannot be negative");
    }
    final long claimSize = size(claim);
    if (offset > claimSize) {
        throw new IllegalArgumentException("offset of " + offset + " exceeds claim size of " + claimSize);
    }
    if (offset == 0 && length == claimSize) {
        return exportTo(claim, destination);
    }
    try (final InputStream in = read(claim)) {
        StreamUtils.skip(in, offset);
        final byte[] buffer = new byte[8192];
        int len;
        long copied = 0L;
        while ((len = in.read(buffer, 0, (int) Math.min(length - copied, buffer.length))) > 0) {
            destination.write(buffer, 0, len);
            copied += len;
        }
        return copied;
    }
}
 
Example 2
Source File: CompressableRecordReader.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void skipTo(final long position) throws IOException {
    // we are subtracting headerLength from the number of bytes consumed because we used to
    // consider the offset of the first record "0" - now we consider it whatever position it
    // it really is in the stream.
    final long currentPosition = byteCountingIn.getBytesConsumed() - headerLength;
    if (currentPosition == position) {
        return;
    }
    if (currentPosition > position) {
        throw new IOException("Cannot skip to byte offset " + position + " in stream because already at byte offset " + currentPosition);
    }

    final long toSkip = position - currentPosition;
    StreamUtils.skip(dis, toSkip);
}
 
Example 3
Source File: EventIdFirstSchemaRecordReader.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Optional<StandardProvenanceEventRecord> readToEvent(final long eventId, final DataInputStream dis, final int serializationVersion) throws IOException {
    verifySerializationVersion(serializationVersion);

    while (isData(dis)) {
        final long startOffset = getBytesConsumed();
        final long id = dis.readInt() + firstEventId;
        final int recordLength = dis.readInt();

        if (id >= eventId) {
            final StandardProvenanceEventRecord event = readRecord(dis, id, startOffset, recordLength);
            return Optional.ofNullable(event);
        } else {
            // This is not the record we want. Skip over it instead of deserializing it.
            StreamUtils.skip(dis, recordLength);
        }
    }

    return Optional.empty();
}
 
Example 4
Source File: VolatileContentRepository.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException {
    if (claim == null) {
        if (append) {
            return 0L;
        }
        Files.createFile(destination);
        return 0L;
    }

    final StandardOpenOption openOption = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE;
    try (final InputStream in = read(claim);
            final OutputStream destinationStream = Files.newOutputStream(destination, openOption)) {

        if (offset > 0) {
            StreamUtils.skip(in, offset);
        }

        StreamUtils.copy(in, destinationStream, length);
        return length;
    }
}
 
Example 5
Source File: VolatileContentRepository.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException {
    if (claim == null) {
        if (append) {
            return 0L;
        }
        Files.createFile(destination);
        return 0L;
    }

    final StandardOpenOption openOption = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE;
    try (final InputStream in = read(claim);
            final OutputStream destinationStream = Files.newOutputStream(destination, openOption)) {

        if (offset > 0) {
            StreamUtils.skip(in, offset);
        }

        StreamUtils.copy(in, destinationStream, length);
        return length;
    }
}
 
Example 6
Source File: FileSystemRepository.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public long exportTo(final ContentClaim claim, final OutputStream destination, final long offset, final long length) throws IOException {
    if (offset < 0) {
        throw new IllegalArgumentException("offset cannot be negative");
    }
    final long claimSize = size(claim);
    if (offset > claimSize) {
        throw new IllegalArgumentException("offset of " + offset + " exceeds claim size of " + claimSize);
    }
    if (offset == 0 && length == claimSize) {
        return exportTo(claim, destination);
    }
    try (final InputStream in = read(claim)) {
        StreamUtils.skip(in, offset);
        final byte[] buffer = new byte[8192];
        int len;
        long copied = 0L;
        while ((len = in.read(buffer, 0, (int) Math.min(length - copied, buffer.length))) > 0) {
            destination.write(buffer, 0, len);
            copied += len;
        }
        return copied;
    }
}
 
Example 7
Source File: FileSystemRepository.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream read(final ContentClaim claim) throws IOException {
    if (claim == null) {
        return new ByteArrayInputStream(new byte[0]);
    }
    final Path path = getPath(claim, true);
    final FileInputStream fis = new FileInputStream(path.toFile());
    if (claim.getOffset() > 0L) {
        try {
            StreamUtils.skip(fis, claim.getOffset());
        } catch (IOException ioe) {
            IOUtils.closeQuietly(fis);
            throw ioe;
        }

    }

    // see javadocs for claim.getLength() as to why we do this.
    if (claim.getLength() >= 0) {
        return new LimitedInputStream(fis, claim.getLength());
    } else {
        return fis;
    }
}
 
Example 8
Source File: NifiSeekableInputStream.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void seek(long newPos) throws IOException {
    final long currentPos = getPos();
    if (newPos == currentPos) {
        return;
    }

    if (newPos < currentPos) {
        // seeking backwards so first reset back to beginning of the stream then seek
        input.reset();
        input.mark(Integer.MAX_VALUE);
    }

    // must call getPos() again in case reset was called above
    StreamUtils.skip(input, newPos - getPos());
}
 
Example 9
Source File: EncryptedSchemaRecordReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Optional<StandardProvenanceEventRecord> readToEvent(final long eventId, final DataInputStream dis, final int serializationVersion) throws IOException {
    verifySerializationVersion(serializationVersion);

    while (isData(dis)) {
        final long startOffset = getBytesConsumed();
        final long id = dis.readInt() + getFirstEventId();
        final int recordLength = dis.readInt();

        if (id >= eventId) {
            final StandardProvenanceEventRecord event = readRecord(dis, id, startOffset, recordLength);
            return Optional.ofNullable(event);
        } else {
            // This is not the record we want. Skip over it instead of deserializing it.
            StreamUtils.skip(dis, recordLength);
        }
    }

    return Optional.empty();
}
 
Example 10
Source File: EventIdFirstSchemaRecordReader.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Optional<StandardProvenanceEventRecord> readToEvent(final long eventId, final DataInputStream dis, final int serializationVersion) throws IOException {
    verifySerializationVersion(serializationVersion);

    while (isData(dis)) {
        final long startOffset = getBytesConsumed();
        final long id = dis.readInt() + firstEventId;
        final int recordLength = dis.readInt();

        if (id >= eventId) {
            final StandardProvenanceEventRecord event = readRecord(dis, id, startOffset, recordLength);
            return Optional.ofNullable(event);
        } else {
            // This is not the record we want. Skip over it instead of deserializing it.
            StreamUtils.skip(dis, recordLength);
        }
    }

    return Optional.empty();
}
 
Example 11
Source File: ContentClaimInputStream.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void formDelegate() throws IOException {
    if (delegate != null) {
        delegate.close();
    }

    delegate = contentRepository.read(contentClaim);
    StreamUtils.skip(delegate, claimOffset);
    currentOffset = claimOffset;
}
 
Example 12
Source File: VolatileContentRepository.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public long exportTo(ContentClaim claim, OutputStream destination, long offset, long length) throws IOException {
    final InputStream in = read(claim);
    try {
        StreamUtils.skip(in, offset);
        StreamUtils.copy(in, destination, length);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return length;
}
 
Example 13
Source File: FileSystemRepository.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream read(final ContentClaim claim) throws IOException {
    if (claim == null) {
        return new ByteArrayInputStream(new byte[0]);
    }
    final Path path = getPath(claim, true);
    final FileInputStream fis = new FileInputStream(path.toFile());
    if (claim.getOffset() > 0L) {
        try {
            StreamUtils.skip(fis, claim.getOffset());
        } catch (final EOFException eof) {
            final long resourceClaimBytes;
            try {
                resourceClaimBytes = Files.size(path);
            } catch (final IOException e) {
                throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset()
                    + " but Resource Claim has fewer than this many bytes (actual length of the resource claim could not be determined)");
            }

            throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset() + " but Resource Claim " + path + " is only " + resourceClaimBytes + " bytes");
        } catch (final IOException ioe) {
            IOUtils.closeQuietly(fis);
            throw ioe;
        }
    }

    // A claim length of -1 indicates that the claim is still being written to and we don't know
    // the length. In this case, we don't limit the Input Stream. If the Length has been populated, though,
    // it is possible that the Length could then be extended. However, we do want to avoid ever allowing the
    // stream to read past the end of the Content Claim. To accomplish this, we use a LimitedInputStream but
    // provide a LongSupplier for the length instead of a Long value. this allows us to continue reading until
    // we get to the end of the Claim, even if the Claim grows. This may happen, for instance, if we obtain an
    // InputStream for this claim, then read from it, write more to the claim, and then attempt to read again. In
    // such a case, since we have written to that same Claim, we should still be able to read those bytes.
    if (claim.getLength() >= 0) {
        return new LimitedInputStream(fis, claim::getLength);
    } else {
        return fis;
    }
}
 
Example 14
Source File: ContentClaimInputStream.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void reset() throws IOException {
    if (markOffset < 0) {
        throw new IOException("Stream has not been marked");
    }

    if (currentOffset != markOffset) {
        delegate.close();
        formDelegate();
        StreamUtils.skip(delegate, markOffset - claimOffset);
        currentOffset = markOffset;
    }
}
 
Example 15
Source File: CompressableRecordReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void skipToBlock(final int blockIndex) throws IOException {
    if (tocReader == null) {
        throw new IllegalStateException("Cannot skip to block " + blockIndex + " for Provenance Log " + filename + " because no Table-of-Contents file was found for this Log");
    }

    if (blockIndex < 0) {
        throw new IllegalArgumentException("Cannot skip to block " + blockIndex + " because the value is negative");
    }

    if (blockIndex == getBlockIndex()) {
        return;
    }

    final long offset = tocReader.getBlockOffset(blockIndex);
    if (offset < 0) {
        throw new IOException("Unable to find block " + blockIndex + " in Provenance Log " + filename);
    }

    final long curOffset = rawInputStream.getBytesConsumed();

    final long bytesToSkip = offset - curOffset;
    if (bytesToSkip >= 0) {
        try {
            StreamUtils.skip(rawInputStream, bytesToSkip);
            logger.debug("Skipped stream from offset {} to {} ({} bytes skipped)", curOffset, offset, bytesToSkip);
        } catch (final EOFException eof) {
            throw new EOFException("Attempted to skip to byte offset " + offset + " for " + filename + " but file does not have that many bytes (TOC Reader=" + getTocReader() + ")");
        } catch (final IOException e) {
            throw new IOException("Failed to skip to offset " + offset + " for block " + blockIndex + " of Provenance Log " + filename, e);
        }

        resetStreamForNextBlock();
    }
}
 
Example 16
Source File: FileSystemRepository.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException {
    if (claim == null && offset > 0) {
        throw new IllegalArgumentException("Cannot specify an offset of " + offset + " for a null claim");
    }
    if (claim == null) {
        if (append) {
            return 0L;
        }
        Files.createFile(destination);
        return 0L;
    }

    final long claimSize = size(claim);
    if (offset > claimSize) {
        throw new IllegalArgumentException("Offset of " + offset + " exceeds claim size of " + claimSize);

    }

    try (final InputStream in = read(claim);
            final FileOutputStream fos = new FileOutputStream(destination.toFile(), append)) {
        if (offset > 0) {
            StreamUtils.skip(in, offset);
        }
        StreamUtils.copy(in, fos, length);
        if (alwaysSync) {
            fos.getFD().sync();
        }
        return length;
    }
}
 
Example 17
Source File: VolatileContentRepository.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public long exportTo(ContentClaim claim, OutputStream destination, long offset, long length) throws IOException {
    final InputStream in = read(claim);
    try {
        StreamUtils.skip(in, offset);
        StreamUtils.copy(in, destination, length);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return length;
}
 
Example 18
Source File: CompressableRecordReader.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void skip(final long bytesToSkip) throws IOException {
    StreamUtils.skip(dis, bytesToSkip);
}
 
Example 19
Source File: StandardProcessSession.java    From nifi with Apache License 2.0 4 votes vote down vote up
private InputStream getInputStream(final FlowFile flowFile, final ContentClaim claim, final long offset, final boolean allowCachingOfStream) throws ContentNotFoundException {
    // If there's no content, don't bother going to the Content Repository because it is generally expensive and we know
    // that there is no actual content.
    if (flowFile.getSize() == 0L) {
        return new ByteArrayInputStream(new byte[0]);
    }

    try {
        // If the recursion set is empty, we can use the same input stream that we already have open. However, if
        // the recursion set is NOT empty, we can't do this because we may be reading the input of FlowFile 1 while in the
        // callback for reading FlowFile 1 and if we used the same stream we'd be destroying the ability to read from FlowFile 1.
        if (allowCachingOfStream && readRecursionSet.isEmpty() && writeRecursionSet.isEmpty()) {
            if (currentReadClaim == claim) {
                if (currentReadClaimStream != null && currentReadClaimStream.getCurrentOffset() <= offset) {
                    final long bytesToSkip = offset - currentReadClaimStream.getCurrentOffset();
                    if (bytesToSkip > 0) {
                        StreamUtils.skip(currentReadClaimStream, bytesToSkip);
                    }

                    return new DisableOnCloseInputStream(currentReadClaimStream);
                }
            }

            claimCache.flush(claim);

            if (currentReadClaimStream != null) {
                currentReadClaimStream.close();
            }

            currentReadClaim = claim;
            currentReadClaimStream = new ContentClaimInputStream(context.getContentRepository(), claim, offset);

            // Use a non-closeable stream because we want to keep it open after the callback has finished so that we can
            // reuse the same InputStream for the next FlowFile
            final InputStream disableOnClose = new DisableOnCloseInputStream(currentReadClaimStream);
            return disableOnClose;
        } else {
            claimCache.flush(claim);

            final InputStream rawInStream = new ContentClaimInputStream(context.getContentRepository(), claim, offset);
            return rawInStream;
        }
    } catch (final ContentNotFoundException cnfe) {
        throw cnfe;
    } catch (final EOFException eof) {
        throw new ContentNotFoundException(claim, eof);
    } catch (final IOException ioe) {
        throw new FlowFileAccessException("Failed to read content of " + flowFile, ioe);
    }
}
 
Example 20
Source File: CompressableRecordReader.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void skip(final long bytesToSkip) throws IOException {
    StreamUtils.skip(dis, bytesToSkip);
}