Skip to content

Commit 5ea74d6

Browse files
committed
output-api: refactor API
- Make Output more simply, so it allows reuse and sharing - Added BufferedOutput for buffers
1 parent 27a3650 commit 5ea74d6

File tree

57 files changed

+689
-557
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+689
-557
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().getContextOutputFactory();
696+
return getRouter().getOutputFactory().getContextFactory();
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().getContextOutputFactory();
675+
return ctx.getOutputFactory().getContextFactory();
676676
}
677677

678678
@Override

jooby/src/main/java/io/jooby/ServerSentMessage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public ServerSentMessage(@NonNull Object data) {
137137
var route = ctx.getRoute();
138138
var encoder = route.getEncoder();
139139
var bufferFactory = ctx.getOutputFactory();
140-
var buffer = bufferFactory.newOutput();
140+
var buffer = bufferFactory.allocate();
141141

142142
if (id != null) {
143143
buffer.write(ID);

jooby/src/main/java/io/jooby/internal/handler/ChunkedSubscriber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void onComplete() {
117117
}
118118

119119
private static Output prepend(Context ctx, Output data, byte c) {
120-
var buffer = ctx.getOutputFactory().newCompositeOutput();
120+
var buffer = ctx.getOutputFactory().newComposite();
121121
buffer.write(c);
122122
data.transferTo(buffer::write);
123123
return buffer;

jooby/src/main/java/io/jooby/internal/output/CompsiteByteBufferOutput.java renamed to jooby/src/main/java/io/jooby/internal/output/CompositeOutput.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
package io.jooby.internal.output;
77

88
import java.nio.ByteBuffer;
9-
import java.nio.charset.Charset;
109
import java.util.ArrayList;
1110
import java.util.List;
1211

1312
import edu.umd.cs.findbugs.annotations.NonNull;
1413
import io.jooby.Context;
1514
import io.jooby.SneakyThrows;
16-
import io.jooby.output.Output;
15+
import io.jooby.output.BufferedOutput;
1716

18-
public class CompsiteByteBufferOutput implements Output {
17+
/**
18+
* Merge buffers into one.
19+
*
20+
* @author edgar
21+
* @since 4.0.0
22+
*/
23+
public class CompositeOutput implements BufferedOutput {
1924
private final List<ByteBuffer> chunks = new ArrayList<>();
2025
private int size = 0;
2126

@@ -25,25 +30,25 @@ public int size() {
2530
}
2631

2732
@Override
28-
public Output write(byte b) {
33+
public BufferedOutput write(byte b) {
2934
addChunk(ByteBuffer.wrap(new byte[] {b}));
3035
return this;
3136
}
3237

3338
@Override
34-
public Output write(byte[] source) {
39+
public BufferedOutput write(byte[] source) {
3540
addChunk(ByteBuffer.wrap(source));
3641
return this;
3742
}
3843

3944
@Override
40-
public Output write(byte[] source, int offset, int length) {
45+
public BufferedOutput write(byte[] source, int offset, int length) {
4146
addChunk(ByteBuffer.wrap(source, offset, length));
4247
return this;
4348
}
4449

4550
@Override
46-
public Output clear() {
51+
public BufferedOutput clear() {
4752
chunks.forEach(ByteBuffer::clear);
4853
chunks.clear();
4954
return this;
@@ -57,18 +62,13 @@ public Output clear() {
5762
@Override
5863
public ByteBuffer asByteBuffer() {
5964
var buf = ByteBuffer.allocate(size);
60-
chunks.forEach(buf::put);
65+
for (ByteBuffer chunk : chunks) {
66+
buf.put(chunk.duplicate());
67+
}
6168
buf.flip();
6269
return buf;
6370
}
6471

65-
@Override
66-
public String asString(@NonNull Charset charset) {
67-
var sb = new StringBuilder();
68-
chunks.forEach(bytes -> sb.append(charset.decode(bytes)));
69-
return sb.toString();
70-
}
71-
7272
@Override
7373
public void transferTo(@NonNull SneakyThrows.Consumer<ByteBuffer> consumer) {
7474
chunks.forEach(consumer);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal.output;
7+
8+
import java.nio.ByteBuffer;
9+
import java.nio.charset.Charset;
10+
11+
import edu.umd.cs.findbugs.annotations.NonNull;
12+
import io.jooby.output.ByteBufferedOutputFactory;
13+
import io.jooby.output.Output;
14+
import io.jooby.output.OutputOptions;
15+
16+
public class ContextOutputFactory extends ByteBufferedOutputFactory {
17+
public ContextOutputFactory(OutputOptions options) {
18+
super(options);
19+
}
20+
21+
@Override
22+
public Output wrap(@NonNull ByteBuffer buffer) {
23+
return new WrappedOutput(buffer);
24+
}
25+
26+
@Override
27+
public Output wrap(@NonNull String value, @NonNull Charset charset) {
28+
return new WrappedOutput(charset.encode(value));
29+
}
30+
31+
@Override
32+
public Output wrap(@NonNull byte[] bytes) {
33+
return new WrappedOutput(ByteBuffer.wrap(bytes));
34+
}
35+
36+
@Override
37+
public Output wrap(@NonNull byte[] bytes, int offset, int length) {
38+
return new WrappedOutput(ByteBuffer.wrap(bytes, offset, length));
39+
}
40+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
99
import java.io.OutputStream;
1010

1111
import edu.umd.cs.findbugs.annotations.NonNull;
12+
import io.jooby.output.BufferedOutput;
1213
import io.jooby.output.Output;
1314

1415
/**
1516
* An {@link OutputStream} that writes to a {@link Output}.
1617
*
17-
* @see Output#asOutputStream()
18+
* @see BufferedOutput#asOutputStream()
1819
*/
1920
public class OutputOutputStream extends OutputStream {
2021

21-
private final Output output;
22+
private final BufferedOutput output;
2223

2324
private boolean closed;
2425

25-
public OutputOutputStream(@NonNull Output output) {
26+
public OutputOutputStream(@NonNull BufferedOutput output) {
2627
this.output = output;
2728
}
2829

@@ -46,7 +47,6 @@ public void close() throws IOException {
4647
return;
4748
}
4849
this.closed = true;
49-
output.clear();
5050
}
5151

5252
private void checkClosed() throws IOException {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import java.nio.charset.Charset;
1212

1313
import edu.umd.cs.findbugs.annotations.NonNull;
14-
import io.jooby.output.Output;
14+
import io.jooby.output.BufferedOutput;
1515

1616
public class OutputWriter extends Writer {
17-
private final Output output;
17+
private final BufferedOutput output;
1818
private final Charset charset;
1919
private boolean closed;
2020

21-
public OutputWriter(@NonNull Output output, @NonNull Charset charset) {
21+
public OutputWriter(@NonNull BufferedOutput output, @NonNull Charset charset) {
2222
this.output = output;
2323
this.charset = charset;
2424
}
@@ -61,7 +61,6 @@ public void close() throws IOException {
6161
return;
6262
}
6363
this.closed = true;
64-
output.clear();
6564
}
6665

6766
private void checkClosed() throws IOException {

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,27 @@
66
package io.jooby.internal.output;
77

88
import java.nio.ByteBuffer;
9-
import java.nio.charset.Charset;
109
import java.util.function.Supplier;
1110

1211
import edu.umd.cs.findbugs.annotations.NonNull;
1312
import io.jooby.Context;
1413
import io.jooby.SneakyThrows;
1514
import io.jooby.output.Output;
1615

17-
public class ByteBufferOutputStatic implements Output {
16+
public class StaticOutput implements Output {
1817

1918
private final int size;
2019
private final Supplier<ByteBuffer> provider;
2120

22-
public ByteBufferOutputStatic(int size, Supplier<ByteBuffer> provider) {
21+
public StaticOutput(int size, Supplier<ByteBuffer> provider) {
2322
this.size = size;
2423
this.provider = provider;
2524
}
2625

27-
public ByteBufferOutputStatic(ByteBuffer byteBuffer) {
26+
public StaticOutput(ByteBuffer byteBuffer) {
2827
this(byteBuffer.remaining(), () -> byteBuffer);
2928
}
3029

31-
@Override
32-
public Output write(byte b) {
33-
throw new UnsupportedOperationException();
34-
}
35-
36-
@Override
37-
public Output write(byte[] source) {
38-
throw new UnsupportedOperationException();
39-
}
40-
41-
@Override
42-
public Output write(byte[] source, int offset, int length) {
43-
throw new UnsupportedOperationException();
44-
}
45-
46-
@Override
47-
public Output clear() {
48-
return this;
49-
}
50-
5130
@Override
5231
public int size() {
5332
return size;
@@ -60,12 +39,8 @@ public void transferTo(@NonNull SneakyThrows.Consumer<ByteBuffer> consumer) {
6039

6140
@Override
6241
public ByteBuffer asByteBuffer() {
63-
return provider.get();
64-
}
65-
66-
@Override
67-
public String asString(@NonNull Charset charset) {
68-
return charset.decode(asByteBuffer()).toString();
42+
var buffer = provider.get();
43+
return buffer.slice().asReadOnlyBuffer();
6944
}
7045

7146
@Override
@@ -75,6 +50,6 @@ public String toString() {
7550

7651
@Override
7752
public void send(Context ctx) {
78-
ctx.send(asByteBuffer());
53+
ctx.send(provider.get());
7954
}
8055
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.internal.output;
7+
8+
import java.nio.ByteBuffer;
9+
10+
import edu.umd.cs.findbugs.annotations.NonNull;
11+
import io.jooby.Context;
12+
import io.jooby.SneakyThrows;
13+
import io.jooby.output.Output;
14+
import io.jooby.output.OutputFactory;
15+
16+
/** This is part of {@link OutputFactory#getContextFactory()}. */
17+
public class WrappedOutput implements Output {
18+
19+
private final ByteBuffer buffer;
20+
21+
public WrappedOutput(ByteBuffer buffer) {
22+
this.buffer = buffer;
23+
}
24+
25+
@Override
26+
public int size() {
27+
return buffer.remaining();
28+
}
29+
30+
@Override
31+
public void transferTo(@NonNull SneakyThrows.Consumer<ByteBuffer> consumer) {
32+
consumer.accept(asByteBuffer());
33+
}
34+
35+
@Override
36+
public ByteBuffer asByteBuffer() {
37+
return buffer.slice().asReadOnlyBuffer();
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "size=" + size();
43+
}
44+
45+
@Override
46+
public void send(Context ctx) {
47+
ctx.send(buffer);
48+
}
49+
}

0 commit comments

Comments
 (0)