3232import java .util .concurrent .Callable ;
3333
3434/**
35- * Google Storage blob copy writer. This class holds the result of a copy request. If source and
35+ * Google Storage blob copy writer. A {@code CopyWriter} object allows to copy both blob's data and
36+ * information. To override source blob's information supply a {@code BlobInfo} to the
37+ * {@code CopyRequest} using either
38+ * {@link Storage.CopyRequest.Builder#target(BlobInfo, Storage.BlobTargetOption...)} or
39+ * {@link Storage.CopyRequest.Builder#target(BlobInfo, Iterable)}.
40+ *
41+ * <p>This class holds the result of a copy request. If source and
3642 * destination blobs share the same location and storage class the copy is completed in one RPC call
3743 * otherwise one or more {@link #copyChunk} calls are necessary to complete the copy. In addition,
3844 * {@link CopyWriter#result()} can be used to automatically complete the copy and return information
@@ -65,11 +71,11 @@ public class CopyWriter implements Restorable<CopyWriter> {
6571 *
6672 * @throws StorageException upon failure
6773 */
68- public BlobInfo result () {
74+ public Blob result () {
6975 while (!isDone ()) {
7076 copyChunk ();
7177 }
72- return BlobInfo .fromPb (rewriteResponse .result );
78+ return Blob .fromPb (serviceOptions . service (), rewriteResponse .result );
7379 }
7480
7581 /**
@@ -120,8 +126,10 @@ public RestorableState<CopyWriter> capture() {
120126 serviceOptions ,
121127 BlobId .fromPb (rewriteResponse .rewriteRequest .source ),
122128 rewriteResponse .rewriteRequest .sourceOptions ,
129+ rewriteResponse .rewriteRequest .overrideInfo ,
123130 BlobInfo .fromPb (rewriteResponse .rewriteRequest .target ),
124131 rewriteResponse .rewriteRequest .targetOptions )
132+ .result (rewriteResponse .result != null ? BlobInfo .fromPb (rewriteResponse .result ) : null )
125133 .blobSize (blobSize ())
126134 .isDone (isDone ())
127135 .megabytesCopiedPerChunk (rewriteResponse .rewriteRequest .megabytesRewrittenPerCall )
@@ -132,11 +140,12 @@ public RestorableState<CopyWriter> capture() {
132140
133141 static class StateImpl implements RestorableState <CopyWriter >, Serializable {
134142
135- private static final long serialVersionUID = 8279287678903181701L ;
143+ private static final long serialVersionUID = 1693964441435822700L ;
136144
137145 private final StorageOptions serviceOptions ;
138146 private final BlobId source ;
139147 private final Map <StorageRpc .Option , ?> sourceOptions ;
148+ private final boolean overrideInfo ;
140149 private final BlobInfo target ;
141150 private final Map <StorageRpc .Option , ?> targetOptions ;
142151 private final BlobInfo result ;
@@ -150,6 +159,7 @@ static class StateImpl implements RestorableState<CopyWriter>, Serializable {
150159 this .serviceOptions = builder .serviceOptions ;
151160 this .source = builder .source ;
152161 this .sourceOptions = builder .sourceOptions ;
162+ this .overrideInfo = builder .overrideInfo ;
153163 this .target = builder .target ;
154164 this .targetOptions = builder .targetOptions ;
155165 this .result = builder .result ;
@@ -165,6 +175,7 @@ static class Builder {
165175 private final StorageOptions serviceOptions ;
166176 private final BlobId source ;
167177 private final Map <StorageRpc .Option , ?> sourceOptions ;
178+ private final boolean overrideInfo ;
168179 private final BlobInfo target ;
169180 private final Map <StorageRpc .Option , ?> targetOptions ;
170181 private BlobInfo result ;
@@ -175,11 +186,12 @@ static class Builder {
175186 private Long megabytesCopiedPerChunk ;
176187
177188 private Builder (StorageOptions options , BlobId source ,
178- Map <StorageRpc .Option , ?> sourceOptions ,
179- BlobInfo target , Map <StorageRpc .Option , ?> targetOptions ) {
189+ Map <StorageRpc .Option , ?> sourceOptions , boolean overrideInfo , BlobInfo target ,
190+ Map <StorageRpc .Option , ?> targetOptions ) {
180191 this .serviceOptions = options ;
181192 this .source = source ;
182193 this .sourceOptions = sourceOptions ;
194+ this .overrideInfo = overrideInfo ;
183195 this .target = target ;
184196 this .targetOptions = targetOptions ;
185197 }
@@ -220,15 +232,15 @@ RestorableState<CopyWriter> build() {
220232 }
221233
222234 static Builder builder (StorageOptions options , BlobId source ,
223- Map <StorageRpc .Option , ?> sourceOptions , BlobInfo target ,
235+ Map <StorageRpc .Option , ?> sourceOptions , boolean overrideInfo , BlobInfo target ,
224236 Map <StorageRpc .Option , ?> targetOptions ) {
225- return new Builder (options , source , sourceOptions , target , targetOptions );
237+ return new Builder (options , source , sourceOptions , overrideInfo , target , targetOptions );
226238 }
227239
228240 @ Override
229241 public CopyWriter restore () {
230- RewriteRequest rewriteRequest = new RewriteRequest (
231- source . toPb (), sourceOptions , target .toPb (), targetOptions , megabytesCopiedPerChunk );
242+ RewriteRequest rewriteRequest = new RewriteRequest (source . toPb (), sourceOptions ,
243+ overrideInfo , target .toPb (), targetOptions , megabytesCopiedPerChunk );
232244 RewriteResponse rewriteResponse = new RewriteResponse (rewriteRequest ,
233245 result != null ? result .toPb () : null , blobSize , isDone , rewriteToken ,
234246 totalBytesCopied );
@@ -237,8 +249,9 @@ public CopyWriter restore() {
237249
238250 @ Override
239251 public int hashCode () {
240- return Objects .hash (serviceOptions , source , sourceOptions , target , targetOptions , result ,
241- blobSize , isDone , megabytesCopiedPerChunk , rewriteToken , totalBytesCopied );
252+ return Objects .hash (serviceOptions , source , sourceOptions , overrideInfo , target ,
253+ targetOptions , result , blobSize , isDone , megabytesCopiedPerChunk , rewriteToken ,
254+ totalBytesCopied );
242255 }
243256
244257 @ Override
@@ -253,6 +266,7 @@ public boolean equals(Object obj) {
253266 return Objects .equals (this .serviceOptions , other .serviceOptions )
254267 && Objects .equals (this .source , other .source )
255268 && Objects .equals (this .sourceOptions , other .sourceOptions )
269+ && Objects .equals (this .overrideInfo , other .overrideInfo )
256270 && Objects .equals (this .target , other .target )
257271 && Objects .equals (this .targetOptions , other .targetOptions )
258272 && Objects .equals (this .result , other .result )
@@ -267,10 +281,14 @@ public boolean equals(Object obj) {
267281 public String toString () {
268282 return MoreObjects .toStringHelper (this )
269283 .add ("source" , source )
284+ .add ("overrideInfo" , overrideInfo )
270285 .add ("target" , target )
271- .add ("isDone" , isDone )
272- .add ("totalBytesRewritten" , totalBytesCopied )
286+ .add ("result" , result )
273287 .add ("blobSize" , blobSize )
288+ .add ("isDone" , isDone )
289+ .add ("rewriteToken" , rewriteToken )
290+ .add ("totalBytesCopied" , totalBytesCopied )
291+ .add ("megabytesCopiedPerChunk" , megabytesCopiedPerChunk )
274292 .toString ();
275293 }
276294 }
0 commit comments