You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by Josh Micich <jo...@gmail.com> on 2008/02/06 21:53:32 UTC
Should POIFSFileSystem close its input stream (Buzilla 44366)?
The way POIFSFileSystem is implemented right now, the caller is
responsible for closing the input stream supplied. During normal
operation, POIFSFileSystem reads until EOF (see code within
RawDataBlockList/RawDataBlock). In most cases the input stream is
being closed by the caller (either explicitly, or implicitly due to
garbage collection of an input stream local variable).
POIFSFileSystem could close the input stream, but doesn't.
I ran into this problem, when a java process held a lock on an XLS
file long after HSSFWorkbook had finished reading it. Of course the
solution was simple - just call fis.close(), but I was wondering how
many other places this problem might exist.
My suggestion is to have POIFSFileSystem always close the input
stream, so the caller never has to worry about that detail.
There is a potential problem with this proposed patch for the unusual
case where the caller supplies an input stream that can continue to be
used after reaching EOF. I've never seen such a stream, but any
arbitrary subclass of InputStream could allow such behaviour. In this
unusual circumstance however, the caller could wrap the input stream
in order to trap the close() call.
Another less likely problem with the patch concerns calling close()
twice on an input stream. The javadoc has nothing to say about this
point, but all major subclasses of InputStream have no trouble with
this.
So I guess it is a question of whether we want to code "is.close()" in
a finally block after every call to POIFSFileSystem (and classes that
utilise it), vs writing wrapper code in the uncommon cases when
"is.close()" is unwanted.
All existing junits work in the presence of this new patch. If anyone
can think of a scenario where this patch (always closing the input
stream) would be bad, it would be good to see this requirement
demonstrated as a new junit.
Either way this gets decided, I would like to update the javadoc.
It's always good to document who is responsible for closing a stream.
-josh
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Nick Burch <ni...@torchbox.com>.
On Fri, 15 Feb 2008, Josh Micich wrote:
> I made those changes and re-submitted the patch (also moving the
> unrelated test suite hierarchy stuff elsewhere). Is this what you had
> in mind?
Looks just the job, thanks. I've applied your patch + test to svn trunk
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Josh Micich <jo...@gmail.com>.
I made those changes and re-submitted the patch (also moving the
unrelated test suite hierarchy stuff elsewhere). Is this what you had
in mind?
-Josh
On Fri, Feb 8, 2008 at 4:40 AM, Nick Burch <ni...@torchbox.com> wrote:
> On Thu, 7 Feb 2008, Josh Micich wrote:
> > I didn't think closing the input stream twice was the main worry. My
> > main concern was the possibility of reading the input stream after
> > reaching EOF. One way to achieve this would be with a stream that
> > returned true for markSupported(). reset() could be called at any time,
> > and the whole stream could be re-read without re-opening. (Note - the
> > DB2 driver supports this).
>
> That does give me an idea. How about we check for markSupported() before
> we auto-close the input stream. If it is supported, we issue a warning to
> the user that behaviour has changed. If we were to also provide the
> InputStream wrapper that ignores the close, we can tell them the one line
> fix with the warning.
>
> (We can turn the warning off a release or two later on, once everyone who
> needs to has fixed their code)
>
> Does that sound sensible and workable to people?
>
>
> Nick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Nick Burch <ni...@torchbox.com>.
On Thu, 7 Feb 2008, Josh Micich wrote:
> I didn't think closing the input stream twice was the main worry. My
> main concern was the possibility of reading the input stream after
> reaching EOF. One way to achieve this would be with a stream that
> returned true for markSupported(). reset() could be called at any time,
> and the whole stream could be re-read without re-opening. (Note - the
> DB2 driver supports this).
That does give me an idea. How about we check for markSupported() before
we auto-close the input stream. If it is supported, we issue a warning to
the user that behaviour has changed. If we were to also provide the
InputStream wrapper that ignores the close, we can tell them the one line
fix with the warning.
(We can turn the warning off a release or two later on, once everyone who
needs to has fixed their code)
Does that sound sensible and workable to people?
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Josh Micich <jo...@gmail.com>.
I've done a quick search through a few common JDBC drivers to see how
they handle closing of blob input streams (see attached file).
Closing twice is not a problem for those either.
I didn't think closing the input stream twice was the main worry. My
main concern was the possibility of reading the input stream after
reaching EOF. One way to achieve this would be with a stream that
returned true for markSupported(). reset() could be called at any
time, and the whole stream could be re-read without re-opening. (Note
- the DB2 driver supports this).
So it's clear - there could be code out there that would get broken by
this change, but the fix would be easy.
I would prefer updating the JavaDoc over adding the boolean parameter.
The extra parameter would need to propagate up to all the public
callers of POIFSFileSystem (listed below). Many of these methods
already have been overloaded for other reasons.
I think the greater good (allowing simplification of 99.x% of calling
code) should win out. However, if it is decided not to make the
change, we should add a test case to demonstrate this requirement.
I've attached a attached a version of the junit test which asserts the
inverse (stream never closed).
-josh
-- -- -- --
//Public callers of POIFSFileSystem.<init>(InputStream):
org.apache.poi.hdf.model.HDFObjectFactory.getTypes(InputStream)
org.apache.poi.hdf.model.HDFObjectFactory.HDFObjectFactory(InputStream,
HDFLowLevelParsingListener)
org.apache.poi.hdgf.extractor.VisioTextExtractor.VisioTextExtractor(InputStream)
org.apache.poi.hslf.extractor.PowerPointExtractor.PowerPointExtractor(InputStream)
org.apache.poi.hsmf.MAPIMessage.MAPIMessage(InputStream)
org.apache.poi.hssf.usermodel.HSSFWorkbook.HSSFWorkbook(InputStream, boolean)
org.apache.poi.hwpf.HWPFDocument.verifyAndBuildPOIFS(InputStream)
org.apache.poi.hslf.dev.SlideShowDumper.SlideShowDumper(InputStream)
org.apache.poi.hslf.extractor.QuickButCruddyTextExtractor.QuickButCruddyTextExtractor(InputStream)
org.apache.poi.hslf.HSLFSlideShow.HSLFSlideShow(InputStream)
On Feb 7, 2008 3:48 AM, Nick Burch <ni...@torchbox.com> wrote:
> On Thu, 7 Feb 2008, Yegor Kozlov wrote:
> > With java.io.* streams calling fis.close() twice doesn't do any harm.
> > BUT think of reading via JDBC input streams ( from Oracle or MySQL
> > blobs, etc.). Are you sure they will be happy with closing of already
> > closed stream?
>
> My worry is there will be badly behaved inputstreams out there, which we
> may start breaking. Servlet engines, odd jdbc drivers etc
>
> > What we can do is to create a special constructor:
> > POIFSFileSystem(InputStream stream, boolean autoClose ).
> >
> > autoClose=false by default.
>
> That looks good to me. It makes it clearer to users that autoClose isn't
> automatically done (since there are now two constructors, one which offers
> it at an option), and allows users who really want autoclose to have it
> done for them. All without breaking existing code :)
>
> Nick
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
>
>
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Nick Burch <ni...@torchbox.com>.
On Thu, 7 Feb 2008, Yegor Kozlov wrote:
> With java.io.* streams calling fis.close() twice doesn't do any harm.
> BUT think of reading via JDBC input streams ( from Oracle or MySQL
> blobs, etc.). Are you sure they will be happy with closing of already
> closed stream?
My worry is there will be badly behaved inputstreams out there, which we
may start breaking. Servlet engines, odd jdbc drivers etc
> What we can do is to create a special constructor:
> POIFSFileSystem(InputStream stream, boolean autoClose ).
>
> autoClose=false by default.
That looks good to me. It makes it clearer to users that autoClose isn't
automatically done (since there are now two constructors, one which offers
it at an option), and allows users who really want autoclose to have it
done for them. All without breaking existing code :)
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
Re: Should POIFSFileSystem close its input stream (Buzilla 44366)?
Posted by Yegor Kozlov <ye...@dinom.ru>.
Personally, I think the caller is always responsible for closing the input stream.
With java.io.* streams calling fis.close() twice doesn't do any harm.
BUT think of reading via JDBC input streams ( from Oracle or MySQL
blobs, etc.). Are you sure they will be happy with closing of already
closed stream?
So, I'm -1 to automatically close it in POIFSFileSystem(InputStream
stream).
What we can do is to create a special constructor:
POIFSFileSystem(InputStream stream, boolean autoClose ).
autoClose=false by default.
Use it in your code if you need this "auto-close" behavior
Yegor
> The way POIFSFileSystem is implemented right now, the caller is
> responsible for closing the input stream supplied. During normal
> operation, POIFSFileSystem reads until EOF (see code within
> RawDataBlockList/RawDataBlock). In most cases the input stream is
> being closed by the caller (either explicitly, or implicitly due to
> garbage collection of an input stream local variable).
> POIFSFileSystem could close the input stream, but doesn't.
> I ran into this problem, when a java process held a lock on an XLS
> file long after HSSFWorkbook had finished reading it. Of course the
> solution was simple - just call fis.close(), but I was wondering how
> many other places this problem might exist.
> My suggestion is to have POIFSFileSystem always close the input
> stream, so the caller never has to worry about that detail.
> There is a potential problem with this proposed patch for the unusual
> case where the caller supplies an input stream that can continue to be
> used after reaching EOF. I've never seen such a stream, but any
> arbitrary subclass of InputStream could allow such behaviour. In this
> unusual circumstance however, the caller could wrap the input stream
> in order to trap the close() call.
> Another less likely problem with the patch concerns calling close()
> twice on an input stream. The javadoc has nothing to say about this
> point, but all major subclasses of InputStream have no trouble with
> this.
> So I guess it is a question of whether we want to code "is.close()" in
> a finally block after every call to POIFSFileSystem (and classes that
> utilise it), vs writing wrapper code in the uncommon cases when
> "is.close()" is unwanted.
> All existing junits work in the presence of this new patch. If anyone
> can think of a scenario where this patch (always closing the input
> stream) would be bad, it would be good to see this requirement
> demonstrated as a new junit.
> Either way this gets decided, I would like to update the javadoc.
> It's always good to document who is responsible for closing a stream.
> -josh
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
> For additional commands, e-mail: dev-help@poi.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org