Skip to content

Commit 69f9ebe

Browse files
committed
output api: remove wrap from Output
- just keep them at OutputFactory
1 parent 1a0882a commit 69f9ebe

File tree

21 files changed

+164
-92
lines changed

21 files changed

+164
-92
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import io.jooby.internal.MissingValue;
3636
import io.jooby.internal.SingleValue;
3737
import io.jooby.internal.UrlParser;
38-
import io.jooby.output.DefaultOutputFactory;
3938
import io.jooby.output.OutputFactory;
4039
import io.jooby.value.Value;
4140
import io.jooby.value.ValueFactory;
@@ -656,6 +655,6 @@ default Context sendError(@NonNull Throwable cause, @NonNull StatusCode code) {
656655

657656
@Override
658657
default OutputFactory getOutputFactory() {
659-
return new DefaultOutputFactory();
658+
return getRouter().getOutputFactory();
660659
}
661660
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import io.jooby.exception.StatusCodeException;
4545
import io.jooby.internal.handler.ServerSentEventHandler;
4646
import io.jooby.internal.handler.WebSocketHandler;
47-
import io.jooby.output.DefaultOutputFactory;
4847
import io.jooby.output.OutputFactory;
4948
import io.jooby.problem.ProblemDetailsHandler;
5049
import io.jooby.value.ValueFactory;
@@ -172,7 +171,7 @@ public Stack executor(Executor executor) {
172171

173172
private ValueFactory valueFactory = new ValueFactory();
174173

175-
private OutputFactory outputFactory = new DefaultOutputFactory();
174+
private OutputFactory outputFactory = OutputFactory.create(false);
176175

177176
public RouterImpl() {
178177
stack.addLast(new Stack(chi, null));

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ public ByteBufferOutput(boolean direct, int capacity) {
3333
this.capacity = this.buffer.remaining();
3434
}
3535

36-
public ByteBufferOutput(boolean direct) {
37-
this(direct, BUFFER_SIZE);
38-
}
39-
40-
public ByteBufferOutput(int bufferSize) {
41-
this(false, bufferSize);
42-
}
43-
44-
public ByteBufferOutput() {
45-
this(BUFFER_SIZE);
46-
}
47-
4836
@Override
4937
public int size() {
5038
return this.writePosition - this.readPosition;

jooby/src/main/java/io/jooby/output/DefaultOutputFactory.java renamed to jooby/src/main/java/io/jooby/output/ByteBufferOutputFactory.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,34 @@
1919
* @author edgar
2020
* @since 4.0.0
2121
*/
22-
public class DefaultOutputFactory implements OutputFactory {
22+
public class ByteBufferOutputFactory implements OutputFactory {
23+
private int initialBufferSize;
24+
private final boolean direct;
25+
26+
public ByteBufferOutputFactory(boolean direct, int initialBufferSize) {
27+
this.initialBufferSize = initialBufferSize;
28+
this.direct = direct;
29+
}
30+
31+
@Override
32+
public int getInitialBufferSize() {
33+
return initialBufferSize;
34+
}
35+
36+
@Override
37+
public OutputFactory setInitialBufferSize(int initialBufferSize) {
38+
this.initialBufferSize = initialBufferSize;
39+
return this;
40+
}
41+
42+
@Override
43+
public boolean isDirect() {
44+
return direct;
45+
}
46+
2347
@Override
24-
public Output newBufferedOutput(int size) {
25-
return new ByteBufferOutput(size);
48+
public Output newBufferedOutput(boolean direct, int size) {
49+
return new ByteBufferOutput(direct, size);
2650
}
2751

2852
@Override

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,36 @@ public abstract class ForwardingOutputFactory implements OutputFactory {
1919

2020
protected final OutputFactory delegate;
2121

22-
public ForwardingOutputFactory(OutputFactory delegate) {
22+
public ForwardingOutputFactory(@NonNull OutputFactory delegate) {
2323
this.delegate = delegate;
2424
}
2525

26+
@Override
27+
public int getInitialBufferSize() {
28+
return delegate.getInitialBufferSize();
29+
}
30+
31+
@Override
32+
public OutputFactory setInitialBufferSize(int initialBufferSize) {
33+
delegate.setInitialBufferSize(initialBufferSize);
34+
return this;
35+
}
36+
37+
@Override
38+
public boolean isDirect() {
39+
return delegate.isDirect();
40+
}
41+
2642
@Override
2743
public Output newBufferedOutput(int size) {
2844
return delegate.newBufferedOutput(size);
2945
}
3046

47+
@Override
48+
public Output newBufferedOutput(boolean direct, int size) {
49+
return delegate.newBufferedOutput(direct, size);
50+
}
51+
3152
@Override
3253
public Output newCompositeOutput() {
3354
return delegate.newCompositeOutput();

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import edu.umd.cs.findbugs.annotations.NonNull;
1818
import io.jooby.SneakyThrows;
19-
import io.jooby.internal.output.ByteArrayWrappedOutput;
20-
import io.jooby.internal.output.ByteBufferWrappedOutput;
2119
import io.jooby.internal.output.OutputOutputStream;
2220
import io.jooby.internal.output.OutputWriter;
2321

@@ -187,24 +185,4 @@ default Output write(@NonNull CharBuffer source, @NonNull Charset charset) {
187185
void send(io.jooby.Context ctx);
188186

189187
Output clear();
190-
191-
static Output wrap(ByteBuffer buffer) {
192-
return new ByteBufferWrappedOutput(buffer);
193-
}
194-
195-
static Output wrap(byte[] bytes) {
196-
return new ByteArrayWrappedOutput(bytes);
197-
}
198-
199-
static Output wrap(byte[] bytes, int offset, int length) {
200-
return new ByteBufferWrappedOutput(ByteBuffer.wrap(bytes, offset, length));
201-
}
202-
203-
static Output wrap(String value) {
204-
return wrap(value, StandardCharsets.UTF_8);
205-
}
206-
207-
static Output wrap(String value, Charset charset) {
208-
return wrap(value.getBytes(charset));
209-
}
210188
}

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
import edu.umd.cs.findbugs.annotations.NonNull;
1515

16+
/**
17+
* Factory class for buffered {@link Output}.
18+
*
19+
* @author edgar
20+
* @since 4.0.0
21+
*/
1622
public interface OutputFactory {
1723

1824
/**
@@ -22,10 +28,14 @@ public interface OutputFactory {
2228
* @param factory Factory.
2329
* @return Thread local factory.
2430
*/
25-
static OutputFactory threadLocal(OutputFactory factory, int bufferSize) {
31+
static OutputFactory threadLocal(OutputFactory factory) {
2632
return new ForwardingOutputFactory(factory) {
27-
private final ThreadLocal<Output> threadLocal =
28-
withInitial(() -> factory.newBufferedOutput(bufferSize));
33+
private final ThreadLocal<Output> threadLocal = withInitial(factory::newBufferedOutput);
34+
35+
@Override
36+
public Output newBufferedOutput(boolean direct, int size) {
37+
return threadLocal.get().clear();
38+
}
2939

3040
@Override
3141
public Output newBufferedOutput(int size) {
@@ -44,25 +54,62 @@ public Output newBufferedOutput() {
4454
*
4555
* @return Default output factory.
4656
*/
47-
static OutputFactory create() {
48-
return new DefaultOutputFactory();
57+
static OutputFactory create(boolean direct, int bufferSize) {
58+
return new ByteBufferOutputFactory(direct, bufferSize);
59+
}
60+
61+
/**
62+
* Default output factory, backed by {@link ByteBuffer}.
63+
*
64+
* @return Default output factory.
65+
*/
66+
static OutputFactory create(boolean direct) {
67+
return new ByteBufferOutputFactory(direct, Output.BUFFER_SIZE);
4968
}
5069

70+
/**
71+
* Indicates whether this factory allocates direct buffers (i.e. non-heap, native memory).
72+
*
73+
* @return {@code true} if this factory allocates direct buffers; {@code false} otherwise
74+
*/
75+
boolean isDirect();
76+
77+
/**
78+
* Buffer of a default initial capacity. Default capacity is <code>1024</code> bytes.
79+
*
80+
* @return buffer of a default initial capacity.
81+
*/
82+
int getInitialBufferSize();
83+
84+
/**
85+
* Set default buffer initial capacity.
86+
*
87+
* @param initialBufferSize Default initial buffer capacity.
88+
* @return This buffer factory.
89+
*/
90+
OutputFactory setInitialBufferSize(int initialBufferSize);
91+
5192
/**
5293
* Creates a new byte buffered output.
5394
*
95+
* @param direct True for direct buffers.
5496
* @param size Output size.
5597
* @return A byte buffered output.
5698
*/
57-
Output newBufferedOutput(int size);
99+
Output newBufferedOutput(boolean direct, int size);
58100

59101
/**
60102
* Creates a new byte buffered output with an initial size of {@link Output#BUFFER_SIZE}.
61103
*
104+
* @param size Output size.
62105
* @return A byte buffered output.
63106
*/
107+
default Output newBufferedOutput(int size) {
108+
return newBufferedOutput(isDirect(), size);
109+
}
110+
64111
default Output newBufferedOutput() {
65-
return newBufferedOutput(Output.BUFFER_SIZE);
112+
return newBufferedOutput(isDirect(), Output.BUFFER_SIZE);
66113
}
67114

68115
/**

jooby/src/test/java/io/jooby/Issue3607.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private static class TemplateEngineImpl implements TemplateEngine {
1717
@Override
1818
public Output render(Context ctx, ModelAndView<?> modelAndView) throws Exception {
1919
// do nothing
20-
return Output.wrap(new byte[0]);
20+
return ctx.getOutputFactory().wrap(new byte[0]);
2121
}
2222
}
2323

jooby/src/test/java/io/jooby/ServerSentMessageTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import org.junit.jupiter.api.Test;
1515

16-
import io.jooby.output.DefaultOutputFactory;
16+
import io.jooby.output.OutputFactory;
1717

1818
public class ServerSentMessageTest {
1919

@@ -22,7 +22,7 @@ public void shouldFormatMessage() throws Exception {
2222
var data = "some";
2323
var ctx = mock(Context.class);
2424

25-
var bufferFactory = new DefaultOutputFactory();
25+
var bufferFactory = OutputFactory.create(false);
2626
when(ctx.getOutputFactory()).thenReturn(bufferFactory);
2727
var encoder = mock(MessageEncoder.class);
2828
when(encoder.encode(ctx, data))
@@ -42,7 +42,7 @@ public void shouldFormatMultiLineMessage() throws Exception {
4242
var data = "line 1\n line ,a .. 2\nline ...abc 3";
4343
var ctx = mock(Context.class);
4444

45-
var bufferFactory = new DefaultOutputFactory();
45+
var bufferFactory = OutputFactory.create(false);
4646
when(ctx.getOutputFactory()).thenReturn(bufferFactory);
4747
var encoder = mock(MessageEncoder.class);
4848
when(encoder.encode(ctx, data))
@@ -64,7 +64,7 @@ public void shouldFormatMessageEndingWithNL() throws Exception {
6464
var data = "line 1\n";
6565
var ctx = mock(Context.class);
6666

67-
var bufferFactory = new DefaultOutputFactory();
67+
var bufferFactory = OutputFactory.create(false);
6868
when(ctx.getOutputFactory()).thenReturn(bufferFactory);
6969
var encoder = mock(MessageEncoder.class);
7070
when(encoder.encode(ctx, data))

jooby/src/test/java/io/jooby/buffer/Issue3434.java

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

1717
import io.jooby.SneakyThrows;
18-
import io.jooby.output.DefaultOutputFactory;
1918
import io.jooby.output.OutputFactory;
2019

2120
public class Issue3434 {
22-
OutputFactory factory = new DefaultOutputFactory();
21+
OutputFactory factory = OutputFactory.create(false);
2322

2423
@Test
2524
void shouldWriteCharBufferOnBufferWriter() throws IOException {

0 commit comments

Comments
 (0)