Skip to content

Commit 3c09531

Browse files
committed
more refactor on byterange
1 parent 1ad9e32 commit 3c09531

5 files changed

Lines changed: 23 additions & 115 deletions

File tree

jooby/src/main/java/io/jooby/ByteRange.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package io.jooby;
22

3+
import org.apache.commons.io.input.BoundedInputStream;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
38
public class ByteRange {
49
private static final String BYTES_EQ = "bytes=";
510

@@ -62,6 +67,17 @@ public ByteRange apply(Context ctx) {
6267
return this;
6368
}
6469

70+
public InputStream apply(InputStream input) throws IOException {
71+
if (statusCode == StatusCode.OK) {
72+
return input;
73+
}
74+
if (statusCode == StatusCode.PARTIAL_CONTENT) {
75+
input.skip(start);
76+
return new BoundedInputStream(input, end);
77+
}
78+
throw new Err(statusCode, value);
79+
}
80+
6581
@Override public String toString() {
6682
return value;
6783
}

modules/server/jooby-jetty/src/main/java/io/jooby/internal/jetty/ByteRangeInputStream.java

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

modules/server/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,9 @@ private Context sendStreamInternal(@Nonnull InputStream in) {
336336
long len = response.getContentLength();
337337
InputStream stream;
338338
if (len > 0) {
339-
ByteRange range = ByteRange.parse(request.getHeader(HttpHeader.RANGE.asString()), len)
340-
.apply(this);
341-
stream = new ByteRangeInputStream(in, range);
339+
stream = ByteRange.parse(request.getHeader(HttpHeader.RANGE.asString()), len)
340+
.apply(this)
341+
.apply(in);
342342
} else {
343343
response.setHeader(HttpHeader.TRANSFER_ENCODING, HttpHeaderValue.CHUNKED.asString());
344344
stream = in;

modules/server/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,9 @@ public NettyContext(ChannelHandlerContext ctx, HttpRequest req, Router router, S
332332
try {
333333
prepareChunked();
334334
long len = responseLength();
335-
ChunkedInput chunkedStream;
336335
ByteRange range = ByteRange.parse(req.headers().get(RANGE), len)
337336
.apply(this);
338-
if (range.isPartial()) {
339-
range.apply(this);
340-
in.skip(range.getStart());
341-
chunkedStream = new ChunkedLimitedStream(in, bufferSize, range.getEnd());
342-
} else {
343-
chunkedStream = new ChunkedStream(in, bufferSize);
344-
}
337+
ChunkedStream chunkedStream = new ChunkedStream(range.apply(in), bufferSize);
345338

346339
DefaultHttpResponse rsp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status, setHeaders);
347340
responseStarted = true;

modules/server/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,9 @@ public Context setContentType(@Nonnull MediaType contentType, @Nullable Charset
274274
try {
275275
ifSetChunked();
276276
long len = exchange.getResponseContentLength();
277-
if (len > 0) {
278-
ByteRange range = ByteRange.parse(exchange.getRequestHeaders().getFirst(RANGE), len)
279-
.apply(this);
280-
in.skip(range.getStart());
281-
len = range.getEnd();
282-
} else {
283-
len = -1;
284-
}
285-
new UtowChunkedStream(len).send(Channels.newChannel(in), exchange, this);
277+
ByteRange range = ByteRange.parse(exchange.getRequestHeaders().getFirst(RANGE), len)
278+
.apply(this);
279+
new UtowChunkedStream(len).send(Channels.newChannel(range.apply(in)), exchange, this);
286280
return this;
287281
} catch (IOException x) {
288282
throw Throwing.sneakyThrow(x);

0 commit comments

Comments
 (0)