Skip to content

Commit

Permalink
#12680 fix rounding error in buffer count calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Jan 9, 2025
1 parent 3627f5e commit 7d44d72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ private MultiBufferFileMappedHttpContent(HttpContent content, int maxBufferSize)

long contentLength = content.getContentLengthValue();
int bufferCount = Math.toIntExact(contentLength / maxBufferSize);
if (contentLength % maxBufferSize != 0)
{
if (bufferCount == Integer.MAX_VALUE)
throw new IOException("Cannot memory map Content as that would require over Integer.MAX_VALUE buffers: " + content);
bufferCount++;
}
_buffers = new ByteBuffer[bufferCount];
long currentPos = 0L;
long total = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand All @@ -39,7 +41,7 @@ public class FileMappingHttpContentFactoryTest
public WorkDir workDir;

@Test
public void testMultiBufferFileMapped() throws Exception
public void testMultiBufferFileMappedOffsetAndLength() throws Exception
{
Path file = Files.writeString(workDir.getEmptyPathDir().resolve("file.txt"), "0123456789abcdefghijABCDEFGHIJ");
FileMappingHttpContentFactory fileMappingHttpContentFactory = new FileMappingHttpContentFactory(
Expand Down Expand Up @@ -76,6 +78,22 @@ public void testMultiBufferFileMapped() throws Exception
assertThat(writeToString(content, 25, -1), is("FGHIJ"));
}

@ParameterizedTest
@ValueSource(ints = {8, 10})
public void testMultiBufferFileMappedMaxBufferSizeRounding(int maxBufferSize) throws Exception
{
Path file = Files.writeString(workDir.getEmptyPathDir().resolve("file.txt"), "0123456789abcdefghijABCDEFGHIJ");
FileMappingHttpContentFactory fileMappingHttpContentFactory = new FileMappingHttpContentFactory(
new ResourceHttpContentFactory(ResourceFactory.root().newResource(file.getParent()), MimeTypes.DEFAULTS, ByteBufferPool.SIZED_NON_POOLING),
0, maxBufferSize);

HttpContent content = fileMappingHttpContentFactory.getContent("file.txt");

assertThat(content.getContentLength().getValue(), is("30"));
assertThat(content.getContentLengthValue(), is(30L));
assertThat(writeToString(content, 0, -1), is("0123456789abcdefghijABCDEFGHIJ"));
}

private static String writeToString(HttpContent content, long offset, long length) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down

0 comments on commit 7d44d72

Please sign in to comment.