38
38
public class StreamDownloadTask extends StorageTask <StreamDownloadTask .TaskSnapshot > {
39
39
static final long PREFERRED_CHUNK_SIZE = 256 * 1024 ;
40
40
private static final String TAG = "StreamDownloadTask" ;
41
- private StorageReference mStorageRef ;
42
- private ExponentialBackoffSender mSender ;
43
- private volatile Exception mException = null ;
44
- private volatile int mResultCode = 0 ;
45
- private StreamProcessor mProcessor ;
46
-
47
- private long mTotalBytes = -1 ;
48
- private long mBytesDownloaded ;
49
- private long mBytesDownloadedSnapped ;
50
- private InputStream mInputStream ;
51
- private NetworkRequest mRequest ;
52
- private String mETagVerification ;
41
+ private StorageReference storageRef ;
42
+ private ExponentialBackoffSender sender ;
43
+ private volatile Exception exception = null ;
44
+ private volatile int resultCode = 0 ;
45
+ private StreamProcessor processor ;
46
+
47
+ private long totalBytes = -1 ;
48
+ private long bytesDownloaded ;
49
+ private long bytesDownloadedSnapped ;
50
+ private InputStream inputStream ;
51
+ private NetworkRequest request ;
52
+ private String eTagVerification ;
53
53
54
54
/*package*/ StreamDownloadTask (@ NonNull StorageReference storageRef ) {
55
- mStorageRef = storageRef ;
55
+ this . storageRef = storageRef ;
56
56
57
- FirebaseStorage storage = mStorageRef .getStorage ();
58
- mSender =
57
+ FirebaseStorage storage = this . storageRef .getStorage ();
58
+ sender =
59
59
new ExponentialBackoffSender (
60
60
storage .getApp ().getApplicationContext (),
61
61
storage .getAuthProvider (),
@@ -71,16 +71,16 @@ public class StreamDownloadTask extends StorageTask<StreamDownloadTask.TaskSnaps
71
71
*/
72
72
/*package*/ StreamDownloadTask setStreamProcessor (@ NonNull StreamProcessor processor ) {
73
73
Preconditions .checkNotNull (processor );
74
- Preconditions .checkState (mProcessor == null );
75
- this .mProcessor = processor ;
74
+ Preconditions .checkState (this . processor == null );
75
+ this .processor = processor ;
76
76
return this ;
77
77
}
78
78
79
79
/** @return the target of the download. */
80
80
@ Override
81
81
@ NonNull
82
82
/*package*/ StorageReference getStorage () {
83
- return mStorageRef ;
83
+ return storageRef ;
84
84
}
85
85
86
86
/**
@@ -89,16 +89,16 @@ public class StreamDownloadTask extends StorageTask<StreamDownloadTask.TaskSnaps
89
89
*/
90
90
@ SuppressWarnings ("unused" )
91
91
/*package*/ long getTotalBytes () {
92
- return mTotalBytes ;
92
+ return totalBytes ;
93
93
}
94
94
95
95
void recordDownloadedBytes (long bytesDownloaded ) {
96
- mBytesDownloaded += bytesDownloaded ;
97
- if (mBytesDownloadedSnapped + PREFERRED_CHUNK_SIZE <= mBytesDownloaded ) {
96
+ this . bytesDownloaded += bytesDownloaded ;
97
+ if (bytesDownloadedSnapped + PREFERRED_CHUNK_SIZE <= this . bytesDownloaded ) {
98
98
if (getInternalState () == INTERNAL_STATE_IN_PROGRESS ) {
99
99
tryChangeState (INTERNAL_STATE_IN_PROGRESS , false );
100
100
} else {
101
- mBytesDownloadedSnapped = mBytesDownloaded ;
101
+ bytesDownloadedSnapped = this . bytesDownloaded ;
102
102
}
103
103
}
104
104
}
@@ -112,37 +112,37 @@ protected void schedule() {
112
112
113
113
@ SuppressWarnings ({"JavaDoc" , "ThrowableResultOfMethodCallIgnored" })
114
114
private InputStream createDownloadStream () throws Exception {
115
- mSender .reset ();
115
+ sender .reset ();
116
116
117
- if (mRequest != null ) {
118
- mRequest .performRequestEnd ();
117
+ if (request != null ) {
118
+ request .performRequestEnd ();
119
119
}
120
120
121
- mRequest =
122
- new GetNetworkRequest (mStorageRef .getStorageUri (), mStorageRef .getApp (), mBytesDownloaded );
121
+ request =
122
+ new GetNetworkRequest (storageRef .getStorageUri (), storageRef .getApp (), bytesDownloaded );
123
123
124
- mSender .sendWithExponentialBackoff (mRequest , false );
125
- mResultCode = mRequest .getResultCode ();
126
- mException = mRequest .getException () != null ? mRequest .getException () : mException ;
124
+ sender .sendWithExponentialBackoff (request , false );
125
+ resultCode = request .getResultCode ();
126
+ exception = request .getException () != null ? request .getException () : exception ;
127
127
boolean success =
128
- isValidHttpResponseCode (mResultCode )
129
- && mException == null
128
+ isValidHttpResponseCode (resultCode )
129
+ && exception == null
130
130
&& getInternalState () == INTERNAL_STATE_IN_PROGRESS ;
131
131
132
132
if (success ) {
133
- String newEtag = mRequest .getResultString ("ETag" );
133
+ String newEtag = request .getResultString ("ETag" );
134
134
if (!TextUtils .isEmpty (newEtag )
135
- && mETagVerification != null
136
- && !mETagVerification .equals (newEtag )) {
137
- mResultCode = HttpURLConnection .HTTP_CONFLICT ;
135
+ && eTagVerification != null
136
+ && !eTagVerification .equals (newEtag )) {
137
+ resultCode = HttpURLConnection .HTTP_CONFLICT ;
138
138
throw new IOException ("The ETag on the server changed." );
139
139
}
140
140
141
- mETagVerification = newEtag ;
142
- if (mTotalBytes == -1 ) {
143
- mTotalBytes = mRequest .getResultingContentLength ();
141
+ eTagVerification = newEtag ;
142
+ if (totalBytes == -1 ) {
143
+ totalBytes = request .getResultingContentLength ();
144
144
}
145
- return mRequest .getStream ();
145
+ return request .getStream ();
146
146
} else {
147
147
throw new IOException ("Could not open resulting stream." );
148
148
}
@@ -152,7 +152,7 @@ private InputStream createDownloadStream() throws Exception {
152
152
@ SuppressWarnings ({"JavaDoc" , "ThrowableResultOfMethodCallIgnored" })
153
153
@ Override
154
154
/*package*/ void run () {
155
- if (mException != null ) {
155
+ if (exception != null ) {
156
156
tryChangeState (INTERNAL_STATE_FAILURE , false );
157
157
return ;
158
158
}
@@ -170,31 +170,31 @@ public InputStream call() throws Exception {
170
170
}
171
171
},
172
172
StreamDownloadTask .this );
173
- mInputStream = new BufferedInputStream (streamWrapper );
173
+ inputStream = new BufferedInputStream (streamWrapper );
174
174
175
175
try {
176
176
// Open stream to fetch initial state.
177
177
streamWrapper .ensureStream ();
178
178
179
- if (mProcessor != null ) {
179
+ if (processor != null ) {
180
180
try {
181
- mProcessor .doInBackground (snapState (), mInputStream );
181
+ processor .doInBackground (snapState (), inputStream );
182
182
} catch (Exception e ) {
183
183
Log .w (TAG , "Exception occurred calling doInBackground." , e );
184
- mException = e ;
184
+ exception = e ;
185
185
}
186
186
}
187
187
} catch (IOException e ) {
188
188
Log .d (TAG , "Initial opening of Stream failed" , e );
189
- mException = e ;
189
+ exception = e ;
190
190
}
191
191
192
- if (mInputStream == null ) {
193
- mRequest .performRequestEnd ();
194
- mRequest = null ;
192
+ if (inputStream == null ) {
193
+ request .performRequestEnd ();
194
+ request = null ;
195
195
}
196
196
197
- boolean success = mException == null && getInternalState () == INTERNAL_STATE_IN_PROGRESS ;
197
+ boolean success = exception == null && getInternalState () == INTERNAL_STATE_IN_PROGRESS ;
198
198
199
199
if (success ) {
200
200
tryChangeState (INTERNAL_STATE_IN_PROGRESS , false );
@@ -228,19 +228,18 @@ public boolean pause() {
228
228
@ Override
229
229
TaskSnapshot snapStateImpl () {
230
230
return new TaskSnapshot (
231
- StorageException .fromExceptionAndHttpCode (mException , mResultCode ),
232
- mBytesDownloadedSnapped );
231
+ StorageException .fromExceptionAndHttpCode (exception , resultCode ), bytesDownloadedSnapped );
233
232
}
234
233
235
234
@ Override
236
235
protected void onCanceled () {
237
- mSender .cancel ();
238
- mException = StorageException .fromErrorStatus (Status .RESULT_CANCELED );
236
+ sender .cancel ();
237
+ exception = StorageException .fromErrorStatus (Status .RESULT_CANCELED );
239
238
}
240
239
241
240
@ Override
242
241
protected void onProgress () {
243
- mBytesDownloadedSnapped = mBytesDownloaded ;
242
+ bytesDownloadedSnapped = bytesDownloaded ;
244
243
}
245
244
246
245
private boolean isValidHttpResponseCode (int code ) {
@@ -377,9 +376,9 @@ public void close() throws IOException {
377
376
mWrappedStream .close ();
378
377
}
379
378
mStreamClosed = true ;
380
- if (mParentTask != null && mParentTask .mRequest != null ) {
381
- mParentTask .mRequest .performRequestEnd ();
382
- mParentTask .mRequest = null ;
379
+ if (mParentTask != null && mParentTask .request != null ) {
380
+ mParentTask .request .performRequestEnd ();
381
+ mParentTask .request = null ;
383
382
}
384
383
385
384
checkCancel ();
@@ -506,7 +505,7 @@ public long getTotalByteCount() {
506
505
*/
507
506
@ PublicApi
508
507
public InputStream getStream () {
509
- return StreamDownloadTask .this .mInputStream ;
508
+ return StreamDownloadTask .this .inputStream ;
510
509
}
511
510
}
512
511
}
0 commit comments