in src/main/java/com/amazonaws/encryptionsdk/CryptoInputStream.java [80:129]
private int fillOutBytes() throws IOException, BadCiphertextException {
final byte[] inputStreamBytes = new byte[MAX_READ_LEN];
final int readLen = inputStream_.read(inputStreamBytes);
outStart_ = 0;
int processedLen;
if (readLen < 0) {
// Mark end of stream until doFinal returns something.
processedLen = -1;
if (!hasFinalCalled_) {
int outOffset = 0;
int outLen = 0;
// Handle the case where processBytes() was never called before.
// This happens with an empty file where the end of stream is
// reached on the first read attempt. In this case,
// processBytes() must be called so the header bytes are written
// during encryption.
if (!hasProcessBytesCalled_) {
outBytes_ = new byte[cryptoHandler_.estimateOutputSize(0)];
outLen +=
cryptoHandler_
.processBytes(inputStreamBytes, 0, 0, outBytes_, outOffset)
.getBytesWritten();
outOffset += outLen;
} else {
outBytes_ = new byte[cryptoHandler_.estimateFinalOutputSize()];
}
// Get final bytes.
outLen += cryptoHandler_.doFinal(outBytes_, outOffset);
processedLen = outLen;
hasFinalCalled_ = true;
}
} else {
// process the read bytes.
outBytes_ = new byte[cryptoHandler_.estimatePartialOutputSize(readLen)];
processedLen =
cryptoHandler_
.processBytes(inputStreamBytes, 0, readLen, outBytes_, outStart_)
.getBytesWritten();
hasProcessBytesCalled_ = true;
}
outEnd_ = processedLen;
return processedLen;
}