Skip to content

Commit 758b3dd

Browse files
authored
fix: use fast calculation for totalRemaining number of bytes from multiple ByteBuffers (#2633)
1 parent 5d3ab83 commit 758b3dd

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/GapicUnbufferedReadableByteChannel.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
import com.google.storage.v2.ReadObjectResponse;
3131
import java.io.Closeable;
3232
import java.io.IOException;
33-
import java.nio.Buffer;
3433
import java.nio.ByteBuffer;
3534
import java.nio.channels.ClosedChannelException;
3635
import java.nio.channels.ScatteringByteChannel;
37-
import java.util.Arrays;
3836
import java.util.Iterator;
3937

4038
final class GapicUnbufferedReadableByteChannel
@@ -80,7 +78,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
8078
throw new ClosedChannelException();
8179
}
8280

83-
long totalBufferCapacity = Arrays.stream(dsts).mapToLong(Buffer::remaining).sum();
81+
long totalBufferCapacity = Buffers.totalRemaining(dsts, offset, length);
8482
ReadCursor c = new ReadCursor(blobOffset, blobOffset + totalBufferCapacity);
8583
while (c.hasRemaining()) {
8684
if (leftovers != null) {

google-cloud-storage/src/main/java/com/google/cloud/storage/RewindableContent.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.common.io.ByteStreams;
2323
import java.io.IOException;
2424
import java.io.OutputStream;
25-
import java.nio.Buffer;
2625
import java.nio.ByteBuffer;
2726
import java.nio.channels.Channels;
2827
import java.nio.channels.GatheringByteChannel;
@@ -173,7 +172,7 @@ private static final class ByteBufferContent extends RewindableContent {
173172
private ByteBufferContent(ByteBuffer[] buffers) {
174173
this.buffers = buffers;
175174
this.positions = Arrays.stream(buffers).mapToInt(Buffers::position).toArray();
176-
this.totalLength = Arrays.stream(buffers).mapToLong(Buffer::remaining).sum();
175+
this.totalLength = Buffers.totalRemaining(buffers, 0, buffers.length);
177176
this.dirty = false;
178177
}
179178

google-cloud-storage/src/main/java/com/google/cloud/storage/ThroughputSink.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818

1919
import com.google.common.base.MoreObjects;
2020
import java.io.IOException;
21-
import java.nio.Buffer;
2221
import java.nio.ByteBuffer;
2322
import java.nio.channels.GatheringByteChannel;
2423
import java.nio.channels.WritableByteChannel;
2524
import java.time.Clock;
2625
import java.time.Duration;
2726
import java.time.Instant;
28-
import java.util.Arrays;
2927
import java.util.Objects;
3028
import java.util.concurrent.locks.ReentrantLock;
3129
import java.util.logging.Logger;
@@ -262,7 +260,7 @@ public int write(ByteBuffer src) throws IOException {
262260
@Override
263261
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
264262
boolean exception = false;
265-
long available = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
263+
long available = Buffers.totalRemaining(srcs, offset, length);
266264
Instant begin = clock.instant();
267265
try {
268266
return delegate.write(srcs, offset, length);
@@ -271,7 +269,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
271269
throw e;
272270
} finally {
273271
Instant end = clock.instant();
274-
long remaining = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
272+
long remaining = Buffers.totalRemaining(srcs, offset, length);
275273
Record record = Record.of(available - remaining, begin, end, exception);
276274
sink.recordThroughput(record);
277275
}
@@ -280,7 +278,7 @@ public long write(ByteBuffer[] srcs, int offset, int length) throws IOException
280278
@Override
281279
public long write(ByteBuffer[] srcs) throws IOException {
282280
boolean exception = false;
283-
long available = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
281+
long available = Buffers.totalRemaining(srcs, 0, srcs.length);
284282
Instant begin = clock.instant();
285283
try {
286284
return delegate.write(srcs);
@@ -289,7 +287,7 @@ public long write(ByteBuffer[] srcs) throws IOException {
289287
throw e;
290288
} finally {
291289
Instant end = clock.instant();
292-
long remaining = Arrays.stream(srcs).mapToLong(Buffer::remaining).sum();
290+
long remaining = Buffers.totalRemaining(srcs, 0, srcs.length);
293291
Record record = Record.of(available - remaining, begin, end, exception);
294292
sink.recordThroughput(record);
295293
}

0 commit comments

Comments
 (0)