Hi,

On Tue, Aug 30, 2011 at 8:01 PM, Michael McCandless
<[email protected]> wrote:
> I think Tika.parseToString (static sugar method) closes the
> InputStream for you, while the Parser.parse method does not?
> Kinda confusing!

It is, but there's a design behind this. :-) The basic idea behind
resource handling in Tika is that whoever opens a stream or another
resource is also responsible for properly closing or releasing it.
This (I think) is also pretty well documented in Tika's key
interfaces.

The exception to this rule is the Tika.parseToString(InputStream,
Metadata) method. The reason for making an exception here is that the
Tika facade class was designed primarily for convenience and to
minimize the amount of code that the consumer of the API needs to
write. The proper resource management pattern in this case would have
been:

    InputStream stream = ...;
    try {
        return tika.parseToString(stream, ...);
    } finally {
        stream.close();
    }

However, since in this specific case the client application hardly
ever needs to use the stream for anything else and since in pretty
much all cases the stream in question is constructed right when the
parseToString call is made, it makes more sense for the
parseToString() method to take care of closing the stream. The result
is that the above code can be reduced to:

    return tika.parseToString(..., ...);

BR,

Jukka Zitting

Reply via email to