Home > other >  Difference between `InputSource` and `StreamSource`
Difference between `InputSource` and `StreamSource`

Time:09-16

I wonder what use case InputSource is there for? In other words, is there any situation where InputSource can’t be replaced by StreamSource? In still other words, why do these two classes exist in the JDK?

AFAICS: Both classes lie in the java.xml module, and their APIs are almost identical, the only difference being that InputSource has an “encoding” parameter; and that StreamSource is part of a hierarchy (as it implements Source), which makes it nicer to use in some contexts (you can decide to accept a Source if that is enough for your purpose when designing your API). Furthermore, they both were introduced in Java 1.4.

As also pointed out here, these classes are used in different parts of the JDK API: “A Parser can take its input from an InputSource, but not from a StreamSource. A Transformer can take its input from a StreamSource, but not from an InputSource.” But this does not explain why the designers of the JDK API decided to introduce two apparently redundant classes for the purpose of representing a source. Hence, my question.

(Slightly related: difference between InputSource and InputStream.)

CodePudding user response:

It's always difficult to answer questions of the form "why did the designers of X decide to do it this way" when you aren't one of the designers and don't have access to the minutes of their meetings, but in this case there is a simple answer: they decided to adopt existing APIs that were designed by third parties, and to adopt them "as is" rather than trying to integrate them and remove redundancy.

It doesn't help, of course, that Java interfaces are (or were until recently) cast in stone: once released, they can't be changed or extended in any way without creating an incompatibility.

  • Related