Skip to content

Commit

Permalink
Issue #12680 - Test of 2GB+ files on ResourceHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime authored and lorban committed Jan 9, 2025
1 parent c3557ca commit 3627f5e
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -760,6 +763,28 @@ public void testBigger() throws Exception
assertThat(response.getContent(), containsString(" 400\tThis is a big file\n"));
}

@Test
public void testOver2GBFile() throws Exception
{
long hugeLength = (long)Integer.MAX_VALUE + 10L;

generateFile(docRoot.resolve("huge.mkv"), hugeLength);

HttpTester.Response response = HttpTester.parseResponse(
_local.getResponse("""
GET /context/huge.mkv HTTP/1.1\r
Host: local\r
Connection: close\r
\r
"""));

System.err.println(response);

assertThat(response.getStatus(), is(HttpStatus.OK_200));
long responseContentLength = response.getLongField(CONTENT_LENGTH);
assertThat(responseContentLength, is(hugeLength));
}

@Test
public void testBrotliInitialCompressed() throws Exception
{
Expand Down Expand Up @@ -3939,6 +3964,40 @@ private void setupBigFiles(Path base) throws Exception
}
}

private void generateFile(Path staticFile, long size) throws Exception
{
byte[] buf = new byte[(int)(1024 * 1024)]; // about 1 MB
Arrays.fill(buf, (byte)'x');
ByteBuffer src = ByteBuffer.wrap(buf);

if (Files.exists(staticFile) && Files.size(staticFile) == size)
{
// all done, nothing left to do.
System.err.printf("File Exists Already: %s (%,d bytes)%n", staticFile, Files.size(staticFile));
return;
}

System.err.printf("Creating %,d byte file: %s ...%n", size, staticFile);
try (SeekableByteChannel channel = Files.newByteChannel(staticFile, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))
{
long remaining = size;
while (remaining > 0)
{
ByteBuffer slice = src.slice();
int len = buf.length;
if (remaining < Integer.MAX_VALUE)
{
len = Math.min(buf.length, (int)remaining);
slice.limit(len);
}

channel.write(slice);
remaining -= len;
}
}
System.err.println(" Done");
}

private void setupQuestionMarkDir(Path base) throws IOException
{
boolean filesystemSupportsQuestionMarkDir = false;
Expand Down

0 comments on commit 3627f5e

Please sign in to comment.