From 0c9f7c2d3c79fa39839a9b4d93ee26afa9be6029 Mon Sep 17 00:00:00 2001 From: Santiago Pericas-Geertsen Date: Wed, 2 Oct 2024 10:04:45 -0400 Subject: [PATCH] Allows the webserver's write buffer size to be set to 0 (or less). The underlying output stream shall not be wrapped into a buffered output stream one unless this value is greater than 0. --- .../tests/OutputStreamBufferSizeTest.java | 35 +++++++++++++++++++ .../webserver/tests/OutputStreamTest.java | 4 +-- .../webserver/http1/Http1ServerResponse.java | 8 +++-- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamBufferSizeTest.java diff --git a/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamBufferSizeTest.java b/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamBufferSizeTest.java new file mode 100644 index 00000000000..966e1a54549 --- /dev/null +++ b/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamBufferSizeTest.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2024 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.webserver.tests; + +import io.helidon.webclient.http1.Http1Client; +import io.helidon.webserver.WebServerConfig; +import io.helidon.webserver.testing.junit5.ServerTest; +import io.helidon.webserver.testing.junit5.SetUpServer; + +@ServerTest +class OutputStreamBufferSizeTest extends OutputStreamTest { + + OutputStreamBufferSizeTest(Http1Client client) { + super(client); + } + + @SetUpServer + static void setup(WebServerConfig.Builder builder) { + builder.writeBufferSize(0); // should accept buffer size of 0 + } +} diff --git a/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamTest.java b/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamTest.java index 2411e420644..3733604eda5 100644 --- a/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamTest.java +++ b/webserver/tests/webserver/src/test/java/io/helidon/webserver/tests/OutputStreamTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2024 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -59,7 +59,7 @@ void verifyAutoFlush() { } } - private static class Service { + protected static class Service { public static void outputStream(ServerRequest req, ServerResponse res) throws IOException { InputStream in = new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)); diff --git a/webserver/webserver/src/main/java/io/helidon/webserver/http1/Http1ServerResponse.java b/webserver/webserver/src/main/java/io/helidon/webserver/http1/Http1ServerResponse.java index 330611ae76b..cd1194e2440 100644 --- a/webserver/webserver/src/main/java/io/helidon/webserver/http1/Http1ServerResponse.java +++ b/webserver/webserver/src/main/java/io/helidon/webserver/http1/Http1ServerResponse.java @@ -753,15 +753,19 @@ private void writeContent(BufferData buffer) throws IOException { * A special stream that provides buffering for a delegate and special handling * of close logic. Note that due to some locking issues in the JDK, this class * must use delegation with {@link BufferedOutputStream} instead of subclassing. + * + *

If the buffer size is less or equal to zero, it will not wrap the + * {@link io.helidon.webserver.http1.Http1ServerResponse.BlockingOutputStream} + * with a {@link java.io.BufferedOutputStream}. */ static class ClosingBufferedOutputStream extends OutputStream { private final BlockingOutputStream delegate; - private final BufferedOutputStream bufferedDelegate; + private final OutputStream bufferedDelegate; ClosingBufferedOutputStream(BlockingOutputStream out, int size) { this.delegate = out; - this.bufferedDelegate = new BufferedOutputStream(out, size); + this.bufferedDelegate = size <= 0 ? out : new BufferedOutputStream(out, size); } @Override