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 re...@apache.org on 2001/01/26 19:55:01 UTC

cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods PutMethod.java

remm        01/01/26 10:55:00

  Modified:    src/webdav/client/src/org/apache/webdav/lib/methods
                        PutMethod.java
  Log:
  - It should now be possible to retry a PUT method.
  - The bahavior of some of the calls was a bit modified :
    - sendData(File) is unchanged.
    - add a new sendData(URL) method, which will upload the contents of the
      resource which can be retrieved at the given URL.
    - sendData(byte[]) is unchanged.
    - sendData(InputStream) will now buffer all the contents of the given InputStream
      into a byte array in memory. To send large amounts of data, I recommend first
      buffering the data to a file, and then sending the file.
  
  Revision  Changes    Path
  1.5       +51 -31    jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PutMethod.java
  
  Index: PutMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PutMethod.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PutMethod.java	2000/12/11 02:06:01	1.4
  +++ PutMethod.java	2001/01/26 18:54:59	1.5
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PutMethod.java,v 1.4 2000/12/11 02:06:01 remm Exp $
  - * $Revision: 1.4 $
  - * $Date: 2000/12/11 02:06:01 $
  + * $Header: /home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PutMethod.java,v 1.5 2001/01/26 18:54:59 remm Exp $
  + * $Revision: 1.5 $
  + * $Date: 2001/01/26 18:54:59 $
    *
    * ====================================================================
    *
  @@ -63,8 +63,14 @@
   
   package org.apache.webdav.lib.methods;
   
  -import java.io.*;
  -import java.util.*;
  +import java.io.IOException;
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.InputStream;
  +import java.io.OutputStream;
  +import java.io.ByteArrayOutputStream;
  +import java.net.URL;
  +import java.net.URLConnection;
   import org.apache.webdav.lib.State;
   import org.apache.webdav.lib.Header;
   import org.apache.webdav.lib.WebdavStatus;
  @@ -103,21 +109,23 @@
       
       
       /**
  -     * For now, the data to be sent is loaded into memory. That will break if
  -     * uploading of large files is attempted. I'll fix that eventually and use
  -     * streams, but I need chunking on output to work (it does), AND chunking
  -     * on input to work on the server side (it doesn't - Catalina doesn't 
  -     * handle that yet).
  +     * Send byte buffer.
        */
       private byte[] data = null;
       
       
       /**
  -     * Input stream to the data.
  +     * Send file contents.
        */
  -    private InputStream is = null;
  +    private File file = null;
       
       
  +    /**
  +     * Set content from URL.
  +     */
  +    private URL url = null;
  +    
  +    
       // --------------------------------------------------------- Public Methods
       
       
  @@ -127,12 +135,21 @@
       public void sendData(File file)
           throws IOException {
           checkNotUsed();
  -        sendData(new FileInputStream(file));
  -        //setHeader("Content-Length", new Long(file.length()).toString());
  +        this.file = file;
       }
       
       
       /**
  +     * Send the contents of the resource at the specified URL.
  +     */
  +    public void sendData(URL url)
  +        throws IOException {
  +        checkNotUsed();
  +        this.url = url;
  +    }
  +    
  +    
  +    /**
        * Send the contents of a byte array.
        */
       public void sendData(byte[] data) {
  @@ -151,11 +168,13 @@
       
       
       /**
  -     * Send the contents of an input stream.
  +     * 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();
           byte[] buffer = new byte[4096];
           ByteArrayOutputStream os = new ByteArrayOutputStream();
           int nb = 0;
  @@ -166,9 +185,6 @@
               os.write(buffer, 0, nb);
           }
           data = os.toByteArray();
  -        */
  -        checkNotUsed();
  -        this.is = is;
       }
       
       
  @@ -182,7 +198,7 @@
        * @return boolean True if the content is avalable in an InputStream
        */
       public boolean isStreamedQuery() {
  -        return (is != null);
  +        return ((file != null) || (url != null));
       }
       
       
  @@ -193,14 +209,8 @@
       public void recycle() {
           super.recycle();
           data = null;
  -        if (is != null) {
  -            try {
  -                is.close();
  -            } catch (IOException e) {
  -                // Ignore
  -            }
  -        }
  -        is = null;
  +        url = null;
  +        file = null;
       }
       
       
  @@ -210,10 +220,11 @@
        * @return String query
        */
       public String generateQuery() {
  -        if (data == null)
  +        if (data == null) {
               return "";
  -        else
  +        } else {
               return new String(data);
  +        }
       }
       
       
  @@ -224,14 +235,23 @@
       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();
  +        }
  +        
           byte[] buffer = new byte[4096];
           int nb = 0;
           while (true) {
  -            nb = is.read(buffer);
  +            nb = inputStream.read(buffer);
               if (nb == -1)
                   break;
               out.write(buffer, 0, nb);
           }
  +        
  +        inputStream.close();
           
       }
       
  
  
  

Re: cvs commit: jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods PutMethod.java

Posted by "Park, Sung-Gu" <je...@thinkfree.com>.
Very good.  I also felt the need  for that functions.  ^^

+1.

----- Original Message -----
From: <re...@apache.org>
To: <ja...@apache.org>
Sent: Saturday, January 27, 2001 3:55 AM
Subject: cvs commit:
jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods
PutMethod.java


| remm        01/01/26 10:55:00
|
|   Modified:    src/webdav/client/src/org/apache/webdav/lib/methods
|                         PutMethod.java
|   Log:
|   - It should now be possible to retry a PUT method.
|   - The bahavior of some of the calls was a bit modified :
|     - sendData(File) is unchanged.
|     - add a new sendData(URL) method, which will upload the contents of
the
|       resource which can be retrieved at the given URL.
|     - sendData(byte[]) is unchanged.
|     - sendData(InputStream) will now buffer all the contents of the given
InputStream
|       into a byte array in memory. To send large amounts of data, I
recommend first
|       buffering the data to a file, and then sending the file.
|
|   Revision  Changes    Path
|   1.5       +51 -31
jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/PutMethod.
java
|
|   Index: PutMethod.java
|   ===================================================================
|   RCS file:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/
PutMethod.java,v
|   retrieving revision 1.4
|   retrieving revision 1.5
|   diff -u -r1.4 -r1.5
|   --- PutMethod.java 2000/12/11 02:06:01 1.4
|   +++ PutMethod.java 2001/01/26 18:54:59 1.5
|   @@ -1,7 +1,7 @@
|    /*
|   - * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/
PutMethod.java,v 1.4 2000/12/11 02:06:01 remm Exp $
|   - * $Revision: 1.4 $
|   - * $Date: 2000/12/11 02:06:01 $
|   + * $Header:
/home/cvs/jakarta-slide/src/webdav/client/src/org/apache/webdav/lib/methods/
PutMethod.java,v 1.5 2001/01/26 18:54:59 remm Exp $
|   + * $Revision: 1.5 $
|   + * $Date: 2001/01/26 18:54:59 $
|     *
|     * ====================================================================
|     *
|   @@ -63,8 +63,14 @@
|
|    package org.apache.webdav.lib.methods;
|
|   -import java.io.*;
|   -import java.util.*;
|   +import java.io.IOException;
|   +import java.io.File;
|   +import java.io.FileInputStream;
|   +import java.io.InputStream;
|   +import java.io.OutputStream;
|   +import java.io.ByteArrayOutputStream;
|   +import java.net.URL;
|   +import java.net.URLConnection;
|    import org.apache.webdav.lib.State;
|    import org.apache.webdav.lib.Header;
|    import org.apache.webdav.lib.WebdavStatus;
|   @@ -103,21 +109,23 @@
|
|
|        /**
|   -     * For now, the data to be sent is loaded into memory. That will
break if
|   -     * uploading of large files is attempted. I'll fix that eventually
and use
|   -     * streams, but I need chunking on output to work (it does), AND
chunking
|   -     * on input to work on the server side (it doesn't - Catalina
doesn't
|   -     * handle that yet).
|   +     * Send byte buffer.
|         */
|        private byte[] data = null;
|
|
|        /**
|   -     * Input stream to the data.
|   +     * Send file contents.
|         */
|   -    private InputStream is = null;
|   +    private File file = null;
|
|
|   +    /**
|   +     * Set content from URL.
|   +     */
|   +    private URL url = null;
|   +
|   +
|        // --------------------------------------------------------- Public
Methods
|
|
|   @@ -127,12 +135,21 @@
|        public void sendData(File file)
|            throws IOException {
|            checkNotUsed();
|   -        sendData(new FileInputStream(file));
|   -        file://setHeader("Content-Length", new
Long(file.length()).toString());
|   +        this.file = file;
|        }
|
|
|        /**
|   +     * Send the contents of the resource at the specified URL.
|   +     */
|   +    public void sendData(URL url)
|   +        throws IOException {
|   +        checkNotUsed();
|   +        this.url = url;
|   +    }
|   +
|   +
|   +    /**
|         * Send the contents of a byte array.
|         */
|        public void sendData(byte[] data) {
|   @@ -151,11 +168,13 @@
|
|
|        /**
|   -     * Send the contents of an input stream.
|   +     * 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();
|            byte[] buffer = new byte[4096];
|            ByteArrayOutputStream os = new ByteArrayOutputStream();
|            int nb = 0;
|   @@ -166,9 +185,6 @@
|                os.write(buffer, 0, nb);
|            }
|            data = os.toByteArray();
|   -        */
|   -        checkNotUsed();
|   -        this.is = is;
|        }
|
|
|   @@ -182,7 +198,7 @@
|         * @return boolean True if the content is avalable in an
InputStream
|         */
|        public boolean isStreamedQuery() {
|   -        return (is != null);
|   +        return ((file != null) || (url != null));
|        }
|
|
|   @@ -193,14 +209,8 @@
|        public void recycle() {
|            super.recycle();
|            data = null;
|   -        if (is != null) {
|   -            try {
|   -                is.close();
|   -            } catch (IOException e) {
|   -                // Ignore
|   -            }
|   -        }
|   -        is = null;
|   +        url = null;
|   +        file = null;
|        }
|
|
|   @@ -210,10 +220,11 @@
|         * @return String query
|         */
|        public String generateQuery() {
|   -        if (data == null)
|   +        if (data == null) {
|                return "";
|   -        else
|   +        } else {
|                return new String(data);
|   +        }
|        }
|
|
|   @@ -224,14 +235,23 @@
|        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();
|   +        }
|   +
|            byte[] buffer = new byte[4096];
|            int nb = 0;
|            while (true) {
|   -            nb = is.read(buffer);
|   +            nb = inputStream.read(buffer);
|                if (nb == -1)
|                    break;
|                out.write(buffer, 0, nb);
|            }
|   +
|   +        inputStream.close();
|
|        }
|
|
|
|
|