[prev in list] [next in list] [prev in thread] [next in thread] 

List:       slide-user
Subject:    Streaming a file upload
From:       "Smith, Timothy" <Timothy.Smith () cai ! com>
Date:       2001-06-27 23:50:34
[Download RAW message or body]

Hello All,

We are using a CVS version of Slide from the 19th of June and Tomcat v3.2.2
(with the standard Slide webapp).

We have a query as to how to stream a file to a Slide server using the Slide
Client API.

Our initial long winded approach to the problem was to use the following
approach...

1. Setup a PipedOutputStream and a PipedInputStream.
2. In a separate thread execute the WebdavResource method putMethod(String
path, InputStream in) passing the PipedInputStream as the argument.
3. Begin streaming the file upload request to the PipedOutputStream.

However after looking at the source for the Jakarta Commons-HttpClient we
found that the PutMethod.sendData(InputStream in) simply reads all the
contents of the InputStream into memory and then writes the contents as a
large byte[] chunk.  Therefore the attempt to stream the contents of the
upload was a waste of time.

So what we really need to know is how to use the Slide Client API to stream
a Put command.

I seem to think the problem can be solved by changing the Commons-HttpClient
PutMethod.streamQuery(OutputStream out) code to accept any arbitrary
InputStream to perform the upload with, rather than only a URL or a File.

    /**
     * Stream the body of the query. This function should be used to send
large
     * request bodies.
     */
    public void streamQuery(OutputStream out)
        throws IOException {
        
        InputStream inputStream = null;
        if (file != null) {
            inputStream = new FileInputStream(file);
        } else if (url != null) {
            inputStream = url.openConnection().getInputStream();
        } else {
		inputStream = in; // The input stream passed to the
sendData(InputStream in) method
	  }
        
        byte[] buffer = new byte[4096];
        int nb = 0;
        while (true) {
            nb = inputStream.read(buffer);
            if (nb == -1)
                break;
            out.write(buffer, 0, nb);
        }
        
        inputStream.close();
        
    }

Then change the code Commons-HttpClient PutMethod.sendData(InputStream in)
to the following:

    /**
     * Send the contents of an input stream. The contents will be buffered
into
     * memory. To upload large entities, it is recommended to first buffer
the
     * data into a temporary file, and then send that file.
     */
    public void sendData(InputStream is)
        throws IOException {
        checkNotUsed();
	  this.in = is;

        //byte[] buffer = new byte[4096];
        //ByteArrayOutputStream os = new ByteArrayOutputStream();
        //int nb = 0;
        //while (true) {
        //    nb = is.read(buffer);
        //    if (nb == -1)
        //        break;
        //    os.write(buffer, 0, nb);
        //}
        //data = os.toByteArray();
    }

Cheers,

Tim

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic