@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, content, true); } });
@Override public void process(final InputStream in) throws IOException { StreamUtils.copy(in, baos); } });
@Override public void skip(final long bytesToSkip) throws IOException { StreamUtils.skip(dis, bytesToSkip); }
@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); }
@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, buffer); } });
@Override public void process(final InputStream in) throws IOException { StreamUtils.copy(in, baos); } });
@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; } }
@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(); }
@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, messageContent, true); } });
@Override public void process(final OutputStream out) throws IOException { StreamUtils.copy(in, out); } });
@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; } }
@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; } }
@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, messageContent, true); } });
@Override public void process(final InputStream in, final OutputStream out) throws IOException { in.skip(startOffset); StreamUtils.copy(in, out, newFileSize); } });
@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(); }
@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, buffer); } });
@Override public void process(InputStream in) throws IOException { try (BufferedInputStream input = new BufferedInputStream(in)) { StreamUtils.copy(input, baos); } baos.close(); } });
@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(); } }
@Override public void process(final InputStream in) throws IOException { StreamUtils.fillBuffer(in, buffer, false); } });