Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize BufferedChannelFactory's internal buffer size. #706

Merged
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
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