I have a Groovy/Grails web app that reads e-mails from Gmail. It gets messages and attachments well, but I would like to limit the size of an attachment that I receive. Currently I have this code to read attachments:
byte[] file = new byte[maxSizeInBytes]
BufferedInputStream bis = new BufferedInputStream(part.getInputStream())
try {
file = bis.getBytes()
} catch (Exception e) {
...
}
The problem here is that Groovy simply expands the byte array to fit whatever comes in, so if my maxSizeInBytes is set to five megabytes (5*1024*1024), and I receive an attachment that is 10 megabytes the byte array simply expands to fit the entire file. How do I limit the size here? Even better, Is there a way to get how many bytes is the attachment before actually trying to download it?
Does
part.inputStream.available()give you a resonable approximation?Edit
Or,
part.sizeprobably gives you the encoded size of the attachment. I saw over here that multiplying this value by0.65should give you a rough approximation of the final attachment size