Author: scheu
Date: Thu May 15 05:44:24 2008
New Revision: 656634

URL: http://svn.apache.org/viewvc?rev=656634&view=rev
Log:
Improved the TextHelper utility to use the inputstream available() information 
to 
allocate the proper capacity for the StringBuffer.  This prevents unnecessary 
resizing.

I also added two new TextHelper utility methods.  
  * a utility to write the BASE64 data from an InputStream to a StringBuffer.
  * a utility that writes the data from an OMText object to a StringBuffer.
  
I also included a new test, TextHelperTest, to verify.
   

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java?rev=656634&r1=656633&r2=656634&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/TextHelper.java
 Thu May 15 05:44:24 2008
@@ -19,20 +19,76 @@
 
 package org.apache.axiom.om.util;
 
+import org.apache.axiom.om.OMText;
+
+import javax.activation.DataHandler;
+
 import java.io.IOException;
 import java.io.InputStream;
 
 public class TextHelper {
+    
+    /**
+     * @param inStream InputStream
+     * @return Base64 encoded string representint the data in inStream
+     * @throws IOException
+     */
     public static String toString(InputStream inStream) throws IOException {
+        StringBuffer buffer = new StringBuffer();
+        toStringBuffer(inStream, buffer);
+        return buffer.toString();
+    }
+    
+    /**
+     * Append Base64 encoding of the data in the inStream to the specified 
buffer
+     * @param inStream InputStream
+     * @param buffer Buffer
+     * @throws IOException
+     */
+    public static void toStringBuffer(InputStream inStream, StringBuffer 
buffer) throws IOException {
         byte[] data;
-        StringBuffer text = new StringBuffer();
+        
+        int avail = inStream.available();
+        
+        // The Base64 will increase the size by 1.33 + some additional 
+        // space at the data byte[] boundaries.  So a factor of 1.35 is used
+        // to ensure capacity.
+        if (avail > 0) {
+            buffer.ensureCapacity((int) (avail* 1.35) + buffer.length());
+        }
+        
+        
         do {
             data = new byte[1023];
             int len;
             while ((len = inStream.read(data)) > 0) {
-                Base64.encode(data, 0, len, text);
+                Base64.encode(data, 0, len, buffer);
             }
         } while (inStream.available() > 0);
-        return text.toString();
+        return;
+    }
+    
+    /**
+     * Append data in the omText to the specified buffer
+     * @param inStream InputStream
+     * @param buffer Buffer
+     * @throws IOException
+     */
+    public static void toStringBuffer(OMText omText, StringBuffer buffer) 
throws IOException {
+        // If an InputStream is present, stream the BASE64 text to the 
StreamBuffer
+        if (omText.isOptimized()) {
+           Object dh = omText.getDataHandler();
+           if (dh instanceof DataHandler) {
+               InputStream is = ((DataHandler) dh).getInputStream();
+               if (is != null) {
+                   toStringBuffer(is, buffer);
+                   return;
+               }
+           }
+        }
+        
+        // Otherwise append the text
+        buffer.append(omText.getText());
+        return;
     }
 }

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java?rev=656634&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/TextHelperTest.java
 Thu May 15 05:44:24 2008
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.om.util;
+
+import org.apache.axiom.om.AbstractTestCase;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMText;
+
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+/**
+ * Validate TextHelper code
+ */
+public class TextHelperTest extends AbstractTestCase {
+
+    private File file;
+    private FileInputStream fis;
+    private static final long SIZE = 10 * 1024;
+    
+    
+    public TextHelperTest(String testName) {
+        super(testName);
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        file = File.createTempFile("TextHelperTest", "txt");
+        FileOutputStream fos = new FileOutputStream(file);
+        BufferedOutputStream bos = new BufferedOutputStream(fos);
+        for (long i = 0; i < SIZE; i++) {
+            bos.write((byte)(i % 256));
+        }
+        fos.close();
+        fis = new FileInputStream(file);
+        file.deleteOnExit();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        if (file != null) {
+            file.delete();
+        }
+    }
+    
+    public void test_toString() throws Exception {
+        String text = TextHelper.toString(fis);
+        assertTrue(text.length() > SIZE);
+    }
+    
+    public void test_toStringBuffer() throws Exception {
+        StringBuffer buffer = new StringBuffer();
+        TextHelper.toStringBuffer(fis, buffer);
+        assertTrue(buffer.length() > SIZE);
+    }
+    
+    public void test_fromOMText() throws Exception {
+        
+        OMFactory factory = OMAbstractFactory.getOMFactory();
+        FileDataSource fds = new FileDataSource(file);
+        DataHandler dh = new DataHandler(fds);
+        OMText omText = factory.createOMText(dh, true);
+        StringBuffer buffer = new StringBuffer();
+        TextHelper.toStringBuffer(omText, buffer);
+        assertTrue(buffer.length() > SIZE);
+    }
+    
+}
\ No newline at end of file


Reply via email to