diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/BidiBlobWriteSessionConfig.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/BidiBlobWriteSessionConfig.java index 06dd7e7a02..390c5abbc0 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/BidiBlobWriteSessionConfig.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/BidiBlobWriteSessionConfig.java @@ -17,19 +17,31 @@ package com.google.cloud.storage; import com.google.api.core.ApiFuture; -import com.google.api.core.ApiFutures; import com.google.api.core.BetaApi; import com.google.api.core.InternalApi; import com.google.api.gax.grpc.GrpcCallContext; +import com.google.cloud.storage.DefaultBlobWriteSessionConfig.DecoratedWritableByteChannelSession; +import com.google.cloud.storage.DefaultBlobWriteSessionConfig.LazySession; +import com.google.cloud.storage.TransportCompatibility.Transport; import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.MoreExecutors; import com.google.storage.v2.BidiWriteObjectRequest; import com.google.storage.v2.BidiWriteObjectResponse; import java.io.IOException; -import java.nio.channels.WritableByteChannel; import java.time.Clock; +import javax.annotation.concurrent.Immutable; -public class BidiBlobWriteSessionConfig extends BlobWriteSessionConfig +/** + * Perform a resumable upload, uploading at most {@code bufferSize} bytes each flush. + * + *

Configuration of buffer size can be performed via {@link + * BidiBlobWriteSessionConfig#withBufferSize(int)}. + * + * @since 2.34.0 This new api is in preview and is subject to breaking changes. + */ +@Immutable +@BetaApi +@TransportCompatibility({Transport.GRPC}) +public final class BidiBlobWriteSessionConfig extends BlobWriteSessionConfig implements BlobWriteSessionConfig.GrpcCompatible { private static final long serialVersionUID = -903533790705476197L; @@ -54,7 +66,7 @@ public int getBufferSize() { @Override WriterFactory createFactory(Clock clock) throws IOException { - return new Factory(ByteSizeConstants._16MiB); + return new Factory(bufferSize); } @InternalApi @@ -124,57 +136,4 @@ public BidiBlobWriteSessionConfig withBufferSize(int bufferSize) { ByteSizeConstants._256KiB); return new BidiBlobWriteSessionConfig(bufferSize); } - - private static final class DecoratedWritableByteChannelSession - implements WritableByteChannelSession { - - private final WritableByteChannelSession delegate; - private final Conversions.Decoder decoder; - - private DecoratedWritableByteChannelSession( - WritableByteChannelSession delegate, Conversions.Decoder decoder) { - this.delegate = delegate; - this.decoder = decoder; - } - - @Override - public WBC open() { - try { - return WritableByteChannelSession.super.open(); - } catch (Exception e) { - throw StorageException.coalesce(e); - } - } - - @Override - public ApiFuture openAsync() { - return delegate.openAsync(); - } - - @Override - public ApiFuture getResult() { - return ApiFutures.transform( - delegate.getResult(), decoder::decode, MoreExecutors.directExecutor()); - } - } - - private static final class LazySession - implements WritableByteChannelSession< - BufferedWritableByteChannelSession.BufferedWritableByteChannel, R> { - private final LazyWriteChannel lazy; - - private LazySession(LazyWriteChannel lazy) { - this.lazy = lazy; - } - - @Override - public ApiFuture openAsync() { - return lazy.getSession().openAsync(); - } - - @Override - public ApiFuture getResult() { - return lazy.getSession().getResult(); - } - } } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultBlobWriteSessionConfig.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultBlobWriteSessionConfig.java index 090f929e7e..3a4dd2be23 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultBlobWriteSessionConfig.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/DefaultBlobWriteSessionConfig.java @@ -153,13 +153,13 @@ public WritableByteChannelSession writeSession( } } - private static final class DecoratedWritableByteChannelSession + static final class DecoratedWritableByteChannelSession implements WritableByteChannelSession { private final WritableByteChannelSession delegate; private final Decoder decoder; - private DecoratedWritableByteChannelSession( + DecoratedWritableByteChannelSession( WritableByteChannelSession delegate, Decoder decoder) { this.delegate = delegate; this.decoder = decoder; @@ -186,11 +186,11 @@ public ApiFuture getResult() { } } - private static final class LazySession + static final class LazySession implements WritableByteChannelSession { private final LazyWriteChannel lazy; - private LazySession(LazyWriteChannel lazy) { + LazySession(LazyWriteChannel lazy) { this.lazy = lazy; }