John Manak
John Manak

Reputation: 13558

FileBackedOutputStream use case (Guava)

I came across FileBackedOutputStream class from Google Guava library and was wondering if it's suitable to be used as a kind of a buffer: once every day, a process in my webapp generates tens of thousands of lines (each containing about 100characters) which are then uploaded to a file on an FTP server. I was thinking of using a FileBackedOutputStream object to first write all these strings to and then give access to them to my FTP client by using FileBackedOutputStream.getSupplier().getInput(), which returns an InputStream. Would this be a correct use case for FileBackedOutputStream?

Upvotes: 3

Views: 2320

Answers (2)

Hrvoje
Hrvoje

Reputation: 189

I know this is a rather old post, but just in case you are still using the above mentioned class, just be aware of this vulnerability, https://nvd.nist.gov/vuln/detail/CVE-2023-2976, where files created by using the older versions might be vulnerable to attack, namely files and/or directories may be accessible to other parties (improper authorization)

Upvotes: 0

ColinD
ColinD

Reputation: 110094

Yes, I think that would be an acceptable use case for FileBackedOutputStream. However, I think FileBackedOutputStream is best when you're using it with data that may vary in size considerably... for small amounts of data that can fit in memory without a problem you want to just buffer them in memory but for large amounts of data that might give you an OutOfMemoryError if you try to read it all in to memory, you want to switch to buffering to a file. This is where FileBackedOutputStream really shines I think. I've used it for buffering uploaded files that I need to do several things with.

Upvotes: 4

Related Questions