Author: nthaker
Date: Wed Apr  9 13:47:32 2008
New Revision: 646524

URL: http://svn.apache.org/viewvc?rev=646524&view=rev
Log:
WSCOMMONS-313
Checking for in.markSupported() and adding test for ByteArrayDataSource.

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

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=646524&r1=646523&r2=646524&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
 Wed Apr  9 13:47:32 2008
@@ -27,6 +27,8 @@
 import java.nio.channels.FileLock;
 
 import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
 
 import org.apache.axiom.om.OMException;
 import org.apache.commons.logging.Log;
@@ -212,18 +214,22 @@
             }
             return -1;
         }
-        InputStream in = getInputStream(dh);
-        if(in == null){
-            if(log.isDebugEnabled()){
-                log.debug("Input Stream is null");
-            }
-            return -1;
-        }
         
+        InputStream in=null;
         //read bytes from input stream to check if 
         //attachment size is greater than the optimized size.        
         int totalRead = 0;
         try{
+            in = getInputStream(dh);
+            if(in.markSupported()){
+                in.mark((int)limit);
+            }
+            if(in == null){
+                if(log.isDebugEnabled()){
+                    log.debug("Input Stream is null");
+                }
+                return -1;
+            }
             do{
                 byte[] buffer = getTempBuffer();
                 int bytesRead = in.read(buffer, 0, BUFFER_LEN);
@@ -231,13 +237,17 @@
                 releaseTempBuffer(buffer);
             }while((limit>totalRead) && (in.available()>0));
             
+            if(in.markSupported()){                
+                in.reset();
+            }
             if(totalRead > limit){
                 if(log.isDebugEnabled()){
                     log.debug("Attachment size greater than limit");
                 }
                 return 1;
             }
-        }catch(IOException e){
+        }catch(Exception e){            
+            log.warn(e.getMessage());
             return -1;
         }
         
@@ -249,11 +259,19 @@
     }
     
     private static java.io.InputStream getInputStream(DataHandler 
dataHandlerObject) throws OMException {
-        InputStream inStream;
+        InputStream inStream = null;
         javax.activation.DataHandler dataHandler =
             (javax.activation.DataHandler) dataHandlerObject;
         try {
-            inStream = dataHandler.getDataSource().getInputStream();
+            DataSource ds = dataHandler.getDataSource();
+            if(ds instanceof FileDataSource){
+                inStream = ds.getInputStream();
+            }else{
+                inStream = dataHandler.getDataSource().getInputStream();
+                if(!inStream.markSupported()){
+                    throw new OMException("Stream does not support mark, 
Cannot read the stream as DataSource will be consumed.");
+                }
+            }
         } catch (IOException e) {
             throw new OMException(
                 "Cannot get InputStream from DataHandler." + e);

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/impl/BufferUtilsTests.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/impl/BufferUtilsTests.java?rev=646524&r1=646523&r2=646524&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/impl/BufferUtilsTests.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/attachments/impl/BufferUtilsTests.java
 Wed Apr  9 13:47:32 2008
@@ -27,6 +27,7 @@
 import javax.activation.DataHandler;
 import javax.activation.FileDataSource;
 import javax.activation.MimeType;
+import javax.mail.util.ByteArrayDataSource;
 
 import junit.framework.TestCase;
 
@@ -106,6 +107,23 @@
         String str = "This is a test String";
         try{
             DataHandler dh = new DataHandler(str, "text/plain");          
+            int unsupported= BufferUtils.doesDataHandlerExceedLimit(dh, 0);
+            assertEquals(unsupported, -1);
+            int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);
+            //Expecting Mark NotSupported
+            assertEquals(doesExceed, -1);
+        }catch(Exception e){
+            e.printStackTrace();
+            fail();
+        }
+        
+    }
+    public void testByteArrayDataSourceBackedDataHandlerExceedLimit(){
+        String str = "This is a test String";
+        byte[] b = str.getBytes();
+        ByteArrayDataSource bads = new ByteArrayDataSource(b, "text/plain");
+        try{
+            DataHandler dh = new DataHandler(bads);          
             int unsupported= BufferUtils.doesDataHandlerExceedLimit(dh, 0);
             assertEquals(unsupported, -1);
             int doesExceed = BufferUtils.doesDataHandlerExceedLimit(dh, 10);



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to