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.OutputStream;
22
import java.util.HashMap;
23
import java.util.Map;
24
 
25
class ProgressNoopOutputStream extends OutputStream implements RequestOutputStream {
26
    private final Map<Request, RequestProgress> progressMap = new HashMap<Request, RequestProgress>();
27
    private final Handler callbackHandler;
28
 
29
    private Request currentRequest;
30
    private RequestProgress currentRequestProgress;
31
    private int batchMax;
32
 
33
    ProgressNoopOutputStream(Handler callbackHandler) {
34
        this.callbackHandler = callbackHandler;
35
    }
36
 
37
    public void setCurrentRequest(Request currentRequest) {
38
        this.currentRequest = currentRequest;
39
        this.currentRequestProgress = currentRequest != null? progressMap.get(currentRequest) : null;
40
    }
41
 
42
    int getMaxProgress() {
43
        return batchMax;
44
    }
45
 
46
    Map<Request,RequestProgress> getProgressMap() {
47
        return progressMap;
48
    }
49
 
50
    void addProgress(long size) {
51
        if (currentRequestProgress == null) {
52
            currentRequestProgress = new RequestProgress(callbackHandler, currentRequest);
53
            progressMap.put(currentRequest, currentRequestProgress);
54
        }
55
 
56
        currentRequestProgress.addToMax(size);
57
        batchMax += size;
58
    }
59
 
60
    @Override
61
    public void write(byte[] buffer) {
62
        addProgress(buffer.length);
63
    }
64
 
65
    @Override
66
    public void write(byte[] buffer, int offset, int length) {
67
        addProgress(length);
68
    }
69
 
70
    @Override
71
    public void write(int oneByte) {
72
        addProgress(1);
73
    }
74
}