Skip to content

Commit

Permalink
#10226 - handle review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Aug 21, 2023
1 parent 8582b3a commit 664c118
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ public InputStreamContentSource(InputStream inputStream, ByteRange byteRange) th
}

@Override
protected int fillBufferFromInputStream(InputStream inputStream, ByteBuffer buffer) throws IOException
protected int fillBufferFromInputStream(InputStream inputStream, byte[] buffer) throws IOException
{
if (toRead == 0)
return -1;
int toReadInt = (int)Math.min(Integer.MAX_VALUE, toRead);
int len = Math.min(toReadInt, buffer.capacity());
int read = inputStream.read(buffer.array(), buffer.arrayOffset(), len);
int len = Math.min(toReadInt, buffer.length);
int read = inputStream.read(buffer, 0, len);
toRead -= read;
return read;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ protected boolean isReadComplete(long read)
}

/**
* <p>A {@link MultiPart.Part} whose content is a byte range of a file.</p>
* <p>A {@link MultiPart.Part} whose content is a byte range of a {@link Resource}.</p>
*/
public static class Part extends MultiPart.Part
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Content.Chunk read()
try
{
ByteBuffer buffer = streamBuffer.getByteBuffer();
int read = fillBufferFromInputStream(inputStream, buffer);
int read = fillBufferFromInputStream(inputStream, buffer.array());
if (read < 0)
{
streamBuffer.release();
Expand All @@ -101,9 +101,9 @@ public Content.Chunk read()
}
}

protected int fillBufferFromInputStream(InputStream inputStream, ByteBuffer buffer) throws IOException
protected int fillBufferFromInputStream(InputStream inputStream, byte[] buffer) throws IOException
{
return inputStream.read(buffer.array(), buffer.arrayOffset(), buffer.capacity());
return inputStream.read(buffer, 0, buffer.length);
}

private void close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean handle(Request request, Response response, Callback callback)
String boundary = "boundary";
try (MultiPartByteRanges.ContentSource content = new MultiPartByteRanges.ContentSource(boundary))
{
ranges.forEach(range -> content.addPart(new MultiPartByteRanges.Part("text/plain", ResourceFactory.root().newResource(resourcePath), range, content.getLength())));
ranges.forEach(range -> content.addPart(new MultiPartByteRanges.Part("text/plain", ResourceFactory.of(this).newResource(resourcePath), range, content.getLength())));
content.close();

response.setStatus(HttpStatus.PARTIAL_CONTENT_206);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void start() throws Exception
Files.writeString(rangeFile, rangeChars);

ResourceHandler handler = new ResourceHandler();
handler.setBaseResource(ResourceFactory.root().newResource(dir));
handler.setBaseResource(ResourceFactory.of(handler).newResource(dir));
server.setHandler(handler);
server.start();
}
Expand All @@ -99,7 +99,7 @@ public void testMemoryResourceRange() throws Exception
{
changeHandler(new ResourceHandler()
{
final Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/simple/big.txt"));
final Resource memResource = ResourceFactory.of(this).newMemoryResource(getClass().getResource("/simple/big.txt"));

@Override
protected HttpContent.Factory newHttpContentFactory()
Expand Down Expand Up @@ -131,7 +131,7 @@ public void testMemoryResourceMultipleRanges() throws Exception
{
changeHandler(new ResourceHandler()
{
final Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/simple/big.txt"));
final Resource memResource = ResourceFactory.of(this).newMemoryResource(getClass().getResource("/simple/big.txt"));

@Override
protected HttpContent.Factory newHttpContentFactory()
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testMemoryResourceRangeUsingBufferedHttpContent() throws Exception
{
changeHandler(new ResourceHandler()
{
final Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/simple/big.txt"));
final Resource memResource = ResourceFactory.of(this).newMemoryResource(getClass().getResource("/simple/big.txt"));

@Override
protected HttpContent.Factory newHttpContentFactory()
Expand Down Expand Up @@ -206,7 +206,7 @@ public void testMemoryResourceMultipleRangesUsingBufferedHttpContent() throws Ex
{
changeHandler(new ResourceHandler()
{
final Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/simple/big.txt"));
final Resource memResource = ResourceFactory.of(this).newMemoryResource(getClass().getResource("/simple/big.txt"));

@Override
protected HttpContent.Factory newHttpContentFactory()
Expand Down Expand Up @@ -249,7 +249,7 @@ public void testNotAcceptRanges() throws Exception
{
changeHandler(new ResourceHandler()
{
final Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/simple/big.txt"));
final Resource memResource = ResourceFactory.of(this).newMemoryResource(getClass().getResource("/simple/big.txt"));

@Override
protected HttpContent.Factory newHttpContentFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3452,7 +3452,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
@Test
public void testMemoryResourceRange() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
DefaultServlet defaultServlet = new DefaultServlet();
context.addServlet(new ServletHolder(defaultServlet), "/");
defaultServlet.getResourceService().setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
Expand All @@ -3473,7 +3473,7 @@ public void testMemoryResourceRange() throws Exception
@Test
public void testMemoryResourceRangeUsingBufferedHttpContent() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
DefaultServlet defaultServlet = new DefaultServlet();
context.addServlet(new ServletHolder(defaultServlet), "/");
defaultServlet.getResourceService().setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain")
Expand Down Expand Up @@ -3503,7 +3503,7 @@ public ByteBuffer getByteBuffer()
@Test
public void testMemoryResourceMultipleRanges() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
DefaultServlet defaultServlet = new DefaultServlet();
context.addServlet(new ServletHolder(defaultServlet), "/");
defaultServlet.getResourceService().setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
Expand All @@ -3527,7 +3527,7 @@ public void testMemoryResourceMultipleRanges() throws Exception
@Test
public void testMemoryResourceMultipleRangesUsingBufferedHttpContent() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
DefaultServlet defaultServlet = new DefaultServlet();
context.addServlet(new ServletHolder(defaultServlet), "/");
defaultServlet.getResourceService().setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain")
Expand Down Expand Up @@ -3560,7 +3560,7 @@ public ByteBuffer getByteBuffer()
@Test
public void testNotAcceptRanges() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
DefaultServlet defaultServlet = new DefaultServlet();
context.addServlet(new ServletHolder(defaultServlet), "/");
defaultServlet.getResourceService().setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void init(WorkDir workDir) throws Exception
URLClassLoader extraClassLoader = new URLClassLoader(urls, parentClassLoader);

context = new ServletContextHandler();
context.setBaseResource(ResourceFactory.root().newResource(docRoot));
context.setBaseResource(ResourceFactory.of(context).newResource(docRoot));
context.setContextPath("/context");
context.setWelcomeFiles(new String[]{"index.html", "index.jsp", "index.htm"});
context.setClassLoader(extraClassLoader);
Expand Down Expand Up @@ -2275,7 +2275,7 @@ public void testOptions() throws Exception
@Test
public void testMemoryResourceRange() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
ResourceService resourceService = new ResourceService();
resourceService.setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
DefaultServlet defaultServlet = new DefaultServlet(resourceService);
Expand All @@ -2297,7 +2297,7 @@ public void testMemoryResourceRange() throws Exception
@Test
public void testMemoryResourceMultipleRanges() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
ResourceService resourceService = new ResourceService();
resourceService.setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
DefaultServlet defaultServlet = new DefaultServlet(resourceService);
Expand All @@ -2322,7 +2322,7 @@ public void testMemoryResourceMultipleRanges() throws Exception
@Test
public void testMemoryResourceRangeUsingBufferedHttpContent() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
ResourceService resourceService = new ResourceService();
resourceService.setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain")
{
Expand Down Expand Up @@ -2353,7 +2353,7 @@ public ByteBuffer getByteBuffer()
@Test
public void testMemoryResourceMultipleRangesUsingBufferedHttpContent() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
ResourceService resourceService = new ResourceService();
resourceService.setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain")
{
Expand Down Expand Up @@ -2387,7 +2387,7 @@ public ByteBuffer getByteBuffer()
@Test
public void testNotAcceptRanges() throws Exception
{
Resource memResource = ResourceFactory.root().newMemoryResource(getClass().getResource("/contextResources/test.txt"));
Resource memResource = ResourceFactory.of(context).newMemoryResource(getClass().getResource("/contextResources/test.txt"));
ResourceService resourceService = new ResourceService();
resourceService.setHttpContentFactory(path -> new ResourceHttpContent(memResource, "text/plain"));
resourceService.setAcceptRanges(false);
Expand Down

0 comments on commit 664c118

Please sign in to comment.