| 1 24 package org.riotfamily.forms.fileupload; 25 26 import java.util.HashMap ; 27 28 import org.riotfamily.common.util.FormatUtils; 29 30 31 34 public class UploadStatus { 35 36 private static HashMap statusMap = new HashMap (); 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 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 getTransferRate() { 78 if (getTimeElapsed() == 0) { 79 return ""; 80 } 81 return FormatUtils.formatByteSize(inputStream.getBytesRead() / 82 getTimeElapsed()) + "/s"; 83 } 84 85 static void add(String uploadId, HttpUploadRequest request) { 86 statusMap.put(uploadId, new UploadStatus(request)); 87 } 88 89 public static UploadStatus getStatus(String uploadId) { 90 return (UploadStatus) statusMap.get(uploadId); 91 } 92 93 public static void clearStatus(Object uploadId) { 94 statusMap.remove(uploadId); 95 } 96 97 public static synchronized String createUploadId() { 98 return String.valueOf(statusMap.size()); 99 } 100 } 101 | Popular Tags |