From 475c5a0cf923a084dd5f7f04af1518f78384c02b Mon Sep 17 00:00:00 2001 From: Andreas Herrmann Date: Tue, 24 Jan 2023 15:32:40 +0100 Subject: [PATCH] Remove skipBytes in favor of offset Addressing review comment https://github.com/bazelbuild/bazel/pull/14258#discussion_r1071370986 --- .../lib/remote/http/HttpDownloadHandler.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/http/HttpDownloadHandler.java b/src/main/java/com/google/devtools/build/lib/remote/http/HttpDownloadHandler.java index 35bea78d11ca37..89e78e11df2730 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/http/HttpDownloadHandler.java +++ b/src/main/java/com/google/devtools/build/lib/remote/http/HttpDownloadHandler.java @@ -47,8 +47,6 @@ final class HttpDownloadHandler extends AbstractHttpHandler { private String path; /** the offset at which to download */ private long offset; - /** the bytes to skip in a full or chunked response */ - private OptionalInt skipBytes; public HttpDownloadHandler( Credentials credentials, ImmutableList> extraHttpHeaders) { @@ -91,14 +89,6 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex if (contentLengthSet) { contentLength = HttpUtil.getContentLength(response); } - if (offset != 0) { - // We are in a retried download and received a full response. - // We need to skip `offset` bytes of the response to continue writing from the offset. - if (!skipBytes.isPresent()) { - // This is the first chunk, or the full response. - skipBytes = OptionalInt.of((int)offset); - } - } downloadSucceeded = response.status().equals(HttpResponseStatus.OK); if (!downloadSucceeded) { out = new ByteArrayOutputStream(); @@ -111,13 +101,13 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Ex ByteBuf content = ((HttpContent) msg).content(); int readableBytes = content.readableBytes(); - if (skipBytes.isPresent() && skipBytes.getAsInt() > 0) { - int skipNow = skipBytes.getAsInt(); + if (offset > 0) { + int skipNow = (int)offset; if (skipNow >= readableBytes) { skipNow = readableBytes; } content.readerIndex(content.readerIndex() + skipNow); - skipBytes = OptionalInt.of(skipBytes.getAsInt() - skipNow); + offset -= skipNow; readableBytes = readableBytes - skipNow; } content.readBytes(out, readableBytes); @@ -153,7 +143,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) out = cmd.out(); path = constructPath(cmd.uri(), cmd.digest().getHash(), cmd.casDownload()); offset = cmd.offset(); - skipBytes = OptionalInt.empty(); HttpRequest request = buildRequest(path, constructHost(cmd.uri())); addCredentialHeaders(request, cmd.uri()); addExtraRemoteHeaders(request);