SSLEngineResult unwrap(ByteBuffer encryptedData) throws SSLException { ByteBuffer allEncryptedData = _buffers.prependCached(encryptedData); _buffers.prepareForUnwrap(allEncryptedData); SSLEngineResult result = doUnwrap(); allEncryptedData.position(result.bytesConsumed()); ByteBuffer unprocessedEncryptedData = BufferUtils.slice(allEncryptedData); emitPlainData(result); switch (result.getStatus()) { case BUFFER_UNDERFLOW: _buffers.cache(unprocessedEncryptedData); break; case BUFFER_OVERFLOW: _buffers.grow(BufferType.IN_PLAIN); if (unprocessedEncryptedData == null) { throw new RuntimeException( "Worker.unwrap had " + "buffer_overflow but all data was consumed!!"); } else { unwrap(unprocessedEncryptedData); } break; case OK: if (unprocessedEncryptedData == null) { _buffers.clearCache(); } else { _buffers.cache(unprocessedEncryptedData); } break; case CLOSED: break; } return result; }
SSLEngineResult wrap(ByteBuffer plainData) throws SSLException { _buffers.prepareForWrap(plainData); SSLEngineResult result = doWrap(); emitWrappedData(result); switch (result.getStatus()) { case BUFFER_UNDERFLOW: throw new RuntimeException("BUFFER_UNDERFLOW while wrapping!"); case BUFFER_OVERFLOW: _buffers.grow(BufferType.OUT_CIPHER); if (plainData.hasRemaining()) { plainData.position(result.bytesConsumed()); ByteBuffer remainingData = BufferUtils.slice(plainData); wrap(remainingData); } break; case OK: break; case CLOSED: break; } return result; }