Skip to content

Commit 492c516

Browse files
committed
output api: remove public chunked version (no need to be public)
- rename chunked to composite buffer (was truly a composite implementation) - replace output.close with clear
1 parent b40f3ef commit 492c516

File tree

31 files changed

+135
-215
lines changed

31 files changed

+135
-215
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import io.jooby.internal.MissingValue;
3636
import io.jooby.internal.SingleValue;
3737
import io.jooby.internal.UrlParser;
38-
import io.jooby.output.ByteBufferOutputFactory;
38+
import io.jooby.output.DefaultOutputFactory;
3939
import io.jooby.output.OutputFactory;
4040
import io.jooby.value.Value;
4141
import io.jooby.value.ValueFactory;
@@ -656,6 +656,6 @@ default Context sendError(@NonNull Throwable cause, @NonNull StatusCode code) {
656656

657657
@Override
658658
default OutputFactory getOutputFactory() {
659-
return new ByteBufferOutputFactory();
659+
return new DefaultOutputFactory();
660660
}
661661
}

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

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

88
import static java.nio.charset.StandardCharsets.UTF_8;
99

10-
import java.io.IOException;
11-
import java.nio.ByteBuffer;
12-
import java.nio.charset.Charset;
1310
import java.util.function.IntPredicate;
1411

1512
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -173,68 +170,4 @@ public ServerSentMessage(@NonNull Object data) {
173170
throw SneakyThrows.propagate(x);
174171
}
175172
}
176-
177-
class ServerSentMessageOutput implements Output {
178-
private final Output delegate;
179-
180-
public ServerSentMessageOutput(Output delegate) {
181-
this.delegate = delegate;
182-
}
183-
184-
@Override
185-
public ByteBuffer asByteBuffer() {
186-
return delegate.asByteBuffer();
187-
}
188-
189-
@Override
190-
public String asString(@NonNull Charset charset) {
191-
return delegate.asString(charset);
192-
}
193-
194-
@Override
195-
public void accept(SneakyThrows.Consumer<ByteBuffer> consumer) {
196-
delegate.accept(consumer);
197-
}
198-
199-
@Override
200-
public int size() {
201-
return delegate.size();
202-
}
203-
204-
@Override
205-
public Output write(byte b) {
206-
return write(new byte[] {b}, 0, 1);
207-
}
208-
209-
@Override
210-
public Output write(byte[] source) {
211-
return write(ByteBuffer.wrap(source));
212-
}
213-
214-
@Override
215-
public Output write(byte[] source, int offset, int length) {
216-
var begin = offset;
217-
var len = offset + length;
218-
for (int i = offset; i < len; i++) {
219-
var ch = source[i];
220-
if (ch == '\n') {
221-
delegate.write(source, begin, i + 1);
222-
if (i < len - 1) {
223-
delegate.write(DATA);
224-
}
225-
begin = i + 1;
226-
}
227-
}
228-
if (begin < len) {
229-
delegate.write(source, begin, len);
230-
}
231-
return this;
232-
}
233-
234-
@Override
235-
public void send(Context ctx) {}
236-
237-
@Override
238-
public void close() throws IOException {}
239-
}
240173
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
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.ByteBufferOutputFactory;
47+
import io.jooby.output.DefaultOutputFactory;
4848
import io.jooby.output.OutputFactory;
4949
import io.jooby.problem.ProblemDetailsHandler;
5050
import io.jooby.value.ValueFactory;
@@ -172,7 +172,7 @@ public Stack executor(Executor executor) {
172172

173173
private ValueFactory valueFactory = new ValueFactory();
174174

175-
private OutputFactory outputFactory = new ByteBufferOutputFactory();
175+
private OutputFactory outputFactory = new DefaultOutputFactory();
176176

177177
public RouterImpl() {
178178
stack.addLast(new Stack(chi, null));

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().newChunkedOutput();
120+
var buffer = ctx.getOutputFactory().newCompositeOutput();
121121
buffer.write(c);
122122
data.accept(buffer::write);
123123
return buffer;

jooby/src/main/java/io/jooby/output/ByteBufferOutputImpl.java renamed to jooby/src/main/java/io/jooby/output/ByteBufferOut.java

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

8-
import java.io.IOException;
98
import java.nio.ByteBuffer;
109
import java.nio.charset.Charset;
11-
import java.nio.charset.StandardCharsets;
1210

1311
import edu.umd.cs.findbugs.annotations.NonNull;
1412
import io.jooby.Context;
1513
import io.jooby.SneakyThrows;
1614

17-
class ByteBufferOutputImpl implements Output {
15+
class ByteBufferOut implements Output {
1816
private static final int MAX_CAPACITY = Integer.MAX_VALUE;
1917

2018
private static final int CAPACITY_THRESHOLD = 1024 * 1024 * 4;
@@ -27,20 +25,20 @@ class ByteBufferOutputImpl implements Output {
2725

2826
private int writePosition;
2927

30-
public ByteBufferOutputImpl(boolean direct, int capacity) {
28+
public ByteBufferOut(boolean direct, int capacity) {
3129
this.buffer = allocate(capacity, direct);
3230
this.capacity = this.buffer.remaining();
3331
}
3432

35-
public ByteBufferOutputImpl(boolean direct) {
33+
public ByteBufferOut(boolean direct) {
3634
this(direct, BUFFER_SIZE);
3735
}
3836

39-
public ByteBufferOutputImpl(int bufferSize) {
37+
public ByteBufferOut(int bufferSize) {
4038
this(false, bufferSize);
4139
}
4240

43-
public ByteBufferOutputImpl() {
41+
public ByteBufferOut() {
4442
this(BUFFER_SIZE);
4543
}
4644

@@ -114,8 +112,9 @@ public Output write(@NonNull ByteBuffer source) {
114112
}
115113

116114
@Override
117-
public void close() throws IOException {
115+
public Output clear() {
118116
this.buffer.clear();
117+
return this;
119118
}
120119

121120
@Override
@@ -125,7 +124,14 @@ public void send(Context ctx) {
125124

126125
@Override
127126
public String toString() {
128-
return asString(StandardCharsets.UTF_8);
127+
return "readPosition="
128+
+ this.readPosition
129+
+ ", writePosition="
130+
+ this.writePosition
131+
+ ", size="
132+
+ this.size()
133+
+ ", capacity="
134+
+ this.capacity;
129135
}
130136

131137
/** Calculate the capacity of the buffer. */

jooby/src/main/java/io/jooby/output/WrappedOutput.java renamed to jooby/src/main/java/io/jooby/output/ByteBufferWrappedOutput.java

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

8-
import java.io.IOException;
98
import java.nio.ByteBuffer;
109
import java.nio.charset.Charset;
1110

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

16-
class WrappedOutput implements Output {
15+
class ByteBufferWrappedOutput implements Output {
1716

1817
private final ByteBuffer buffer;
1918

20-
public WrappedOutput(byte[] source, int offset, int length) {
19+
public ByteBufferWrappedOutput(byte[] source, int offset, int length) {
2120
this(ByteBuffer.wrap(source, offset, length));
2221
}
2322

24-
public WrappedOutput(byte[] source) {
23+
public ByteBufferWrappedOutput(byte[] source) {
2524
this(source, 0, source.length);
2625
}
2726

28-
public WrappedOutput(ByteBuffer buffer) {
27+
public ByteBufferWrappedOutput(ByteBuffer buffer) {
2928
this.buffer = buffer;
3029
}
3130

@@ -45,7 +44,10 @@ public Output write(byte[] source, int offset, int length) {
4544
}
4645

4746
@Override
48-
public void close() throws IOException {}
47+
public Output clear() {
48+
buffer.clear();
49+
return this;
50+
}
4951

5052
@Override
5153
public int size() {
@@ -67,6 +69,11 @@ public String asString(@NonNull Charset charset) {
6769
return charset.decode(asByteBuffer()).toString();
6870
}
6971

72+
@Override
73+
public String toString() {
74+
return "size=" + size();
75+
}
76+
7077
@Override
7178
public void send(Context ctx) {
7279
ctx.send(buffer);

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

jooby/src/main/java/io/jooby/output/ByteBufferChunkedOutput.java renamed to jooby/src/main/java/io/jooby/output/CompsiteByteBufferOutput.java

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

8-
import java.io.IOException;
98
import java.nio.ByteBuffer;
109
import java.nio.charset.Charset;
11-
import java.nio.charset.StandardCharsets;
1210
import java.util.ArrayList;
1311
import java.util.List;
1412

1513
import edu.umd.cs.findbugs.annotations.NonNull;
1614
import io.jooby.Context;
1715
import io.jooby.SneakyThrows;
1816

19-
class ByteBufferChunkedOutput implements ChunkedOutput {
17+
class CompsiteByteBufferOutput implements Output {
2018
private final List<ByteBuffer> chunks = new ArrayList<>();
2119
private int size = 0;
2220
private int bufferSizeHint = BUFFER_SIZE;
2321

24-
@Override
25-
public List<ByteBuffer> getChunks() {
26-
return chunks;
27-
}
28-
2922
@Override
3023
public int size() {
3124
return size;
@@ -50,8 +43,10 @@ public Output write(byte[] source, int offset, int length) {
5043
}
5144

5245
@Override
53-
public void close() throws IOException {
54-
// NOOP
46+
public Output clear() {
47+
chunks.forEach(ByteBuffer::clear);
48+
chunks.clear();
49+
return this;
5550
}
5651

5752
/**
@@ -86,7 +81,7 @@ public int bufferSizeHint() {
8681

8782
@Override
8883
public String toString() {
89-
return asString(StandardCharsets.UTF_8);
84+
return "chunks=" + chunks.size() + ", size=" + size;
9085
}
9186

9287
@Override

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

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

88
import java.nio.ByteBuffer;
99

10-
public class ByteBufferOutputFactory implements OutputFactory {
10+
public class DefaultOutputFactory implements OutputFactory {
1111
@Override
1212
public Output newBufferedOutput(int size) {
13-
return new ByteBufferOutputImpl(size);
13+
return new ByteBufferOut(size);
1414
}
1515

1616
@Override
17-
public ChunkedOutput newChunkedOutput() {
18-
return new ByteBufferChunkedOutput();
17+
public Output newCompositeOutput() {
18+
return new CompsiteByteBufferOutput();
1919
}
2020

2121
@Override
2222
public Output wrap(ByteBuffer buffer) {
23-
return new WrappedOutput(buffer);
23+
return new ByteBufferWrappedOutput(buffer);
2424
}
2525

2626
@Override
2727
public Output wrap(byte[] bytes) {
28-
return new WrappedOutput(bytes);
28+
return new ByteBufferWrappedOutput(bytes);
2929
}
3030

3131
@Override
3232
public Output wrap(byte[] bytes, int offset, int length) {
33-
return new WrappedOutput(bytes, offset, length);
33+
return new ByteBufferWrappedOutput(bytes, offset, length);
3434
}
3535
}

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

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

8-
import java.io.Closeable;
98
import java.io.OutputStream;
109
import java.io.Writer;
1110
import java.nio.ByteBuffer;
@@ -19,7 +18,7 @@
1918
import edu.umd.cs.findbugs.annotations.NonNull;
2019
import io.jooby.SneakyThrows;
2120

22-
public interface Output extends Closeable {
21+
public interface Output {
2322
/** Default buffer size: <code>4k</code>. */
2423
int BUFFER_SIZE = 4096;
2524

@@ -177,16 +176,20 @@ default Output write(@NonNull CharBuffer source, @NonNull Charset charset) {
177176
return this;
178177
}
179178

179+
void send(io.jooby.Context ctx);
180+
181+
Output clear();
182+
180183
static Output wrap(ByteBuffer buffer) {
181-
return new WrappedOutput(buffer);
184+
return new ByteBufferWrappedOutput(buffer);
182185
}
183186

184187
static Output wrap(byte[] bytes) {
185-
return new WrappedOutput(bytes);
188+
return new ByteBufferWrappedOutput(bytes);
186189
}
187190

188191
static Output wrap(byte[] bytes, int offset, int length) {
189-
return new WrappedOutput(bytes, offset, length);
192+
return new ByteBufferWrappedOutput(bytes, offset, length);
190193
}
191194

192195
static Output wrap(String value) {
@@ -196,6 +199,4 @@ static Output wrap(String value) {
196199
static Output wrap(String value, Charset charset) {
197200
return wrap(value.getBytes(charset));
198201
}
199-
200-
void send(io.jooby.Context ctx);
201202
}

0 commit comments

Comments
 (0)