Skip to content

Commit

Permalink
Optimize BufferedChannelFactory's internal buffer size. (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdcove2 authored Feb 27, 2024
1 parent a6a0cbf commit 27e0793
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/emissary/core/channels/BufferedChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ private BufferedChannelFactory() {}
* Creates a SeekableByteChannelFactory that caches the bytes of the passed in SeekableByteChannelFactory.
*
* @param seekableByteChannelFactory to be cached.
* @param bufferSize of the buffer of bytes.
* @param maxBufferSize maximum size of the buffer of bytes (BufferSize = Math.min(sbcf.size(), maxBufferSize)).
* @return the caching SeekableByteChannelFactory.
*/
public static SeekableByteChannelFactory create(final SeekableByteChannelFactory seekableByteChannelFactory,
final int bufferSize) {
return new BufferedChannelFactoryImpl(seekableByteChannelFactory, bufferSize);
final int maxBufferSize) {
return new BufferedChannelFactoryImpl(seekableByteChannelFactory, maxBufferSize);
}

/**
Expand All @@ -42,15 +42,25 @@ private static class BufferedChannelFactoryImpl implements SeekableByteChannelFa
* Creates a SeekableByteChannelFactory that caches the bytes of the passed in SeekableByteChannelFactory.
*
* @param seekableByteChannelFactory to be cached.
* @param bufferSize of the buffer of bytes.
* @param maxBufferSize of the buffer of bytes.
*/
public BufferedChannelFactoryImpl(final SeekableByteChannelFactory seekableByteChannelFactory,
final int bufferSize) {
final int maxBufferSize) {
Validate.notNull(seekableByteChannelFactory, "Required: seekableByteChannelFactory not null!");
Validate.isTrue(bufferSize > 0, "Required: bufferSize > 0");
Validate.isTrue(maxBufferSize > 0, "Required: maxBufferSize > 0");

this.seekableByteChannelFactory = seekableByteChannelFactory;
this.bufferSize = bufferSize;

int b = maxBufferSize;
try (SeekableByteChannel sbc = seekableByteChannelFactory.create()) {
if (sbc.size() < maxBufferSize) {
b = (int) sbc.size();
}
} catch (IOException e) {
// Leave b as maxBufferSize.
}

this.bufferSize = b;
}

@Override
Expand Down

0 comments on commit 27e0793

Please sign in to comment.