final SSLConnectionContext sslCtx, final Buffer netBuffer)
throws SSLException {
final int packetBufferSize = sslCtx.getNetBufferSize();
Buffer buffer;
if (netBuffer != null && !netBuffer.isComposite() &&
netBuffer.capacity() - netBuffer.limit() >= packetBufferSize) {
netBuffer.position(netBuffer.limit());
netBuffer.limit(netBuffer.capacity());
buffer = netBuffer;
} else {
buffer = allocateOutputBuffer(packetBufferSize * 2);
}
final SSLConnectionContext.SslResult result =
sslCtx.wrap(Buffers.EMPTY_BUFFER, buffer, HS_WRAP_ALLOCATOR);
Buffer output = result.getOutput();
output.flip();
if (buffer != output) {
if (buffer == netBuffer) {
netBuffer.flip();
}
}
if (result.isError()) {
if (output != netBuffer) {
output.dispose();
}
throw result.getError();
}