KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > fileupload > UploadStatus


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.fileupload;
25
26 import java.util.HashMap JavaDoc;
27
28 import org.riotfamily.common.util.FormatUtils;
29
30
31 /**
32  * Class that holds upload information like progress and transfer rate.
33  */

34 public class UploadStatus {
35     
36     private static HashMap JavaDoc statusMap = new HashMap JavaDoc();
37
38     private CountingServletInputStream inputStream;
39     
40     private long bytesTotal;
41     
42     private long startTime;
43     
44     protected UploadStatus(HttpUploadRequest request) {
45         this.inputStream = request.getCountingInputStream();
46         this.bytesTotal = request.getContentLength();
47         this.startTime = System.currentTimeMillis();
48     }
49     
50     public int getProgress() {
51         return (int) ((float) inputStream.getBytesRead() / bytesTotal * 100);
52     }
53     
54     public int getProgressWidth(int totalWidth, int stepWidth) {
55         int i = (int) ((float) inputStream.getBytesRead() /
56                 bytesTotal * totalWidth);
57         
58         return i - i % stepWidth;
59     }
60     
61     public int getKbTransfered() {
62         return (int) (inputStream.getBytesRead() / 1024);
63     }
64     
65     public String JavaDoc getDataTransfered() {
66         return FormatUtils.formatByteSize(inputStream.getBytesRead());
67     }
68     
69     public int getEstimatedTimeLeft() {
70         return 0;
71     }
72     
73     public int getTimeElapsed() {
74         return (int) ((System.currentTimeMillis() - startTime) / 1000);
75     }
76     
77     public String JavaDoc getTransferRate() {
78         if (getTimeElapsed() == 0) {
79             return "";
80         }
81         return FormatUtils.formatByteSize(inputStream.getBytesRead() /
82                 getTimeElapsed()) + "/s";
83     }
84     
85     static void add(String JavaDoc uploadId, HttpUploadRequest request) {
86         statusMap.put(uploadId, new UploadStatus(request));
87     }
88     
89     public static UploadStatus getStatus(String JavaDoc uploadId) {
90         return (UploadStatus) statusMap.get(uploadId);
91     }
92     
93     public static void clearStatus(Object JavaDoc uploadId) {
94         statusMap.remove(uploadId);
95     }
96     
97     public static synchronized String JavaDoc createUploadId() {
98         return String.valueOf(statusMap.size());
99     }
100 }
101
Popular Tags