FileBackedOutputStream 用例 (Guava)
我从 Google Guava
库中发现了 FileBackedOutputStream
类,想知道它是否适合用作一种缓冲区:每天一次,我的 web 应用程序中的一个进程会生成数十个数千行(每行大约包含 100 个字符),然后上传到 FTP 服务器上的文件。我正在考虑使用 FileBackedOutputStream 对象首先写入所有这些字符串,然后使用 FileBackedOutputStream.getSupplier().getInput() 向我的 FTP 客户端提供对它们的访问权限,它返回一个InputStream
。这是 FileBackedOutputStream 的正确用例吗?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我认为这对于 FileBackedOutputStream 来说是一个可接受的用例。但是,我认为当您将 FileBackedOutputStream 与大小可能相差很大的数据一起使用时,它是最好的......对于可以容纳在内存中而没有问题的少量数据,您只想缓冲它们但对于大量数据,如果您尝试将其全部读入内存,可能会出现 OutOfMemoryError 错误,您需要切换到缓冲到文件。我认为这就是 FileBackedOutputStream 真正发挥作用的地方。我用它来缓冲上传的文件,我需要用它来做几件事。
Yes, I think that would be an acceptable use case for
FileBackedOutputStream
. However, I thinkFileBackedOutputStream
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 anOutOfMemoryError
if you try to read it all in to memory, you want to switch to buffering to a file. This is whereFileBackedOutputStream
really shines I think. I've used it for buffering uploaded files that I need to do several things with.我知道这是一篇相当旧的帖子,但以防万一您仍在使用上述类,请注意此漏洞,https://nvd.nist.gov/vuln/detail/CVE-2023-2976,其中使用旧版本创建的文件可能容易受到攻击,即其他方可以访问文件和/或目录(授权不当)
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)