List of usage examples for java.nio.channels SeekableByteChannel read
@Override int read(ByteBuffer dst) throws IOException;
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
@Override public Long decryptRange(SeekableByteChannel encryptedFile, OutputStream plaintextFile, long pos, long length) throws IOException { // read iv://from w ww .java2 s . c om encryptedFile.position(0); final ByteBuffer countingIv = ByteBuffer.allocate(AES_BLOCK_LENGTH); final int numIvBytesRead = encryptedFile.read(countingIv); // check validity of header: if (numIvBytesRead != AES_BLOCK_LENGTH) { throw new IOException("Failed to read file header."); } // seek relevant position and update iv: long firstRelevantBlock = pos / AES_BLOCK_LENGTH; // cut of fraction! long beginOfFirstRelevantBlock = firstRelevantBlock * AES_BLOCK_LENGTH; long offsetInsideFirstRelevantBlock = pos - beginOfFirstRelevantBlock; countingIv.putLong(AES_BLOCK_LENGTH - Long.BYTES, firstRelevantBlock); // fast forward stream: encryptedFile.position(64 + beginOfFirstRelevantBlock); // generate cipher: final Cipher cipher = this.aesCtrCipher(primaryMasterKey, countingIv.array(), Cipher.DECRYPT_MODE); // read content final InputStream in = new SeekableByteChannelInputStream(encryptedFile); final InputStream cipheredIn = new CipherInputStream(in, cipher); return IOUtils.copyLarge(cipheredIn, plaintextFile, offsetInsideFirstRelevantBlock, length); }
From source file:org.mycore.common.content.util.MCRServletContentHelper.java
/** * Consumes the content and writes it to the ServletOutputStream. * * @param content The source resource//w w w. jav a2s. c om * @param out The outputBufferSize stream to write to * @param range Range the client wanted to retrieve */ private static void copy(final MCRContent content, final ServletOutputStream out, final Range range, // TODO: beautify this final int inputBufferSize, final int outputBufferSize) throws IOException { if (content.isReusable()) { try (ReadableByteChannel readableByteChannel = content.getReadableByteChannel()) { if (readableByteChannel instanceof SeekableByteChannel) { endCurrentTransaction(); SeekableByteChannel seekableByteChannel = (SeekableByteChannel) readableByteChannel; seekableByteChannel.position(range.start); long bytesToCopy = range.end - range.start + 1; while (bytesToCopy > 0) { ByteBuffer byteBuffer; if (bytesToCopy > (long) MCRServletContentHelper.DEFAULT_BUFFER_SIZE) { byteBuffer = ByteBuffer.allocate(MCRServletContentHelper.DEFAULT_BUFFER_SIZE); } else { byteBuffer = ByteBuffer.allocate((int) bytesToCopy); } int bytesRead = seekableByteChannel.read(byteBuffer); bytesToCopy -= bytesRead; out.write(byteBuffer.array()); } return; } } } try (final InputStream resourceInputStream = content.getInputStream(); final InputStream in = isInputStreamBuffered(resourceInputStream, content) ? resourceInputStream : new BufferedInputStream(resourceInputStream, inputBufferSize)) { endCurrentTransaction(); final IOException exception = copyRange(in, out, 0, range.start, range.end, outputBufferSize); if (exception != null) { throw exception; } } }