001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.protocol.http.servlet; 018 019import java.time.Duration; 020import org.apache.wicket.Session; 021import org.apache.wicket.util.io.IClusterable; 022import org.apache.wicket.util.lang.Bytes; 023import org.apache.wicket.util.time.Durations; 024 025 026/** 027 * Holds information about an upload, also has useful querying methods. 028 * 029 * @author Igor Vaynberg (ivaynberg) 030 * 031 */ 032public class UploadInfo implements IClusterable 033{ 034 private static final long serialVersionUID = 1L; 035 036 private transient long timeStarted; 037 private transient long totalBytes; 038 private transient long bytesUploaded; 039 040 /** 041 * @param totalBytes 042 */ 043 public UploadInfo(final int totalBytes) 044 { 045 timeStarted = System.currentTimeMillis(); 046 this.totalBytes = totalBytes; 047 } 048 049 /** 050 * @return bytes uploaded so far 051 */ 052 public long getBytesUploaded() 053 { 054 return bytesUploaded; 055 } 056 057 /** 058 * Sets bytes uploaded so far 059 * 060 * @param bytesUploaded 061 */ 062 public void setBytesUploaded(final long bytesUploaded) 063 { 064 this.bytesUploaded = bytesUploaded; 065 } 066 067 /** 068 * @return human readable string of bytes uploaded so far 069 */ 070 public String getBytesUploadedString() 071 { 072 return Bytes.bytes(bytesUploaded).toString(Session.get().getLocale()); 073 } 074 075 /** 076 * @return human readable string of total number of bytes 077 */ 078 public String getTotalBytesString() 079 { 080 return Bytes.bytes(totalBytes).toString(Session.get().getLocale()); 081 } 082 083 /** 084 * @return total bytes in the upload 085 */ 086 public long getTotalBytes() 087 { 088 return totalBytes; 089 } 090 091 /** 092 * @return milliseconds elapsed since upload started 093 */ 094 public long getElapsedMilliseconds() 095 { 096 return System.currentTimeMillis() - timeStarted; 097 } 098 099 /** 100 * @return seconds elapsed since upload started 101 */ 102 public long getElapsedSeconds() 103 { 104 return getElapsedMilliseconds() / 1000L; 105 } 106 107 108 /** 109 * @return transfer rate in bits per second 110 */ 111 public long getTransferRateBPS() 112 { 113 return bytesUploaded / Math.max(getElapsedSeconds(), 1); 114 } 115 116 /** 117 * @return transfer rate in a human readable string 118 */ 119 public String getTransferRateString() 120 { 121 return Bytes.bytes(getTransferRateBPS()).toString(Session.get().getLocale()) + "/s"; 122 } 123 124 /** 125 * @return percent of the upload completed 126 */ 127 public int getPercentageComplete() 128 { 129 if (totalBytes == 0) 130 { 131 return 100; 132 } 133 return (int)(((double)bytesUploaded / (double)totalBytes) * 100); 134 135 } 136 137 /** 138 * @return estimate of the remaining number of milliseconds 139 */ 140 public long getRemainingMilliseconds() 141 { 142 int percentageComplete = getPercentageComplete(); 143 144 145 long totalTime = ((getElapsedSeconds() * 100) / Math.max(percentageComplete, 1)); 146 long remainingTime = (totalTime - getElapsedSeconds()); 147 148 return remainingTime * 1000; // convert seconds to milliseconds and return 149 } 150 151 /** 152 * @return estimate of the remaining time in a human readable string 153 */ 154 public String getRemainingTimeString() 155 { 156 return Durations.toString(Duration.ofMillis(getRemainingMilliseconds()), 157 Session.get().getLocale()); 158 } 159}