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

[email protected]
Newsgroups gmane.comp.jakarta.slide.devel
Message-ID <[email protected]/bugzilla/>
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: [email protected]
        ReportedBy: [email protected]


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
[email protected]

-- 
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.