/** * Constructs a {@link CtrCryptoInputStream}. * * @param input the input data. * @param cipher the CryptoCipher instance. * @param bufferSize the bufferSize. * @param key crypto key for the cipher. * @param iv Initialization vector for the cipher. * @param streamOffset the start offset in the stream. * @throws IOException if an I/O error occurs. */ protected CtrCryptoInputStream(Input input, CryptoCipher cipher, int bufferSize, byte[] key, byte[] iv, long streamOffset) throws IOException { super(input, cipher, bufferSize, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv)); this.initIV = iv.clone(); this.iv = iv.clone(); CryptoInputStream.checkStreamCipher(cipher); resetStreamOffset(streamOffset); }
resetStreamOffset(pos); return skipped;
/** * Seeks the stream to a specific position relative to start of the under * layer stream. * * @param position the given position in the data. * @throws IOException if an I/O error occurs. */ public void seek(long position) throws IOException { Utils.checkArgument(position >= 0, "Cannot seek to negative offset."); checkStream(); /* * If data of target pos in the underlying stream has already been read * and decrypted in outBuffer, we just need to re-position outBuffer. */ if (position >= getStreamPosition() && position <= getStreamOffset()) { int forward = (int) (position - getStreamPosition()); if (forward > 0) { outBuffer.position(outBuffer.position() + forward); } } else { input.seek(position); resetStreamOffset(position); } }