Skip to content

Commit 27a3650

Browse files
committed
open-api: javadoc
1 parent c207551 commit 27a3650

File tree

14 files changed

+98
-56
lines changed

14 files changed

+98
-56
lines changed

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,6 @@ default Context sendError(@NonNull Throwable cause, @NonNull StatusCode code) {
693693

694694
@Override
695695
default OutputFactory getOutputFactory() {
696-
return getRouter().getOutputFactory();
696+
return getRouter().getOutputFactory().getContextOutputFactory();
697697
}
698698
}

jooby/src/main/java/io/jooby/ForwardingContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ public Router getRouter() {
672672

673673
@Override
674674
public OutputFactory getOutputFactory() {
675-
return ctx.getOutputFactory();
675+
return ctx.getOutputFactory().getContextOutputFactory();
676676
}
677677

678678
@Override

jooby/src/main/java/io/jooby/internal/output/ByteBufferWrappedOutput.java renamed to jooby/src/main/java/io/jooby/internal/output/ByteBufferOutputStatic.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77

88
import java.nio.ByteBuffer;
99
import java.nio.charset.Charset;
10+
import java.util.function.Supplier;
1011

1112
import edu.umd.cs.findbugs.annotations.NonNull;
1213
import io.jooby.Context;
1314
import io.jooby.SneakyThrows;
1415
import io.jooby.output.Output;
1516

16-
public class ByteBufferWrappedOutput implements Output {
17+
public class ByteBufferOutputStatic implements Output {
1718

18-
private final ByteBuffer buffer;
19+
private final int size;
20+
private final Supplier<ByteBuffer> provider;
1921

20-
public ByteBufferWrappedOutput(ByteBuffer buffer) {
21-
this.buffer = buffer;
22+
public ByteBufferOutputStatic(int size, Supplier<ByteBuffer> provider) {
23+
this.size = size;
24+
this.provider = provider;
25+
}
26+
27+
public ByteBufferOutputStatic(ByteBuffer byteBuffer) {
28+
this(byteBuffer.remaining(), () -> byteBuffer);
2229
}
2330

2431
@Override
@@ -38,23 +45,22 @@ public Output write(byte[] source, int offset, int length) {
3845

3946
@Override
4047
public Output clear() {
41-
buffer.clear();
4248
return this;
4349
}
4450

4551
@Override
4652
public int size() {
47-
return buffer.remaining();
53+
return size;
4854
}
4955

5056
@Override
5157
public void transferTo(@NonNull SneakyThrows.Consumer<ByteBuffer> consumer) {
52-
consumer.accept(buffer);
58+
consumer.accept(asByteBuffer());
5359
}
5460

5561
@Override
5662
public ByteBuffer asByteBuffer() {
57-
return buffer.duplicate();
63+
return provider.get();
5864
}
5965

6066
@Override
@@ -69,6 +75,6 @@ public String toString() {
6975

7076
@Override
7177
public void send(Context ctx) {
72-
ctx.send(buffer);
78+
ctx.send(asByteBuffer());
7379
}
7480
}

jooby/src/main/java/io/jooby/internal/output/CompsiteByteBufferOutput.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public void send(Context ctx) {
8686

8787
private void addChunk(ByteBuffer chunk) {
8888
chunks.add(chunk);
89-
int length = chunk.remaining();
90-
size += length;
89+
size += chunk.remaining();
9190
}
9291
}

jooby/src/main/java/io/jooby/output/ByteBufferOutput.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public Output write(@NonNull ByteBuffer source) {
108108

109109
@Override
110110
public Output clear() {
111+
this.writePosition = 0;
112+
this.readPosition = 0;
111113
this.buffer.clear();
112114
return this;
113115
}

jooby/src/main/java/io/jooby/output/ByteBufferOutputFactory.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package io.jooby.output;
77

88
import java.nio.ByteBuffer;
9+
import java.nio.charset.Charset;
910

1011
import edu.umd.cs.findbugs.annotations.NonNull;
11-
import io.jooby.internal.output.ByteBufferWrappedOutput;
12+
import io.jooby.internal.output.ByteBufferOutputStatic;
1213
import io.jooby.internal.output.CompsiteByteBufferOutput;
1314

1415
/**
@@ -45,18 +46,24 @@ public Output newCompositeOutput() {
4546
return new CompsiteByteBufferOutput();
4647
}
4748

49+
@Override
50+
public Output wrap(@NonNull String value, @NonNull Charset charset) {
51+
return new ByteBufferOutputStatic(ByteBuffer.wrap(value.getBytes(charset)));
52+
}
53+
4854
@Override
4955
public Output wrap(@NonNull ByteBuffer buffer) {
50-
return new ByteBufferWrappedOutput(buffer);
56+
return new ByteBufferOutputStatic(buffer.remaining(), () -> buffer);
5157
}
5258

5359
@Override
5460
public Output wrap(@NonNull byte[] bytes) {
55-
return new ByteBufferWrappedOutput(ByteBuffer.wrap(bytes));
61+
return new ByteBufferOutputStatic(bytes.length, () -> ByteBuffer.wrap(bytes));
5662
}
5763

5864
@Override
5965
public Output wrap(@NonNull byte[] bytes, int offset, int length) {
60-
return new ByteBufferWrappedOutput(ByteBuffer.wrap(bytes, offset, length));
66+
return new ByteBufferOutputStatic(
67+
length - offset, () -> ByteBuffer.wrap(bytes, offset, length));
6168
}
6269
}

jooby/src/main/java/io/jooby/output/OutputFactory.java

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
package io.jooby.output;
77

8-
import static java.lang.ThreadLocal.withInitial;
9-
108
import java.nio.ByteBuffer;
119
import java.nio.charset.Charset;
1210
import java.nio.charset.StandardCharsets;
@@ -21,34 +19,6 @@
2119
*/
2220
public interface OutputFactory {
2321

24-
/**
25-
* Thread local for output buffer. Please note only store calls to {@link #newOutput()}, {@link
26-
* #newCompositeOutput()} are not saved into thread local.
27-
*
28-
* @param factory Factory.
29-
* @return Thread local factory.
30-
*/
31-
static OutputFactory threadLocal(OutputFactory factory) {
32-
return new ForwardingOutputFactory(factory) {
33-
private final ThreadLocal<Output> threadLocal = withInitial(factory::newOutput);
34-
35-
@Override
36-
public Output newOutput(boolean direct, int size) {
37-
return threadLocal.get().clear();
38-
}
39-
40-
@Override
41-
public Output newOutput(int size) {
42-
return threadLocal.get().clear();
43-
}
44-
45-
@Override
46-
public Output newOutput() {
47-
return threadLocal.get().clear();
48-
}
49-
};
50-
}
51-
5222
/**
5323
* Default output factory, backed by {@link ByteBuffer}.
5424
*
@@ -148,4 +118,13 @@ default Output wrap(@NonNull String value, @NonNull Charset charset) {
148118
* @return Readonly buffer.
149119
*/
150120
Output wrap(@NonNull byte[] bytes, int offset, int length);
121+
122+
/**
123+
* Special implementation when output factory is requested from {@link io.jooby.Context}.
124+
*
125+
* @return Same or custom implementation.
126+
*/
127+
default OutputFactory getContextOutputFactory() {
128+
return this;
129+
}
151130
}

jooby/src/test/java/io/jooby/output/OutputTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.junit.jupiter.api.Test;
1616

1717
import io.jooby.SneakyThrows;
18-
import io.jooby.internal.output.ByteBufferWrappedOutput;
18+
import io.jooby.internal.output.ByteBufferOutputStatic;
1919
import io.jooby.internal.output.CompsiteByteBufferOutput;
2020

2121
public class OutputTest {
@@ -104,7 +104,7 @@ public void chunkedOutput() {
104104
@Test
105105
public void wrapOutput() throws IOException {
106106
var bytes = "xxHello World!xx".getBytes(StandardCharsets.UTF_8);
107-
var output = new ByteBufferWrappedOutput(ByteBuffer.wrap(bytes, 2, bytes.length - 4));
107+
var output = new ByteBufferOutputStatic(ByteBuffer.wrap(bytes, 2, bytes.length - 4));
108108
assertEquals("Hello World!", output.asString(StandardCharsets.UTF_8));
109109
assertEquals(12, output.size());
110110
}

modules/jooby-avaje-jsonb/src/test/java/io/jooby/avaje/jsonb/AvajeJsonbEncoderBench.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ public void setup() {
4343

4444
@Benchmark
4545
public void withJsonBuffer() {
46-
jsonb.toJsonBytes(message);
46+
factory.wrap(jsonb.toJsonBytes(message));
4747
}
4848

4949
@Benchmark
50-
public void witCachedBufferedOutput() {
50+
public void withCachedBufferedOutput() {
5151
var buffer = cache.get().clear();
5252
jsonb.toJson(message, jsonb.writer(new BufferedJsonOutput(buffer)));
5353
}
5454

5555
@Benchmark
56-
public void witBufferedOutput() {
56+
public void withBufferedOutput() {
5757
var buffer = factory.newOutput(1024);
5858
jsonb.toJson(message, jsonb.writer(new BufferedJsonOutput(buffer)));
5959
}
60+
61+
@Benchmark
62+
public void withCompositeOutput() {
63+
var buffer = factory.newCompositeOutput();
64+
jsonb.toJson(message, jsonb.writer(new BufferedJsonOutput(buffer)));
65+
}
6066
}

modules/jooby-jetty/src/main/java/io/jooby/jetty/JettyServer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public OutputFactory getOutputFactory() {
9191
return outputFactory;
9292
}
9393

94+
public JettyServer setOutputFactory(OutputFactory outputFactory) {
95+
this.outputFactory = outputFactory;
96+
return this;
97+
}
98+
9499
@NonNull @Override
95100
public String getName() {
96101
return NAME;

0 commit comments

Comments
 (0)