Skip to content

Commit

Permalink
Issue #12695 - restore FileBufferedResponseHandlerTest
Browse files Browse the repository at this point in the history
+ Fallback to normal filesystem read if memory mapping is not supported.
  • Loading branch information
joakime committed Jan 10, 2025
1 parent d1a8929 commit 459700c
Show file tree
Hide file tree
Showing 3 changed files with 729 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,25 @@ public static Response parseResponse(Input input) throws IOException
return parseResponse(input, false);
}

public static Response parseResponse(Input input, Response response) throws IOException
{
return parseResponse(input, response, false);
}

public static Response parseResponse(Input input, boolean head) throws IOException
{
Response response;
return parseResponse(input, new Response(), head);
}

public static Response parseResponse(Input input, Response response, boolean head) throws IOException
{
HttpParser parser = input.takeHttpParser();
if (parser != null)
{
response = (Response)parser.getHandler();
}
else
{
response = new Response();
parser = new HttpParser(response);
}
parser.setHeadResponse(head);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
Expand Down Expand Up @@ -72,7 +73,7 @@ protected BufferedInterceptor newBufferedInterceptor(HttpChannel httpChannel, In

class FileBufferedInterceptor implements BufferedResponseHandler.BufferedInterceptor
{
private static final int MAX_MAPPED_BUFFER_SIZE = Integer.MAX_VALUE / 2;
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE / 2;

private final Interceptor _next;
private final HttpChannel _channel;
Expand Down Expand Up @@ -205,11 +206,9 @@ protected Action process() throws Exception
if (_last)
return Action.SUCCEEDED;

long len = Math.min(MAX_MAPPED_BUFFER_SIZE, fileLength - _pos);
long len = Math.min(MAX_BUFFER_SIZE, fileLength - _pos);
_last = (_pos + len == fileLength);
ByteBuffer buffer = BufferUtil.toMappedBuffer(_filePath, _pos, len);
if (buffer == null)
throw new IOException("Cannot memory map file (not supported by underlying FileSystem): " + _filePath.toUri());
ByteBuffer buffer = readByteBuffer(_filePath, _pos, len);
getNextInterceptor().write(buffer, _last, this);
_pos += len;
return Action.SCHEDULED;
Expand All @@ -231,5 +230,23 @@ protected void onFailure(Throwable cause)
};
icb.iterate();
}

private ByteBuffer readByteBuffer(Path path, long pos, long len) throws IOException
{
ByteBuffer buffer = BufferUtil.toMappedBuffer(path, pos, len);
if (buffer != null)
return buffer;

// if we reached here, then file mapped byte buffers is not supported.
// we fall back to using traditional I/O instead.
try (SeekableByteChannel channel = Files.newByteChannel(path))
{
channel.position(pos);
buffer = ByteBuffer.allocate((int)len);
channel.read(buffer);
buffer.flip();
return buffer;
}
}
}
}
Loading

0 comments on commit 459700c

Please sign in to comment.