28
28
import com .google .cloud .storage .spi .v1 .StorageRpc ;
29
29
import com .google .common .base .MoreObjects ;
30
30
import java .io .IOException ;
31
- import java .io .ObjectInputStream ;
32
31
import java .io .Serializable ;
33
32
import java .nio .ByteBuffer ;
34
33
import java .util .Map ;
@@ -38,7 +37,7 @@ final class BlobReadChannelV2 implements StorageReadChannel {
38
37
39
38
private final StorageObject storageObject ;
40
39
private final Map <StorageRpc .Option , ?> opts ;
41
- private final ClientStuff clientStuff ;
40
+ private final BlobReadChannelContext blobReadChannelContext ;
42
41
43
42
private LazyReadChannel <StorageObject > lazyReadChannel ;
44
43
private StorageObject resolvedObject ;
@@ -48,10 +47,12 @@ final class BlobReadChannelV2 implements StorageReadChannel {
48
47
private BufferHandle bufferHandle ;
49
48
50
49
BlobReadChannelV2 (
51
- StorageObject storageObject , Map <StorageRpc .Option , ?> opts , ClientStuff clientStuff ) {
50
+ StorageObject storageObject ,
51
+ Map <StorageRpc .Option , ?> opts ,
52
+ BlobReadChannelContext blobReadChannelContext ) {
52
53
this .storageObject = storageObject ;
53
54
this .opts = opts ;
54
- this .clientStuff = clientStuff ;
55
+ this .blobReadChannelContext = blobReadChannelContext ;
55
56
this .byteRangeSpec = ByteRangeSpec .nullRange ();
56
57
}
57
58
@@ -116,7 +117,8 @@ public synchronized int read(ByteBuffer dst) throws IOException {
116
117
@ Override
117
118
public RestorableState <ReadChannel > capture () {
118
119
ApiaryReadRequest apiaryReadRequest = getApiaryReadRequest ();
119
- return new BlobReadChannelV2State (apiaryReadRequest , clientStuff , chunkSize );
120
+ return new BlobReadChannelV2State (
121
+ apiaryReadRequest , blobReadChannelContext .getStorageOptions (), chunkSize );
120
122
}
121
123
122
124
private void maybeResetChannel (boolean umallocBuffer ) throws IOException {
@@ -147,7 +149,7 @@ private LazyReadChannel<StorageObject> newLazyReadChannel() {
147
149
}
148
150
return ResumableMedia .http ()
149
151
.read ()
150
- .byteChannel (clientStuff )
152
+ .byteChannel (blobReadChannelContext )
151
153
.setCallback (this ::setResolvedObject )
152
154
.buffered (bufferHandle )
153
155
.setApiaryReadRequest (getApiaryReadRequest ())
@@ -170,21 +172,22 @@ static class BlobReadChannelV2State implements RestorableState<ReadChannel>, Ser
170
172
private static final long serialVersionUID = -7595661593080505431L ;
171
173
172
174
private final ApiaryReadRequest request ;
173
- private final ClientStuff clientStuff ;
175
+ private final HttpStorageOptions options ;
174
176
175
177
private final Integer chunkSize ;
176
178
177
179
private BlobReadChannelV2State (
178
- ApiaryReadRequest request , ClientStuff clientStuff , Integer chunkSize ) {
180
+ ApiaryReadRequest request , HttpStorageOptions options , Integer chunkSize ) {
179
181
this .request = request ;
180
- this .clientStuff = clientStuff ;
182
+ this .options = options ;
181
183
this .chunkSize = chunkSize ;
182
184
}
183
185
184
186
@ Override
185
187
public ReadChannel restore () {
186
188
BlobReadChannelV2 channel =
187
- new BlobReadChannelV2 (request .getObject (), request .getOptions (), clientStuff );
189
+ new BlobReadChannelV2 (
190
+ request .getObject (), request .getOptions (), BlobReadChannelContext .from (options ));
188
191
channel .setByteRangeSpec (request .getByteRangeSpec ());
189
192
if (chunkSize != null ) {
190
193
channel .setChunkSize (chunkSize );
@@ -202,32 +205,31 @@ public boolean equals(Object o) {
202
205
}
203
206
BlobReadChannelV2State that = (BlobReadChannelV2State ) o ;
204
207
return Objects .equals (request , that .request )
205
- && Objects .equals (clientStuff , that .clientStuff )
208
+ && Objects .equals (options , that .options )
206
209
&& Objects .equals (chunkSize , that .chunkSize );
207
210
}
208
211
209
212
@ Override
210
213
public int hashCode () {
211
- return Objects .hash (request , clientStuff , chunkSize );
214
+ return Objects .hash (request , options , chunkSize );
212
215
}
213
216
214
217
@ Override
215
218
public String toString () {
216
219
return MoreObjects .toStringHelper (this )
217
- .add ("\n request " , request )
218
- .add ("\n clientStuff " , clientStuff )
219
- .add ("\n chunkSize " , chunkSize )
220
+ .add ("request " , request )
221
+ .add ("options " , options )
222
+ .add ("chunkSize " , chunkSize )
220
223
.toString ();
221
224
}
222
225
}
223
226
224
- static final class ClientStuff implements Serializable {
225
- private static final long serialVersionUID = 4244938428650333730L ;
227
+ static final class BlobReadChannelContext {
226
228
private final HttpStorageOptions storageOptions ;
227
229
private final HttpRetryAlgorithmManager retryAlgorithmManager ;
228
- private transient Storage apiaryClient ;
230
+ private final Storage apiaryClient ;
229
231
230
- private ClientStuff (
232
+ private BlobReadChannelContext (
231
233
HttpStorageOptions storageOptions ,
232
234
Storage apiaryClient ,
233
235
HttpRetryAlgorithmManager retryAlgorithmManager ) {
@@ -248,20 +250,16 @@ public Storage getApiaryClient() {
248
250
return apiaryClient ;
249
251
}
250
252
251
- private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
252
- in .defaultReadObject ();
253
- this .apiaryClient = storageOptions .getStorageRpcV1 ().getStorage ();
254
- }
255
-
256
- static ClientStuff from (HttpStorageOptions options ) {
257
- return new ClientStuff (
253
+ static BlobReadChannelContext from (HttpStorageOptions options ) {
254
+ return new BlobReadChannelContext (
258
255
options , options .getStorageRpcV1 ().getStorage (), options .getRetryAlgorithmManager ());
259
256
}
260
257
261
- static ClientStuff from (com .google .cloud .storage .Storage s ) {
262
- if (s instanceof StorageImpl ) {
263
- StorageImpl impl = (StorageImpl ) s ;
264
- return from (impl .getOptions ());
258
+ static BlobReadChannelContext from (com .google .cloud .storage .Storage s ) {
259
+ StorageOptions options = s .getOptions ();
260
+ if (options instanceof HttpStorageOptions ) {
261
+ HttpStorageOptions httpStorageOptions = (HttpStorageOptions ) options ;
262
+ return from (httpStorageOptions );
265
263
}
266
264
throw new IllegalArgumentException ("Only HttpStorageOptions based instance supported" );
267
265
}
@@ -271,10 +269,10 @@ public boolean equals(Object o) {
271
269
if (this == o ) {
272
270
return true ;
273
271
}
274
- if (!(o instanceof ClientStuff )) {
272
+ if (!(o instanceof BlobReadChannelContext )) {
275
273
return false ;
276
274
}
277
- ClientStuff that = (ClientStuff ) o ;
275
+ BlobReadChannelContext that = (BlobReadChannelContext ) o ;
278
276
return Objects .equals (storageOptions , that .storageOptions )
279
277
&& Objects .equals (retryAlgorithmManager , that .retryAlgorithmManager );
280
278
}
@@ -286,10 +284,7 @@ public int hashCode() {
286
284
287
285
@ Override
288
286
public String toString () {
289
- return MoreObjects .toStringHelper (this )
290
- .add ("\n storageOptions" , storageOptions )
291
- .add ("\n retryAlgorithmManager" , retryAlgorithmManager )
292
- .toString ();
287
+ return MoreObjects .toStringHelper (this ).add ("storageOptions" , storageOptions ).toString ();
293
288
}
294
289
}
295
290
}
0 commit comments