You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by ol...@notes.uni-paderborn.de on 2003/03/15 17:40:37 UTC

Problem posting file and deleting it afterwards

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();
  }





Re: Problem posting file and deleting it afterwards

Posted by Oleg Kalnichevski <o....@dplanet.ch>.
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
>