|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +package com.google.api.gax.rpc; |
| 31 | + |
| 32 | +import static com.google.common.base.Preconditions.checkArgument; |
| 33 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | + |
| 35 | +import com.google.common.io.ByteStreams; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.InputStream; |
| 38 | +import org.jspecify.annotations.NullMarked; |
| 39 | +import org.jspecify.annotations.Nullable; |
| 40 | + |
| 41 | +/** |
| 42 | + * Manages a single-chunk buffer over an {@link InputStream} for resumable uploads. |
| 43 | + * |
| 44 | + * <p>The buffer holds at most one chunk of data in a reused backing array. It supports forward |
| 45 | + * compaction and topping up upon recovery realignment, and enforces the boundary condition that |
| 46 | + * requests to rewind before the buffer's base offset fail with an unrecoverable {@link |
| 47 | + * FailedPreconditionException}. |
| 48 | + */ |
| 49 | +@NullMarked |
| 50 | +final class RewindableStreamBuffer { |
| 51 | + |
| 52 | + private final InputStream inputStream; |
| 53 | + private final int chunkSize; |
| 54 | + private final String uploadUrl; |
| 55 | + private final byte[] buffer; |
| 56 | + |
| 57 | + private long bufferBaseOffset; |
| 58 | + private int payloadLength; |
| 59 | + private boolean isFinal; |
| 60 | + private boolean streamExhausted; |
| 61 | + |
| 62 | + RewindableStreamBuffer(InputStream inputStream, int chunkSize, String uploadUrl) { |
| 63 | + this.inputStream = checkNotNull(inputStream, "inputStream must not be null"); |
| 64 | + checkArgument(chunkSize > 0, "chunkSize must be > 0"); |
| 65 | + this.chunkSize = chunkSize; |
| 66 | + this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null"); |
| 67 | + this.buffer = new byte[chunkSize]; |
| 68 | + this.bufferBaseOffset = 0L; |
| 69 | + this.payloadLength = 0; |
| 70 | + this.isFinal = false; |
| 71 | + this.streamExhausted = false; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Advances the buffer from the stream starting at {@code targetOffset}, reading up to chunk size. |
| 76 | + * |
| 77 | + * @param targetOffset the absolute stream offset corresponding to the start of this chunk |
| 78 | + * @throws IOException if reading from the stream fails |
| 79 | + */ |
| 80 | + void fill(long targetOffset) throws IOException { |
| 81 | + this.bufferBaseOffset = targetOffset; |
| 82 | + this.payloadLength = ByteStreams.read(inputStream, buffer, 0, chunkSize); |
| 83 | + this.isFinal = (payloadLength < chunkSize); |
| 84 | + if (this.isFinal) { |
| 85 | + this.streamExhausted = true; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Realigns the buffer window to {@code committedOffset}. |
| 91 | + * |
| 92 | + * <p>Compacts forward within the existing buffer to discard already-committed bytes, and then |
| 93 | + * tops up the buffer to capacity from the underlying stream. |
| 94 | + * |
| 95 | + * @param committedOffset the server's committed byte offset |
| 96 | + * @throws FailedPreconditionException if {@code committedOffset} is below the buffer's base |
| 97 | + * offset or beyond the current buffer window |
| 98 | + * @throws IOException if reading from the stream fails |
| 99 | + */ |
| 100 | + void realignTo(long committedOffset) throws IOException { |
| 101 | + if (committedOffset < bufferBaseOffset) { |
| 102 | + throw protocolViolation( |
| 103 | + String.format( |
| 104 | + "Server committed offset %d is below buffer base offset %d for upload URL %s; cannot" |
| 105 | + + " rewind stream before buffer base", |
| 106 | + committedOffset, bufferBaseOffset, uploadUrl)); |
| 107 | + } |
| 108 | + |
| 109 | + if (committedOffset > bufferBaseOffset + payloadLength) { |
| 110 | + throw protocolViolation( |
| 111 | + String.format( |
| 112 | + "Server committed offset %d is beyond current buffer window [%d, %d] for upload URL" |
| 113 | + + " %s", |
| 114 | + committedOffset, bufferBaseOffset, bufferBaseOffset + payloadLength, uploadUrl)); |
| 115 | + } |
| 116 | + |
| 117 | + int committedWithinBuffer = (int) (committedOffset - bufferBaseOffset); |
| 118 | + int remainingBytes = payloadLength - committedWithinBuffer; |
| 119 | + |
| 120 | + if (remainingBytes > 0 && committedWithinBuffer > 0) { |
| 121 | + System.arraycopy(buffer, committedWithinBuffer, buffer, 0, remainingBytes); |
| 122 | + } |
| 123 | + |
| 124 | + this.bufferBaseOffset = committedOffset; |
| 125 | + this.payloadLength = remainingBytes; |
| 126 | + |
| 127 | + if (!streamExhausted && payloadLength < chunkSize) { |
| 128 | + int space = chunkSize - payloadLength; |
| 129 | + int additionalRead = ByteStreams.read(inputStream, buffer, payloadLength, space); |
| 130 | + payloadLength += additionalRead; |
| 131 | + if (additionalRead < space) { |
| 132 | + streamExhausted = true; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + this.isFinal = streamExhausted; |
| 137 | + } |
| 138 | + |
| 139 | + byte[] getBuffer() { |
| 140 | + return buffer; |
| 141 | + } |
| 142 | + |
| 143 | + int getPayloadLength() { |
| 144 | + return payloadLength; |
| 145 | + } |
| 146 | + |
| 147 | + long getBufferBaseOffset() { |
| 148 | + return bufferBaseOffset; |
| 149 | + } |
| 150 | + |
| 151 | + boolean isFinal() { |
| 152 | + return isFinal; |
| 153 | + } |
| 154 | + |
| 155 | + boolean isEmpty() { |
| 156 | + return payloadLength == 0; |
| 157 | + } |
| 158 | + |
| 159 | + private static final StatusCode FAILED_PRECONDITION_STATUS_CODE = |
| 160 | + new StatusCode() { |
| 161 | + @Override |
| 162 | + public StatusCode.Code getCode() { |
| 163 | + return StatusCode.Code.FAILED_PRECONDITION; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public @Nullable Object getTransportCode() { |
| 168 | + return null; |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + private static FailedPreconditionException protocolViolation(String message) { |
| 173 | + return new FailedPreconditionException(message, null, FAILED_PRECONDITION_STATUS_CODE, false); |
| 174 | + } |
| 175 | +} |
0 commit comments