Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
14792 manas 1
/**
2
 * Copyright 2010-present Facebook.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *    http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.facebook;
18
 
19
import android.os.Handler;
20
 
21
import java.io.FilterOutputStream;
22
import java.io.IOException;
23
import java.io.OutputStream;
24
import java.util.Map;
25
 
26
class ProgressOutputStream extends FilterOutputStream implements RequestOutputStream {
27
    private final Map<Request, RequestProgress> progressMap;
28
    private final RequestBatch requests;
29
    private final long threshold;
30
 
31
    private long batchProgress, lastReportedProgress, maxProgress;
32
    private RequestProgress currentRequestProgress;
33
 
34
    ProgressOutputStream(OutputStream out, RequestBatch requests, Map<Request, RequestProgress> progressMap, long maxProgress) {
35
        super(out);
36
        this.requests = requests;
37
        this.progressMap = progressMap;
38
        this.maxProgress = maxProgress;
39
 
40
        this.threshold = Settings.getOnProgressThreshold();
41
    }
42
 
43
    private void addProgress(long size) {
44
        if (currentRequestProgress != null) {
45
            currentRequestProgress.addProgress(size);
46
        }
47
 
48
        batchProgress += size;
49
 
50
        if (batchProgress >= lastReportedProgress + threshold || batchProgress >= maxProgress) {
51
            reportBatchProgress();
52
        }
53
    }
54
 
55
    private void reportBatchProgress() {
56
        if (batchProgress > lastReportedProgress) {
57
            for (RequestBatch.Callback callback : requests.getCallbacks()) {
58
                if (callback instanceof RequestBatch.OnProgressCallback) {
59
                    final Handler callbackHandler = requests.getCallbackHandler();
60
 
61
                    // Keep copies to avoid threading issues
62
                    final RequestBatch.OnProgressCallback progressCallback = (RequestBatch.OnProgressCallback) callback;
63
                    if (callbackHandler == null) {
64
                        progressCallback.onBatchProgress(requests, batchProgress, maxProgress);
65
                    }
66
                    else {
67
                        callbackHandler.post(new Runnable() {
68
                            @Override
69
                            public void run() {
70
                                progressCallback.onBatchProgress(requests, batchProgress, maxProgress);
71
                            }
72
                        });
73
                    }
74
                }
75
            }
76
 
77
            lastReportedProgress = batchProgress;
78
        }
79
    }
80
 
81
    public void setCurrentRequest(Request request) {
82
        currentRequestProgress = request != null? progressMap.get(request) : null;
83
    }
84
 
85
    long getBatchProgress() {
86
        return batchProgress;
87
    }
88
 
89
    long getMaxProgress() {
90
        return maxProgress;
91
    }
92
 
93
    @Override
94
    public void write(byte[] buffer) throws IOException {
95
        out.write(buffer);
96
        addProgress(buffer.length);
97
    }
98
 
99
    @Override
100
    public void write(byte[] buffer, int offset, int length) throws IOException {
101
        out.write(buffer, offset, length);
102
        addProgress(length);
103
    }
104
 
105
    @Override
106
    public void write(int oneByte) throws IOException {
107
        out.write(oneByte);
108
        addProgress(1);
109
    }
110
 
111
    @Override
112
    public void close() throws IOException {
113
        super.close();
114
 
115
        for (RequestProgress p : progressMap.values()) {
116
            p.reportProgress();
117
        }
118
 
119
        reportBatchProgress();
120
    }
121
}