Created
November 10, 2016 05:08
-
-
Save mushfek/03959000f4db7c337fff41b8d8e95e8e to your computer and use it in GitHub Desktop.
JSP Tag Pooling Memory Leak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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