@Override public long exportTo(ContentClaim claim, OutputStream destination) throws IOException { final InputStream in = read(claim); try { return StreamUtils.copy(in, destination); } finally { IOUtils.closeQuietly(in); } }
@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; }
@Override public long merge(final Collection<ContentClaim> claims, final ContentClaim destination, final byte[] header, final byte[] footer, final byte[] demarcator) throws IOException { long bytes = 0L; try (final OutputStream out = write(destination)) { if (header != null) { out.write(header); bytes += header.length; } final Iterator<ContentClaim> itr = claims.iterator(); while (itr.hasNext()) { final ContentClaim readClaim = itr.next(); try (final InputStream in = read(readClaim)) { bytes += StreamUtils.copy(in, out); } if (itr.hasNext() && demarcator != null) { bytes += demarcator.length; out.write(demarcator); } } if (footer != null) { bytes += footer.length; out.write(footer); } return bytes; } }
@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 public ContentClaim clone(final ContentClaim original, final boolean lossTolerant) throws IOException { final ContentClaim createdClaim = create(lossTolerant); try (final InputStream dataIn = read(original)) { final ContentRepository createdClaimRepo = lossTolerant ? this : getBackupRepository(); if (createdClaimRepo == null) { throw new IllegalStateException("Cannot create non-loss-tolerant ContentClaim because there is no persistent Content Repository configured"); } try (final OutputStream dataOut = createdClaimRepo.write(createdClaim)) { StreamUtils.copy(dataIn, dataOut); } } return createdClaim; }