Skip to content

Commit aef9d6a

Browse files
committed
output api: code clean up + refactor
1 parent cee978b commit aef9d6a

File tree

25 files changed

+92
-236
lines changed

25 files changed

+92
-236
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import java.nio.ByteBuffer;
99
import java.nio.charset.Charset;
10+
import java.util.Iterator;
11+
import java.util.List;
1012

1113
import edu.umd.cs.findbugs.annotations.NonNull;
1214
import io.jooby.Context;
@@ -59,6 +61,11 @@ public void accept(SneakyThrows.Consumer<ByteBuffer> consumer) {
5961
consumer.accept(asByteBuffer());
6062
}
6163

64+
@Override
65+
public Iterator<ByteBuffer> iterator() {
66+
return List.of(asByteBuffer()).iterator();
67+
}
68+
6269
private int writableByteCount() {
6370
return this.capacity - this.writePosition;
6471
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ default void toByteBuffer(ByteBuffer dest) {
4242
toByteBuffer(0, dest, dest.position(), size());
4343
}
4444

45+
/**
46+
* Copies the given length from this data buffer into the given destination {@code ByteBuffer},
47+
* beginning at the given source position, and the given destination position in the destination
48+
* byte buffer.
49+
*
50+
* @param srcPos the position of this data buffer from where copying should start
51+
* @param dest the destination byte buffer
52+
* @param destPos the position in {@code dest} to where copying should start
53+
* @param length the amount of data to copy
54+
*/
55+
default void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) {
56+
dest = dest.duplicate().clear();
57+
dest.put(destPos, asByteBuffer(), srcPos, length);
58+
}
59+
4560
default Iterator<ByteBuffer> split(IntPredicate predicate) {
4661
// TODO: fix me for chunks
4762
var buffer = asByteBuffer();
@@ -60,21 +75,6 @@ default Iterator<ByteBuffer> split(IntPredicate predicate) {
6075
return chunks.iterator();
6176
}
6277

63-
/**
64-
* Copies the given length from this data buffer into the given destination {@code ByteBuffer},
65-
* beginning at the given source position, and the given destination position in the destination
66-
* byte buffer.
67-
*
68-
* @param srcPos the position of this data buffer from where copying should start
69-
* @param dest the destination byte buffer
70-
* @param destPos the position in {@code dest} to where copying should start
71-
* @param length the amount of data to copy
72-
*/
73-
default void toByteBuffer(int srcPos, ByteBuffer dest, int destPos, int length) {
74-
dest = dest.duplicate().clear();
75-
dest.put(destPos, asByteBuffer(), srcPos, length);
76-
}
77-
7878
void accept(SneakyThrows.Consumer<ByteBuffer> consumer);
7979

8080
default Iterator<ByteBuffer> iterator() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ final class OutputOutputStream extends OutputStream {
2121

2222
private boolean closed;
2323

24-
public OutputOutputStream(@NonNull Output dataBuffer) {
25-
this.output = dataBuffer;
24+
public OutputOutputStream(@NonNull Output output) {
25+
this.output = output;
2626
}
2727

2828
@Override

modules/jooby-avaje-jsonb/src/main/java/io/jooby/avaje/jsonb/AvajeJsonbModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import io.jooby.MessageDecoder;
1919
import io.jooby.MessageEncoder;
2020
import io.jooby.ServiceRegistry;
21-
import io.jooby.internal.avaje.jsonb.DataBufferJsonOutput;
21+
import io.jooby.internal.avaje.jsonb.BufferedJsonOutput;
2222
import io.jooby.output.Output;
2323

2424
/**
@@ -109,7 +109,7 @@ public Output encode(@NonNull Context ctx, @NonNull Object value) {
109109
ctx.setDefaultResponseType(MediaType.json);
110110
var factory = ctx.getOutputFactory();
111111
var buffer = factory.newBufferedOutput();
112-
try (var writer = jsonb.writer(new DataBufferJsonOutput(buffer))) {
112+
try (var writer = jsonb.writer(new BufferedJsonOutput(buffer))) {
113113
jsonb.toJson(value, writer);
114114
return buffer;
115115
}

modules/jooby-avaje-jsonb/src/main/java/io/jooby/internal/avaje/jsonb/DataBufferJsonOutput.java renamed to modules/jooby-avaje-jsonb/src/main/java/io/jooby/internal/avaje/jsonb/BufferedJsonOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import io.avaje.json.stream.JsonOutput;
1212
import io.jooby.output.Output;
1313

14-
public class DataBufferJsonOutput implements JsonOutput {
14+
public class BufferedJsonOutput implements JsonOutput {
1515
private final Output output;
1616

17-
public DataBufferJsonOutput(Output output) {
17+
public BufferedJsonOutput(Output output) {
1818
this.output = output;
1919
}
2020

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ public class JettyServer extends io.jooby.Server.Base {
5454

5555
private Consumer<HttpConfiguration> httpConfigurer;
5656

57-
// TODO: integrate buffer factory with Jetty.
58-
// private DataBufferFactory bufferFactory;
59-
6057
public JettyServer(@NonNull QueuedThreadPool threadPool) {
6158
this.threadPool = threadPool;
6259
}

modules/jooby-jte/src/main/java/io/jooby/internal/jte/DataBufferOutput.java renamed to modules/jooby-jte/src/main/java/io/jooby/internal/jte/BufferedTemplateOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import gg.jte.TemplateOutput;
1212
import io.jooby.output.Output;
1313

14-
public class DataBufferOutput implements TemplateOutput {
14+
public class BufferedTemplateOutput implements TemplateOutput {
1515
private final Output buffer;
1616
private final Charset charset;
1717

18-
public DataBufferOutput(Output buffer, Charset charset) {
18+
public BufferedTemplateOutput(Output buffer, Charset charset) {
1919
this.buffer = buffer;
2020
this.charset = charset;
2121
}

modules/jooby-jte/src/main/java/io/jooby/internal/jte/JteModelEncoder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public class JteModelEncoder implements io.jooby.MessageEncoder {
1818
public Output encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
1919
if (value instanceof JteModel jte) {
2020
var buffer = ctx.getOutputFactory().newBufferedOutput();
21-
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
22-
jte.render(output);
21+
jte.render(new BufferedTemplateOutput(buffer, StandardCharsets.UTF_8));
2322
return buffer;
2423
}
2524
return null;

modules/jooby-jte/src/main/java/io/jooby/jte/JteTemplateEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.jooby.Context;
1515
import io.jooby.MapModelAndView;
1616
import io.jooby.ModelAndView;
17-
import io.jooby.internal.jte.DataBufferOutput;
17+
import io.jooby.internal.jte.BufferedTemplateOutput;
1818
import io.jooby.output.Output;
1919

2020
class JteTemplateEngine implements io.jooby.TemplateEngine {
@@ -34,7 +34,7 @@ public List<String> extensions() {
3434
@Override
3535
public Output render(Context ctx, ModelAndView<?> modelAndView) {
3636
var buffer = ctx.getOutputFactory().newBufferedOutput();
37-
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
37+
var output = new BufferedTemplateOutput(buffer, StandardCharsets.UTF_8);
3838
var attributes = ctx.getAttributes();
3939
if (modelAndView instanceof MapModelAndView mapModelAndView) {
4040
var mapModel = new HashMap<String, Object>();

modules/jooby-jte/src/test/java/io/jooby/internal/jte/DataBufferOutputTest.java renamed to modules/jooby-jte/src/test/java/io/jooby/internal/jte/BufferedTemplateOutputTest.java

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

1414
import io.jooby.output.DefaultOutputFactory;
1515

16-
public class DataBufferOutputTest {
16+
public class BufferedTemplateOutputTest {
1717

1818
@Test
1919
public void checkWriteContent() {
2020
var factory = new DefaultOutputFactory();
2121
var buffer = factory.newBufferedOutput();
22-
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
22+
var output = new BufferedTemplateOutput(buffer, StandardCharsets.UTF_8);
2323
output.writeContent("Hello");
2424
assertEquals("Hello", buffer.asString(StandardCharsets.UTF_8));
2525
}
@@ -28,7 +28,7 @@ public void checkWriteContent() {
2828
public void checkWriteContentSubstring() {
2929
var factory = new DefaultOutputFactory();
3030
var buffer = factory.newBufferedOutput();
31-
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
31+
var output = new BufferedTemplateOutput(buffer, StandardCharsets.UTF_8);
3232
output.writeContent(" Hello World! ", 1, " Hello World! ".length() - 2);
3333
assertEquals("Hello World", buffer.asString(StandardCharsets.UTF_8));
3434
}
@@ -37,7 +37,7 @@ public void checkWriteContentSubstring() {
3737
public void checkWriteBinaryContent() {
3838
var factory = new DefaultOutputFactory();
3939
var buffer = factory.newBufferedOutput();
40-
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
40+
var output = new BufferedTemplateOutput(buffer, StandardCharsets.UTF_8);
4141
output.writeBinaryContent("Hello".getBytes(StandardCharsets.UTF_8));
4242
assertEquals("Hello", buffer.asString(StandardCharsets.UTF_8));
4343
}

0 commit comments

Comments
 (0)