if (expectedLength == -1 || originalMessage.remaining() < expectedLength) {
return TransformationResult.createIncompletedResult(
originalMessage);
}
} catch (SSLException e) {
throw new TransformationException(e);
}
final Buffer targetBuffer = memoryManager.allocate(
sslEngine.getSession().getApplicationBufferSize());
TransformationResult<Buffer, Buffer> transformationResult = null;
try {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "SSLDecoder engine: {0} input: {1} output: {2}",
new Object[]{sslEngine, originalMessage, targetBuffer});
}
final int pos = originalMessage.position();
final SSLEngineResult sslEngineResult;
if (!originalMessage.isComposite()) {
sslEngineResult = sslEngine.unwrap(originalMessage.toByteBuffer(),
targetBuffer.toByteBuffer());
} else {
final ByteBuffer originalByteBuffer =
originalMessage.toByteBuffer(pos,
pos + expectedLength);
sslEngineResult = sslEngine.unwrap(originalByteBuffer,
targetBuffer.toByteBuffer());
}
originalMessage.position(pos + sslEngineResult.bytesConsumed());
targetBuffer.position(sslEngineResult.bytesProduced());
final SSLEngineResult.Status status = sslEngineResult.getStatus();
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "SSLDecoderr done engine: {0} result: {1} input: {2} output: {3}",
new Object[]{sslEngine, sslEngineResult, originalMessage, targetBuffer});
}
if (status == SSLEngineResult.Status.OK) {
targetBuffer.trim();
return TransformationResult.createCompletedResult(
targetBuffer, originalMessage);
} else if (status == SSLEngineResult.Status.CLOSED) {
targetBuffer.dispose();
return TransformationResult.createCompletedResult(
Buffers.EMPTY_BUFFER, originalMessage);
} else {
targetBuffer.dispose();
if (status == SSLEngineResult.Status.BUFFER_UNDERFLOW) {
transformationResult =
TransformationResult.createIncompletedResult(
originalMessage);
} else if (status == SSLEngineResult.Status.BUFFER_OVERFLOW) {
transformationResult =
TransformationResult.createErrorResult(
BUFFER_OVERFLOW_ERROR,
"Buffer overflow during unwrap operation");
}
}
} catch (SSLException e) {
targetBuffer.dispose();
throw new TransformationException(e);
}
return transformationResult;
}