Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public void write(int byteVal) throws IOException {
(compressedStream != null ? compressedStream : originStream).write(byteVal);
}

@Override
public void flush() throws IOException {
if (compressedStream != null) {
compressedStream.flush();
} else {
originStream.flush();
}
}

@Override
public void close() throws IOException {
if (compressedStream != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ private void useJdkOutput() throws IOException {
}
}

@Override
public void flush() throws IOException {
if (!jdkOutput) {
useJdkOutput();
}
out.flush();
}

@Override
public void close() throws IOException {
if (jdkOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.http.HttpResponse;
import java.util.zip.GZIPInputStream;

Expand All @@ -22,18 +24,21 @@ static TestPair init() {

final Jex app =
Jex.create()
.routing(
r ->
r.get(
"/compress",
ctx ->
ctx.contentType(ContentType.APPLICATION_JSON)
.write(CompressionTest.class.getResourceAsStream("/64KB.json")))
.get(
"/sus",
ctx ->
ctx.write(
CompressionTest.class.getResourceAsStream("/public/sus.txt"))));
.get(
"/compress",
ctx ->
ctx.contentType(ContentType.APPLICATION_JSON)
.write(CompressionTest.class.getResourceAsStream("/64KB.json")))
.get(
"/flush",
ctx -> {
transferStream(
CompressionTest.class.getResourceAsStream("/64KB.json"),
ctx.contentType(ContentType.APPLICATION_JSON).outputStream());
})
.get(
"/sus",
ctx -> ctx.write(CompressionTest.class.getResourceAsStream("/public/sus.txt")));

return TestPair.create(app);
}
Expand Down Expand Up @@ -62,6 +67,25 @@ void testCompression() throws IOException {
assertThat(decompressed).isEqualTo(expected);
}

@Test
void testCompressionFlush() throws IOException {
var res =
pair.request()
.header(Constants.ACCEPT_ENCODING, "deflate, gzip;q=1.0, *;q=0.5")
.path("flush")
.GET()
.asInputStream();
assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.headers().firstValue(Constants.CONTENT_ENCODING)).contains("gzip");

var expected = CompressionTest.class.getResourceAsStream("/64KB.json").readAllBytes();

final var gzipInputStream = new GZIPInputStream(res.body());
var decompressed = gzipInputStream.readAllBytes();
gzipInputStream.close();
assertThat(decompressed).isEqualTo(expected);
}

@Test
void testNoCompression() {
HttpResponse<String> res =
Expand All @@ -88,4 +112,26 @@ void testCompressionRange() throws IOException {
gzipInputStream.close();
assertThat(decompressed).isEqualTo(expected);
}

/**
* Reads data from the input stream in chunks of 1024 bytes and writes it immediately to the
* output stream.
*
* @param in The InputStream to read data from.
* @param out The OutputStream to write data to.
* @throws IOException If an I/O error occurs during read or write operations.
*/
public static void transferStream(InputStream in, OutputStream out) throws IOException {
// Define the buffer size as 1024 bytes (1KB)
final int BUFFER_SIZE = 2048;
byte[] buffer = new byte[BUFFER_SIZE];

int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {

out.write(buffer, 0, bytesRead);
out.flush();
}
out.close();
}
}