I have a file on the disk. I want a clear distinction of responsibilities between my reader and parser. The reader reads the file and extracts the content as a string.

This string is input to the parser.

When should I share the content as a String vs InputStream between the two classes?

  • Umm. Ok, let us know what you come up with. You don't have a question here. Is the string ever going to be long enough that you have to worry about memory issues if you have it memory at once. Is that still true if you are handling multiple strings at once? Do you constantly have to back track? How much context is required to parse reliably? That's the type of things you need to ask yourself. If you are having problems with a specific question, then ask that. – Berin Loritsch Jun 1 '18 at 14:30
  • Thank you. I appreciate your response. My ask was when would I choose one over the other? I should have been more clear. – dpm Jun 1 '18 at 15:39

It would generally be better for the parser to accept an InputStream, as this makes it more versatile and allows it to be used in more situations. It doesn't matter whether the data to be parsed is coming from a file, a network socket, or anything else. It also helps, as Berin Loritsch mentioned in comments, if the data is longer than can reasonably be held entirely in memory.

If what you have actually is a String, then you can convert this into an InputStream by using, for example, a StringBufferInputStream.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.