Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 56 additions & 12 deletions java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class OpenSSLEngine extends SSLEngine {
}

static final int MAX_PLAINTEXT_LENGTH = 16 * 1024; // 2^14
static final boolean USE_POOLED_BYTEBUFFER = Boolean.getBoolean("org.wildfly.openssl.use-pooled-buffer");
private static final int MAX_COMPRESSED_LENGTH = MAX_PLAINTEXT_LENGTH + 1024;
private static final int MAX_CIPHERTEXT_LENGTH = MAX_COMPRESSED_LENGTH + 1024;

Expand Down Expand Up @@ -254,7 +255,8 @@ private int writePlaintextData(final ByteBuffer src) {
return sslWrote;
}
} else {
ByteBuffer buf = ByteBuffer.allocateDirect(len);
final ByteBufferAllocator allocator = getByteBufferAllocator();
final ByteBuffer buf = allocator.allocate(len);
try {
final long addr = memoryAddress(buf);

Expand All @@ -271,8 +273,7 @@ private int writePlaintextData(final ByteBuffer src) {
src.position(pos);
}
} finally {
buf.clear();
ByteBufferUtils.cleanDirectBuffer(buf);
allocator.close();
}
}

Expand All @@ -293,7 +294,8 @@ private int writeEncryptedData(final ByteBuffer src) {
return netWrote;
}
} else {
ByteBuffer buf = ByteBuffer.allocateDirect(len);
final ByteBufferAllocator allocator = getByteBufferAllocator();
final ByteBuffer buf = allocator.allocate(len);
try {
final long addr = memoryAddress(buf);

Expand All @@ -307,8 +309,7 @@ private int writeEncryptedData(final ByteBuffer src) {
src.position(pos);
}
} finally {
buf.clear();
ByteBufferUtils.cleanDirectBuffer(buf);
allocator.close();
}
}

Expand Down Expand Up @@ -344,7 +345,8 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException {
final int pos = dst.position();
final int limit = dst.limit();
final int len = Math.min(MAX_ENCRYPTED_PACKET_LENGTH, limit - pos);
final ByteBuffer buf = ByteBuffer.allocateDirect(len);
final ByteBufferAllocator allocator = getByteBufferAllocator();
final ByteBuffer buf = allocator.allocate(len);
try {
final long addr = memoryAddress(buf);

Expand All @@ -368,8 +370,7 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException {
}
}
} finally {
buf.clear();
ByteBufferUtils.cleanDirectBuffer(buf);
allocator.close();
}
}

Expand All @@ -389,7 +390,8 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) {
return bioRead;
}
} else {
final ByteBuffer buf = ByteBuffer.allocateDirect(pending);
final ByteBufferAllocator allocator = getByteBufferAllocator();
final ByteBuffer buf = allocator.allocate(pending);
try {
final long addr = memoryAddress(buf);

Expand All @@ -403,8 +405,7 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) {
return bioRead;
}
} finally {
buf.clear();
ByteBufferUtils.cleanDirectBuffer(buf);
allocator.close();
}
}

Expand Down Expand Up @@ -1606,4 +1607,47 @@ private Set<String> getSupportedProtocolsSet() {
}
}

private interface ByteBufferAllocator {
ByteBuffer allocate(final int length);

void close();
}

private ByteBufferAllocator getByteBufferAllocator() {
if (USE_POOLED_BYTEBUFFER) {
return new ByteBufferAllocator() {
final DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate();

@Override
public ByteBuffer allocate(int length) {
ByteBuffer buf = pooledByteBuffer.getBuffer();
buf.limit(length);
return buf;
}

@Override
public void close() {
pooledByteBuffer.close();
}
};
} else {
return new ByteBufferAllocator() {
ByteBuffer buf;

@Override
public ByteBuffer allocate(int length) {
buf = ByteBuffer.allocateDirect(length);
return buf;
}

@Override
public void close() {
if (buf != null) {
buf.clear();
ByteBufferUtils.cleanDirectBuffer(buf);
}
}
};
}
}
}