You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by bu...@apache.org on 2007/02/09 00:18:06 UTC

DO NOT REPLY [Bug 41574] New: - putMethod InputStream and OutputStream not closed if exception occurs

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=41574>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=41574

           Summary: putMethod InputStream and OutputStream not closed if
                    exception occurs
           Product: Slide
           Version: 2.1
          Platform: All
        OS/Version: other
            Status: NEW
          Severity: normal
          Priority: P2
         Component: WebDAV client
        AssignedTo: slide-dev@jakarta.apache.org
        ReportedBy: dmx_dawg@hotmail.com


If an exception is thrown between after the input and output streams inStream
and fos are opened, but before they are closed, then the streams will not be
closed.  This error won't blatantly manifest itself on *nix systems, but on
Windows systems, the file being read from by the FileInputStream will be locked
and users will be unable to edit it afterwards (until the Slide client based app
is closed).  There is also the issue of it being a resource leak.

The solution is to put the code that opens the streams in a try block and then
close them in a finally block ensuring they'll be closed even if there is an
exception.

This bug is related to Slide Client bug 40835 in that it's the same general type
of error in both cases.
( http://issues.apache.org/bugzilla/show_bug.cgi?id=40835 ).

FIX is below:

    /**
     * Execute the GET method for the given path.
     *
     * Fixed situation where an exception may cause the input and output
     * streams not to be closed.
     *
     * @param path the server relative path of the resource to get
     * @param file The local file.
     * @return true if the method is succeeded.
     * @exception HttpException
     * @exception IOException
     */
    public boolean getMethod(String path, File file)
        throws HttpException, IOException {

        setClient();
        GetMethod method = new GetMethod(URIUtil.encodePathQuery(path));

        generateTransactionHeader(method);
        int statusCode = client.executeMethod(method);

        setStatusCode(statusCode);

        // get the file only if status is any kind of OK
        if (statusCode >= 200 && statusCode < 300)
        {
            FileOutputStream fos = null;
            InputStream inStream = null;
            try {
                // Do a simple little loop to read the response back into the passed
                // file parameter.
                inStream = method.getResponseBodyAsStream();

                fos = new FileOutputStream(file);
                byte buffer[] = new byte[65535];
                int bytesRead;
                while ((bytesRead = inStream.read(buffer)) >= 0) {
                    fos.write(buffer, 0, bytesRead);
                }

                return true;
            
            } finally {
                if(fos != null)
                    fos.close();
                if(inStream != null)
                    inStream.close();
            }

        } else {
            return false;

        }
   }    


PS:
Sorry to those on the dev mailing list who have already gotten an email from me
regarding this.


Cheers!

Michael N. Christoff
dmx_dawg@hotmail.com

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org


DO NOT REPLY [Bug 41574] - getMethod InputStream and OutputStream not closed if exception occurs

Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=41574>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=41574


antoine@apache.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |2.2




------- Additional Comments From antoine@apache.org  2007-02-10 14:00 -------
Fix committed, thanks very much for your contribution. Antoine

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org


DO NOT REPLY [Bug 41574] - getMethod InputStream and OutputStream not closed if exception occurs

Posted by bu...@apache.org.
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=41574>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=41574


dmx_dawg@hotmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|putMethod InputStream and   |getMethod InputStream and
                   |OutputStream not closed if  |OutputStream not closed if
                   |exception occurs            |exception occurs




------- Additional Comments From dmx_dawg@hotmail.com  2007-02-08 15:23 -------
Sorry, this bug should be renamed 

"getMethod InputStream and OutputStream not closed if exception occurs"

instead of "putMethod ..."

Also in the line where it says:

"This error won't blatantly manifest itself on *nix systems, but on
Windows systems, the file being read from by the FileInputStream ..."

that should be FileOutputStream.

-mike

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org