hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <o.kalnichev...@dplanet.ch>
Subject Re: Problem posting file and deleting it afterwards
Date Sun, 16 Mar 2003 12:02:15 GMT
Olaf,
The bug has been already fixed no that long ago. Thanks for pointing it
out, anyways, as it made me think that the code should probably be even
more protective:

    protected void sendData(OutputStream out) throws IOException {
        LOG.trace("enter sendData(OutputStream out)");
        if (lengthOfData() == 0) {
            
            // this file contains no data, so there is nothing to send.
            // we don't want to create a zero length buffer as this will
            // cause an infinite loop when reading.
            LOG.debug("No data to send.");
            return;
        }
        
        byte[] tmp = new byte[4096];
        InputStream instream = source.createInputStream();
        try {
            int len;
            while ((len = instream.read(tmp)) >= 0) {
                out.write(tmp, 0, len);
            }
        } finally {
            // we're done with the stream, close it
            instream.close();
        }
    }

Cheers

Oleg  

On Sat, 2003-03-15 at 17:40, olaf.hahnl@notes.uni-paderborn.de wrote:
> I send a file on the harddisk using class "FilePart" and everything works
> fine.
> But it is just a temporary file which should be deleted afterwards sending
> it, but File.delete() fails and the file remains on the disk (based on
> alpha 3). As far as I found out, the method "sendData" creates an
> inputStream from the file, but did not close it at any time, so the file is
> still locked after sending. So my fix is to close this InputStream after
> all data is send (line marked with +). I do not know if this has any side
> effects or other problems, but my problem would be fixed.
> 
> What do you think?
> 
> Regards,
> Olaf
> 
> 
>    protected void sendData(OutputStream out) throws IOException {
>       LOG.trace("enter sendData(OutputStream out)");
>       if (lengthOfData() == 0) {
> 
>        // this file contains no data, so there is nothing to send.
>        // we don't want to create a zero length buffer as this will
>        // cause an infinite loop when reading.
>        LOG.debug("No data to send.");
>        return;
>      }
> 
>      byte[] tmp = new byte[4096];
>      InputStream instream = source.createInputStream();
> 
>      int len;
>      while ((len = instream.read(tmp)) >= 0) {
>         out.write(tmp, 0, len);
>      }
> 
> +   instream.close();
>   }
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org
> 


Mime
View raw message