3434/**
3535 * Google Storage blob copy writer. This class holds the result of a copy request.
3636 * If source and destination blobs do not share the same location or storage class more than one
37- * RPC request is needed to copy the blob. When this is the case {@link #copyChunk()} can be used
38- * to copy to destination other chunks of the source blob .
37+ * RPC request is needed to copy the blob otherwise one or more {@link #copyChunk()} calls are
38+ * necessary to complete the copy .
3939 *
4040 * @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/rewrite">Rewrite</a>
4141 */
@@ -52,31 +52,36 @@ public class CopyWriter implements Restorable<CopyWriter> {
5252 }
5353
5454 /**
55- * Returns the updated information for the just written blob when {@link #isDone} is {@code true}.
56- * Returns {@code null} otherwise.
55+ * Returns the updated information for the just written blob. This method might block and issue
56+ * several RPC requests to complete blob copy.
57+ *
58+ * @throws StorageException upon failure
5759 */
5860 public BlobInfo result () {
59- return rewriteResponse .result != null ? BlobInfo .fromPb (rewriteResponse .result ) : null ;
61+ while (!isDone ()) {
62+ copyChunk ();
63+ }
64+ return BlobInfo .fromPb (rewriteResponse .result );
6065 }
6166
6267 /**
6368 * Size of the blob being copied.
6469 */
65- public Long blobSize () {
70+ public long blobSize () {
6671 return rewriteResponse .blobSize ;
6772 }
6873
6974 /**
7075 * Returns {@code true} of blob rewrite finished, {@code false} otherwise.
7176 */
72- public Boolean isDone () {
77+ public boolean isDone () {
7378 return rewriteResponse .isDone ;
7479 }
7580
7681 /**
7782 * Returns the number of bytes copied.
7883 */
79- public Long totalBytesCopied () {
84+ public long totalBytesCopied () {
8085 return rewriteResponse .totalBytesRewritten ;
8186 }
8287
@@ -111,7 +116,7 @@ public RestorableState<CopyWriter> capture() {
111116 rewriteResponse .rewriteRequest .targetOptions )
112117 .blobSize (blobSize ())
113118 .isDone (isDone ())
114- .megabytesRewrittenPerCall (rewriteResponse .rewriteRequest .megabytesRewrittenPerCall )
119+ .megabytesCopiedPerChunk (rewriteResponse .rewriteRequest .megabytesRewrittenPerCall )
115120 .rewriteToken (rewriteResponse .rewriteToken )
116121 .totalBytesRewritten (totalBytesCopied ())
117122 .build ();
@@ -127,11 +132,11 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
127132 private final BlobInfo target ;
128133 private final Map <StorageRpc .Option , ?> targetOptions ;
129134 private final BlobInfo result ;
130- private final Long blobSize ;
131- private final Boolean isDone ;
135+ private final long blobSize ;
136+ private final boolean isDone ;
132137 private final String rewriteToken ;
133- private final Long totalBytesCopied ;
134- private final Long megabytesRewrittenPerCall ;
138+ private final long totalBytesCopied ;
139+ private final Long megabytesCopiedPerChunk ;
135140
136141 StateImpl (Builder builder ) {
137142 this .serviceOptions = builder .serviceOptions ;
@@ -144,7 +149,7 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
144149 this .isDone = builder .isDone ;
145150 this .rewriteToken = builder .rewriteToken ;
146151 this .totalBytesCopied = builder .totalBytesCopied ;
147- this .megabytesRewrittenPerCall = builder .megabytesRewrittenPerCall ;
152+ this .megabytesCopiedPerChunk = builder .megabytesCopiedPerChunk ;
148153 }
149154
150155 static class Builder {
@@ -155,11 +160,11 @@ static class Builder {
155160 private final BlobInfo target ;
156161 private final Map <StorageRpc .Option , ?> targetOptions ;
157162 private BlobInfo result ;
158- private Long blobSize ;
159- private Boolean isDone ;
163+ private long blobSize ;
164+ private boolean isDone ;
160165 private String rewriteToken ;
161- private Long totalBytesCopied ;
162- private Long megabytesRewrittenPerCall ;
166+ private long totalBytesCopied ;
167+ private Long megabytesCopiedPerChunk ;
163168
164169 private Builder (StorageOptions options , BlobId source ,
165170 Map <StorageRpc .Option , ?> sourceOptions ,
@@ -176,12 +181,12 @@ Builder result(BlobInfo result) {
176181 return this ;
177182 }
178183
179- Builder blobSize (Long blobSize ) {
184+ Builder blobSize (long blobSize ) {
180185 this .blobSize = blobSize ;
181186 return this ;
182187 }
183188
184- Builder isDone (Boolean isDone ) {
189+ Builder isDone (boolean isDone ) {
185190 this .isDone = isDone ;
186191 return this ;
187192 }
@@ -191,13 +196,13 @@ Builder rewriteToken(String rewriteToken) {
191196 return this ;
192197 }
193198
194- Builder totalBytesRewritten (Long totalBytesRewritten ) {
199+ Builder totalBytesRewritten (long totalBytesRewritten ) {
195200 this .totalBytesCopied = totalBytesRewritten ;
196201 return this ;
197202 }
198203
199- Builder megabytesRewrittenPerCall (Long megabytesRewrittenPerCall ) {
200- this .megabytesRewrittenPerCall = megabytesRewrittenPerCall ;
204+ Builder megabytesCopiedPerChunk (Long megabytesCopiedPerChunk ) {
205+ this .megabytesCopiedPerChunk = megabytesCopiedPerChunk ;
201206 return this ;
202207 }
203208
@@ -215,7 +220,7 @@ static Builder builder(StorageOptions options, BlobId source,
215220 @ Override
216221 public CopyWriter restore () {
217222 RewriteRequest rewriteRequest = new RewriteRequest (
218- source .toPb (), sourceOptions , target .toPb (), targetOptions , megabytesRewrittenPerCall );
223+ source .toPb (), sourceOptions , target .toPb (), targetOptions , megabytesCopiedPerChunk );
219224 RewriteResponse rewriteResponse = new RewriteResponse (rewriteRequest ,
220225 result != null ? result .toPb () : null , blobSize , isDone , rewriteToken ,
221226 totalBytesCopied );
@@ -225,7 +230,7 @@ public CopyWriter restore() {
225230 @ Override
226231 public int hashCode () {
227232 return Objects .hash (serviceOptions , source , sourceOptions , target , targetOptions , result ,
228- blobSize , isDone , megabytesRewrittenPerCall , rewriteToken , totalBytesCopied );
233+ blobSize , isDone , megabytesCopiedPerChunk , rewriteToken , totalBytesCopied );
229234 }
230235
231236 @ Override
@@ -244,10 +249,10 @@ public boolean equals(Object obj) {
244249 && Objects .equals (this .targetOptions , other .targetOptions )
245250 && Objects .equals (this .result , other .result )
246251 && Objects .equals (this .rewriteToken , other .rewriteToken )
247- && Objects .equals (this .blobSize , other .blobSize )
248- && Objects . equals ( this .isDone , other .isDone )
249- && Objects . equals ( this .megabytesRewrittenPerCall , other .megabytesRewrittenPerCall )
250- && Objects . equals ( this .totalBytesCopied , other .totalBytesCopied ) ;
252+ && Objects .equals (this .megabytesCopiedPerChunk , other .megabytesCopiedPerChunk )
253+ && this .blobSize == other .blobSize
254+ && this .isDone == other .isDone
255+ && this .totalBytesCopied == other .totalBytesCopied ;
251256 }
252257
253258 @ Override
0 commit comments