Skip to content

Commit

Permalink
fix(s3stream): check whether the read length greater than the cache size
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Yu <[email protected]>
  • Loading branch information
Chillax-0v0 committed Jan 31, 2024
1 parent f980595 commit f3c3ba4
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ public synchronized int read(ByteBuf dst, long position, int length) throws IOEx
// If we don't know the capacity now, we can't cache.
return channel.read(dst, position, length);
}

long start = position;
length = Math.min(length, dst.writableBytes());
long end = position + length;

ByteBuf cache = getCache();
if (length > cache.capacity()) {
// If the length is larger than the cache capacity, we can't cache.
return channel.read(dst, position, length);
}

boolean fallWithinCache = cachePosition >= 0 && cachePosition <= start && end <= cachePosition + cache.readableBytes();
if (!fallWithinCache) {
cache.clear();
Expand All @@ -70,6 +77,7 @@ public synchronized int read(ByteBuf dst, long position, int length) throws IOEx
int cacheLength = (int) Math.min(cache.writableBytes(), channel.capacity() - cachePosition);
channel.read(cache, cachePosition, cacheLength);
}

// Now the cache is ready.
int relativePosition = (int) (start - cachePosition);
dst.writeBytes(cache, relativePosition, length);
Expand Down

0 comments on commit f3c3ba4

Please sign in to comment.