Skip to content

Instantly share code, notes, and snippets.

@mushfek
Created November 10, 2016 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mushfek/b78ca28747ff08e6ebd89d0700489543 to your computer and use it in GitHub Desktop.
Save mushfek/b78ca28747ff08e6ebd89d0700489543 to your computer and use it in GitHub Desktop.
public class AverageFileSize extends SimpleTagSupport {
private boolean humanReadable;
private List<AttachedFile> fileList;
public AverageFileSize(List<AttachedFile> fileList) {
this.humanReadable = false;
this.fileList = fileList;
}
public AverageFileSize(boolean humanReadable, List<AttachedFile> fileList) {
this.humanReadable = humanReadable;
this.fileList = fileList;
}
@Override
public void doTag() throws JspException, IOException {
long fileSizeAvg = 0;
for (AttachedFile f : fileList) {
fileSizeAvg += f.getSize();
}
fileSizeAvg /= fileList.size();
try {
String fileSize = humanReadable ? getPrettyFileSize(fileSizeAvg) : Long.toString(fileSizeAvg) + "B";
getJspContext().getOut().write(fileSize);
} catch (IOException e) {
throw new SkipPageException(e);
}
}
private String getPrettyFileSize(long sizeInBytes) {
String prettyFileSize = "";
/**
* Logic for formatting to human readable
*/
return prettyFileSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment