com.rabbitmq.client.impl.nio.DefaultByteBufferFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of amqp-client Show documentation
Show all versions of amqp-client Show documentation
The RabbitMQ Java client library allows Java applications to interface with RabbitMQ.
package com.rabbitmq.client.impl.nio;
import java.nio.ByteBuffer;
import java.util.function.Function;
/**
* Default {@link ByteBufferFactory} that creates heap-based {@link ByteBuffer}s.
* This behavior can be changed by passing in a custom {@link Function}
* to the constructor.
*
* @see NioParams
* @see ByteBufferFactory
* @since 5.5.0
*/
public class DefaultByteBufferFactory implements ByteBufferFactory {
private final Function allocator;
public DefaultByteBufferFactory(Function allocator) {
this.allocator = allocator;
}
public DefaultByteBufferFactory() {
this(capacity -> ByteBuffer.allocate(capacity));
}
@Override
public ByteBuffer createReadBuffer(NioContext nioContext) {
if (nioContext.getSslEngine() == null) {
return allocator.apply(nioContext.getNioParams().getReadByteBufferSize());
} else {
return allocator.apply(nioContext.getSslEngine().getSession().getApplicationBufferSize());
}
}
@Override
public ByteBuffer createWriteBuffer(NioContext nioContext) {
if (nioContext.getSslEngine() == null) {
return allocator.apply(nioContext.getNioParams().getWriteByteBufferSize());
} else {
return allocator.apply(nioContext.getSslEngine().getSession().getApplicationBufferSize());
}
}
@Override
public ByteBuffer createEncryptedReadBuffer(NioContext nioContext) {
return createEncryptedByteBuffer(nioContext);
}
@Override
public ByteBuffer createEncryptedWriteBuffer(NioContext nioContext) {
return createEncryptedByteBuffer(nioContext);
}
protected ByteBuffer createEncryptedByteBuffer(NioContext nioContext) {
if (nioContext.getSslEngine() == null) {
throw new IllegalArgumentException("Encrypted byte buffer should be created only in SSL/TLS context");
} else {
return allocator.apply(nioContext.getSslEngine().getSession().getPacketBufferSize());
}
}
}