19
19
import static com .google .gcloud .RetryHelper .runWithRetries ;
20
20
21
21
import com .google .api .services .storage .model .StorageObject ;
22
+ import com .google .common .base .MoreObjects ;
22
23
import com .google .gcloud .RetryHelper ;
23
24
import com .google .gcloud .spi .StorageRpc ;
24
25
25
26
import java .io .IOException ;
26
- import java .io .ObjectInputStream ;
27
- import java .io .ObjectOutputStream ;
27
+ import java .io .Serializable ;
28
28
import java .nio .ByteBuffer ;
29
29
import java .util .Map ;
30
+ import java .util .Objects ;
30
31
import java .util .concurrent .Callable ;
31
32
32
33
/**
35
36
class BlobReadChannelImpl implements BlobReadChannel {
36
37
37
38
private static final int DEFAULT_CHUNK_SIZE = 2 * 1024 * 1024 ;
38
- private static final long serialVersionUID = 4821762590742862669L ;
39
39
40
40
private final StorageOptions serviceOptions ;
41
41
private final BlobId blob ;
@@ -45,10 +45,10 @@ class BlobReadChannelImpl implements BlobReadChannel {
45
45
private boolean endOfStream ;
46
46
private int chunkSize = DEFAULT_CHUNK_SIZE ;
47
47
48
- private transient StorageRpc storageRpc ;
49
- private transient StorageObject storageObject ;
50
- private transient int bufferPos ;
51
- private transient byte [] buffer ;
48
+ private StorageRpc storageRpc ;
49
+ private StorageObject storageObject ;
50
+ private int bufferPos ;
51
+ private byte [] buffer ;
52
52
53
53
BlobReadChannelImpl (StorageOptions serviceOptions , BlobId blob ,
54
54
Map <StorageRpc .Option , ?> requestOptions ) {
@@ -59,19 +59,18 @@ class BlobReadChannelImpl implements BlobReadChannel {
59
59
initTransients ();
60
60
}
61
61
62
- private void writeObject (ObjectOutputStream out ) throws IOException {
62
+ @ Override
63
+ public State save () {
64
+ StateImpl .Builder builder = StateImpl .builder (serviceOptions , blob , requestOptions )
65
+ .position (position )
66
+ .isOpen (isOpen )
67
+ .endOfStream (endOfStream )
68
+ .chunkSize (chunkSize );
63
69
if (buffer != null ) {
64
- position += bufferPos ;
65
- buffer = null ;
66
- bufferPos = 0 ;
67
- endOfStream = false ;
70
+ builder .position (position + bufferPos );
71
+ builder .endOfStream (false );
68
72
}
69
- out .defaultWriteObject ();
70
- }
71
-
72
- private void readObject (ObjectInputStream in ) throws IOException , ClassNotFoundException {
73
- in .defaultReadObject ();
74
- initTransients ();
73
+ return builder .build ();
75
74
}
76
75
77
76
private void initTransients () {
@@ -148,4 +147,116 @@ public byte[] call() {
148
147
}
149
148
return toWrite ;
150
149
}
150
+
151
+ static class StateImpl implements BlobReadChannel .State , Serializable {
152
+
153
+ private static final long serialVersionUID = 3889420316004453706L ;
154
+
155
+ private final StorageOptions serviceOptions ;
156
+ private final BlobId blob ;
157
+ private final Map <StorageRpc .Option , ?> requestOptions ;
158
+ private final int position ;
159
+ private final boolean isOpen ;
160
+ private final boolean endOfStream ;
161
+ private final int chunkSize ;
162
+
163
+ StateImpl (Builder builder ) {
164
+ this .serviceOptions = builder .serviceOptions ;
165
+ this .blob = builder .blob ;
166
+ this .requestOptions = builder .requestOptions ;
167
+ this .position = builder .position ;
168
+ this .isOpen = builder .isOpen ;
169
+ this .endOfStream = builder .endOfStream ;
170
+ this .chunkSize = builder .chunkSize ;
171
+ }
172
+
173
+ public static class Builder {
174
+ private final StorageOptions serviceOptions ;
175
+ private final BlobId blob ;
176
+ private final Map <StorageRpc .Option , ?> requestOptions ;
177
+ private int position ;
178
+ private boolean isOpen ;
179
+ private boolean endOfStream ;
180
+ private int chunkSize ;
181
+
182
+ private Builder (StorageOptions options , BlobId blob , Map <StorageRpc .Option , ?> reqOptions ) {
183
+ this .serviceOptions = options ;
184
+ this .blob = blob ;
185
+ this .requestOptions = reqOptions ;
186
+ }
187
+
188
+ public Builder position (int position ) {
189
+ this .position = position ;
190
+ return this ;
191
+ }
192
+
193
+ public Builder isOpen (boolean isOpen ) {
194
+ this .isOpen = isOpen ;
195
+ return this ;
196
+ }
197
+
198
+ public Builder endOfStream (boolean endOfStream ) {
199
+ this .endOfStream = endOfStream ;
200
+ return this ;
201
+ }
202
+
203
+ public Builder chunkSize (int chunkSize ) {
204
+ this .chunkSize = chunkSize ;
205
+ return this ;
206
+ }
207
+
208
+ public State build () {
209
+ return new StateImpl (this );
210
+ }
211
+ }
212
+
213
+ public static Builder builder (
214
+ StorageOptions options , BlobId blob , Map <StorageRpc .Option , ?> reqOptions ) {
215
+ return new Builder (options , blob , reqOptions );
216
+ }
217
+
218
+ @ Override
219
+ public BlobReadChannel restore () {
220
+ BlobReadChannelImpl channel = new BlobReadChannelImpl (serviceOptions , blob , requestOptions );
221
+ channel .position = position ;
222
+ channel .isOpen = isOpen ;
223
+ channel .endOfStream = endOfStream ;
224
+ channel .chunkSize = chunkSize ;
225
+ return channel ;
226
+ }
227
+
228
+ @ Override
229
+ public int hashCode () {
230
+ return Objects .hash (serviceOptions , blob , requestOptions , position , isOpen , endOfStream ,
231
+ chunkSize );
232
+ }
233
+
234
+ @ Override
235
+ public boolean equals (Object obj ) {
236
+ if (obj == null ) {
237
+ return false ;
238
+ }
239
+ if (!(obj instanceof StateImpl )) {
240
+ return false ;
241
+ }
242
+ final StateImpl other = (StateImpl ) obj ;
243
+ return Objects .equals (this .serviceOptions , other .serviceOptions ) &&
244
+ Objects .equals (this .blob , other .blob ) &&
245
+ Objects .equals (this .requestOptions , other .requestOptions ) &&
246
+ this .position == other .position &&
247
+ this .isOpen == other .isOpen &&
248
+ this .endOfStream == other .endOfStream &&
249
+ this .chunkSize == other .chunkSize ;
250
+ }
251
+
252
+ @ Override
253
+ public String toString () {
254
+ return MoreObjects .toStringHelper (this )
255
+ .add ("blob" , blob )
256
+ .add ("position" , position )
257
+ .add ("isOpen" , isOpen )
258
+ .add ("endOfStream" , endOfStream )
259
+ .toString ();
260
+ }
261
+ }
151
262
}
0 commit comments