3

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?

John Manak
  • 13,328
  • 29
  • 78
  • 119
  • If you want an InputStream, why not use a ByteArrayOutputStream and a ByteArrayInputStream? Its likely to be simpler and more efficient. – Peter Lawrey Jan 27 '11 at 14:01
  • Is that also suitable for data of the size I mentioned, i.e. tens of thousands of lines (strings), each about 100 characters long? – John Manak Jan 27 '11 at 14:11
  • It's thread safe which is important when I/O resources are used. Give it a try – Boris Pavlović Jan 27 '11 at 14:13
  • Give a try to ByteArrayOutputStream or FileBackedOutputStream? – John Manak Jan 27 '11 at 14:17
  • Peter: BAOS is slightly more efficient, but FileBackedOutputStream was designed for efficiency: [it] starts buffering to a byte array, but switches to file buffering once the data reaches a configurable size. So you get most of both: in-memory speed for small data and huge capacity. – maaartinus Jan 28 '11 at 08:05

1 Answers1

4

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.

ColinD
  • 108,630
  • 30
  • 201
  • 202