Author: veithen
Date: Thu Mar  5 23:14:56 2009
New Revision: 750669

URL: http://svn.apache.org/viewvc?rev=750669&view=rev
Log:
Allow SizeAwareDataSource#getSize() to return -1 if it is unable to determine 
the size. Also added a comment about the expected accurateness of the size 
information.

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/SizeAwareDataSource.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/BufferUtils.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/SizeAwareDataSource.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/SizeAwareDataSource.java?rev=750669&r1=750668&r2=750669&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/SizeAwareDataSource.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/SizeAwareDataSource.java
 Thu Mar  5 23:14:56 2009
@@ -27,15 +27,31 @@
  * Code working with data sources can use this interface to optimize certain 
operations.
  * An example is
  * {...@link 
org.apache.axiom.attachments.impl.BufferUtils#doesDataHandlerExceedLimit(javax.activation.DataHandler,
 int)}.
+ * <p>
+ * Code using this interface should be aware that some implementations may be 
unable to guarantee
+ * 100% accuracy when determining the size of the data source. Situations 
where this can occur
+ * include:
+ * <ul>
+ *   <li>The data source uses a network protocol that allows to get the size 
of the data
+ *       but that doesn't guarantee accurateness.</li>
+ *   <li>Reading the data involves a decoding operation and the length of the 
resulting stream
+ *       can't be determined precisely without performing the decoding 
operation. In this
+ *       case the implementation of this interface may return a value based on 
an estimation.</li>
+ * </ul>
+ * When reading the actual data, the code should always read until the end of 
the stream is
+ * reached (as indicated by the return value of the <code>read</code> methods 
of the 
+ * {...@link java.io.InputStream} class). It must be prepared to reach the end 
of the stream after
+ * a number of bytes that is lower or higher than the value returned by 
{...@link #getSize()}.
  */
 public interface SizeAwareDataSource extends DataSource {
     /**
      * Get the size of the data source.
      * Implementations must return the number of bytes that can be read from
      * the input stream returned by {...@link #getInputStream()} before 
reaching
-     * the end of the stream.
+     * the end of the stream. If the implementation is unable to determine the
+     * size, it must return -1.
      * 
-     * @return the size of the data source
+     * @return the size of the data source or -1 if the size is not known
      */
     long getSize();
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/BufferUtils.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/BufferUtils.java?rev=750669&r1=750668&r2=750669&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/BufferUtils.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/impl/BufferUtils.java
 Thu Mar  5 23:14:56 2009
@@ -279,8 +279,12 @@
         }
         DataSource ds = dh.getDataSource();
         if (ds instanceof SizeAwareDataSource) {
-            return ((SizeAwareDataSource)ds).getSize() > limit ? 1 : 0;
-        } else if (ds instanceof javax.mail.util.ByteArrayDataSource) {
+            long size = ((SizeAwareDataSource)ds).getSize();
+            if (size != -1) {
+                return size > limit ? 1 : 0;
+            }
+        }
+        if (ds instanceof javax.mail.util.ByteArrayDataSource) {
             // Special optimization for JavaMail's ByteArrayDataSource 
(Axiom's ByteArrayDataSource
             // already implements SizeAwareDataSource and doesn't need further 
optimization):
             // we know that ByteArrayInputStream#available() directly returns 
the size of the


Reply via email to