You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2010/05/15 02:34:38 UTC

svn commit: r944536 - in /webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom: ext/stax/ om/impl/ util/base64/ util/stax/

Author: veithen
Date: Sat May 15 00:34:38 2010
New Revision: 944536

URL: http://svn.apache.org/viewvc?rev=944536&view=rev
Log:
XMLStreamReaderUtils#getDataHandlerFromElement: avoid unnecessary conversions and buffering when decoding base64.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/SwitchingWrapper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Constants.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/WrappedTextNodeStreamReader.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java?rev=944536&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java Sat May 15 00:34:38 2010
@@ -0,0 +1,68 @@
+/*
+ * 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.ext.stax;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.xml.stream.XMLStreamException;
+
+/**
+ * Optional interface implemented by {@link javax.xml.stream.XMLStreamReader}
+ * implementations that support writing character data directly to a
+ * {@link Writer}.
+ */
+public interface CharacterDataReader {
+    /**
+     * The name of the property used to look up this extension interface from a
+     * {@link javax.xml.stream.XMLStreamReader} implementation.
+     */
+    String PROPERTY = CharacterDataReader.class.getName();
+    
+    /**
+     * Output the character data for the current event to the given writer. In
+     * general, the implementation behaves such that
+     * <code>reader.writeTextTo(writer)</code> has the same effect as
+     * <code>writer.write(reader.getText())</code>. However, the implementation
+     * MAY choose to split the character data differently. E.g. it MAY write the
+     * character data in multiple chunks or it MAY choose to process more
+     * character data in a single event than would be returned by
+     * {@link javax.xml.stream.XMLStreamReader#getText()}. Therefore, using this
+     * method together with {@link javax.xml.stream.XMLStreamReader#getText()},
+     * {@link javax.xml.stream.XMLStreamReader#getTextCharacters()},
+     * {@link javax.xml.stream.XMLStreamReader#getTextStart()},
+     * {@link javax.xml.stream.XMLStreamReader#getTextLength()} or
+     * {@link javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)}
+     * is not supported and may lead to undefined results.
+     * <p>
+     * The implementation SHOULD avoid any unnecessary conversions between
+     * strings and character arrays.
+     * 
+     * @param writer
+     *            the writer to write the character data to
+     * @throws XMLStreamException
+     *             if the underlying XML source is not well-formed
+     * @throws IOException
+     *             if an I/O error occurs when writing the character data
+     * @throws IllegalStateException
+     *             if this state is not a valid text state.
+     */
+    void writeTextTo(Writer writer) throws XMLStreamException, IOException;
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/ext/stax/CharacterDataReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/SwitchingWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/SwitchingWrapper.java?rev=944536&r1=944535&r2=944536&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/SwitchingWrapper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/SwitchingWrapper.java Sat May 15 00:34:38 2010
@@ -19,6 +19,8 @@
 
 package org.apache.axiom.om.impl;
 
+import java.io.IOException;
+import java.io.Writer;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
@@ -34,6 +36,7 @@ import javax.xml.stream.XMLStreamConstan
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.axiom.ext.stax.CharacterDataReader;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
 import org.apache.axiom.om.OMAttribute;
@@ -55,6 +58,7 @@ import org.apache.axiom.om.impl.exceptio
 import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
 import org.apache.axiom.util.stax.AbstractXMLStreamReader;
 import org.apache.axiom.util.stax.DummyLocation;
+import org.apache.axiom.util.stax.XMLStreamReaderUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -62,7 +66,7 @@ import org.apache.commons.logging.LogFac
  * Class used internally by {@link OMStAXWrapper}.
  */
 class SwitchingWrapper extends AbstractXMLStreamReader
-    implements DataHandlerReader, XMLStreamConstants {
+    implements DataHandlerReader, CharacterDataReader, XMLStreamConstants {
     
     private static final Log log = LogFactory.getLog(SwitchingWrapper.class);
     private static boolean DEBUG_ENABLED = log.isDebugEnabled();
@@ -432,6 +436,31 @@ class SwitchingWrapper extends AbstractX
         }
     }
 
+    public void writeTextTo(Writer writer) throws XMLStreamException, IOException {
+        if (parser != null) {
+            XMLStreamReaderUtils.writeTextTo(parser, writer);
+        } else {
+            switch (currentEvent) {
+                case CHARACTERS:
+                case CDATA:
+                case SPACE:
+                    OMText text = (OMText)lastNode;
+                    if (text.isCharacters()) {
+                        writer.write(text.getTextCharacters());
+                    } else {
+                        // TODO: we should cover the binary case in an optimized way
+                        writer.write(text.getText());
+                    }
+                    break;
+                case COMMENT:
+                    writer.write(((OMComment)lastNode).getValue());
+                    break;
+                default:
+                    throw new IllegalStateException();
+            }
+        }
+    }
+
     /**
      * @return Returns int.
      * @see javax.xml.stream.XMLStreamReader#getEventType()
@@ -1010,6 +1039,9 @@ class SwitchingWrapper extends AbstractX
         if (value != null) {
             return value;
         }
+        if (CharacterDataReader.PROPERTY.equals(s)) {
+            return this;
+        }
         if (parser != null) {
             return parser.getProperty(s);
         }

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java?rev=944536&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java Sat May 15 00:34:38 2010
@@ -0,0 +1,126 @@
+/*
+ * 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.util.base64;
+
+import java.io.IOException;
+import java.io.Writer;
+
+/**
+ * Base class for {@link Writer} implementations that decode data in base64.
+ */
+public abstract class AbstractBase64DecodingWriter extends Writer {
+    private final char[] in = new char[4];
+    private final byte[] out = new byte[3];
+    private int rest; // Number of characters remaining in the in buffer
+
+    public final void write(char[] cbuf, int off, int len) throws IOException {
+        if (rest > 0) {
+            while (len > 0 && rest < 4) {
+                in[rest++] = cbuf[off++];
+                len--;
+            }
+            if (rest == 4) {
+                decode(in, 0);
+                rest = 0;
+            }
+        }
+        while (len >= 4) {
+            decode(cbuf, off);
+            off += 3;
+            len -= 3;
+        }
+        while (len > 0) {
+            in[rest++] = cbuf[off++];
+            len--;
+        }
+    }
+
+    public final void write(String str, int off, int len) throws IOException {
+        while (len > 0) {
+            write(str.charAt(off));
+            off++;
+            len--;
+        }
+    }
+
+    public final void write(int c) throws IOException {
+        in[rest++] = (char)c;
+        if (rest == 4) {
+            decode(in, 0);
+            rest = 0;
+        }
+    }
+
+    private int decode(char c) throws IOException {
+        if (c == Base64Constants.S_BASE64PAD) {
+            return -1;
+        } else if (c < Base64Constants.S_DECODETABLE.length) {
+            int result = Base64Constants.S_DECODETABLE[c];
+            if (result != Byte.MAX_VALUE) {
+                return result;
+            }
+        }
+        throw new IOException("Invalid base64 char '" + c + "'");
+    }
+    
+    private void decode(char[] data, int off) throws IOException {
+        int outlen = 3;
+        if (data[off+3] == Base64Constants.S_BASE64PAD) {
+            outlen = 2;
+        }
+        if (data[off+2] == Base64Constants.S_BASE64PAD) {
+            outlen = 1;
+        }
+        int b0 = decode(data[off]);
+        int b1 = decode(data[off+1]);
+        int b2 = decode(data[off+2]);
+        int b3 = decode(data[off+3]);
+        switch (outlen) {
+            case 1:
+                out[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                break;
+            case 2:
+                out[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                out[1] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
+                break;
+            case 3:
+                out[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
+                out[1] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
+                out[2] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
+        }
+        doWrite(out, outlen);
+    }
+    
+    /**
+     * Write base64 decoded data. If necessary, the implementation should
+     * accumulate the data in a buffer before writing it to the underlying
+     * stream. The maximum number of bytes passed to this method in a single
+     * call is 3.
+     * 
+     * @param b
+     *            the byte array containing the data to write, starting at
+     *            offset 0
+     * @param len
+     *            the number of bytes to write
+     * @throws IOException
+     *             if an I/O error occurs
+     */
+    protected abstract void doWrite(byte[] b, int len) throws IOException;
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/AbstractBase64DecodingWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Constants.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Constants.java?rev=944536&r1=944535&r2=944536&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Constants.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Constants.java Sat May 15 00:34:38 2010
@@ -20,13 +20,25 @@
 package org.apache.axiom.util.base64;
 
 // For internal use only
-interface Base64Constants {
-    byte[] S_BASE64CHAR = { 'A', 'B', 'C', 'D', 'E', 'F',
+class Base64Constants {
+    static final byte[] S_BASE64CHAR = { 'A', 'B', 'C', 'D', 'E', 'F',
         'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
         'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
         't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
         '6', '7', '8', '9', '+', '/' };
 
-    byte S_BASE64PAD = '=';
+    static final byte S_BASE64PAD = '=';
+
+    static final byte[] S_DECODETABLE = new byte[128];
+    
+    static {
+        for (int i = 0; i < S_DECODETABLE.length; i++) {
+            S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
+        }
+        for (int i = 0; i < S_BASE64CHAR.length; i++) {
+            // 0 to 63
+            S_DECODETABLE[S_BASE64CHAR[i]] = (byte) i;
+        }
+    }
 }

Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java?rev=944536&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java Sat May 15 00:34:38 2010
@@ -0,0 +1,47 @@
+/*
+ * 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.util.base64;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * {@link java.io.Writer} implementation that decodes base64 data and writes it
+ * to a an {@link OutputStream}.
+ */
+public class Base64DecodingOutputStreamWriter extends AbstractBase64DecodingWriter {
+    private final OutputStream stream;
+
+    public Base64DecodingOutputStreamWriter(OutputStream stream) {
+        this.stream = stream;
+    }
+
+    protected void doWrite(byte[] b, int len) throws IOException {
+        stream.write(b, 0, len);
+    }
+
+    public void flush() throws IOException {
+        stream.flush();
+    }
+
+    public void close() throws IOException {
+        stream.close();
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64DecodingOutputStreamWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/WrappedTextNodeStreamReader.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/WrappedTextNodeStreamReader.java?rev=944536&r1=944535&r2=944536&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/WrappedTextNodeStreamReader.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/WrappedTextNodeStreamReader.java Sat May 15 00:34:38 2010
@@ -64,6 +64,7 @@ import org.apache.axiom.util.namespace.M
  * "parser" is not coalescing.
  * 
  */
+// TODO: this is a good candidate to implement the CharacterDataReader interface
 public class WrappedTextNodeStreamReader implements XMLStreamReader {
     /**
      * The qualified name of the wrapper element.

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java?rev=944536&r1=944535&r2=944536&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java Sat May 15 00:34:38 2010
@@ -19,14 +19,21 @@
 
 package org.apache.axiom.util.stax;
 
+import java.io.IOException;
+import java.io.Writer;
+
 import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
 import org.apache.axiom.attachments.ByteArrayDataSource;
+import org.apache.axiom.ext.stax.CharacterDataReader;
 import org.apache.axiom.ext.stax.datahandler.DataHandlerReader;
-import org.apache.axiom.util.base64.Base64Utils;
+import org.apache.axiom.util.base64.Base64DecodingOutputStreamWriter;
+import org.apache.axiom.util.blob.BlobDataSource;
+import org.apache.axiom.util.blob.MemoryBlob;
+import org.apache.axiom.util.blob.WritableBlob;
 import org.apache.axiom.util.stax.wrapper.XMLStreamReaderContainer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -71,48 +78,74 @@ public class XMLStreamReaderUtils {
     public static DataHandler getDataHandlerFromElement(XMLStreamReader reader)
             throws XMLStreamException {
         
+        int event = reader.next();
+        if (event == XMLStreamConstants.END_ELEMENT) {
+            // This means that the element is actually empty -> return empty DataHandler
+            return new DataHandler(new ByteArrayDataSource(new byte[0]));
+        } else if (event != XMLStreamConstants.CHARACTERS) {
+            throw new XMLStreamException("Expected a CHARACTER event");
+        }
         DataHandlerReader dhr = getDataHandlerReader(reader);
-        String base64;
-        if (dhr == null) {
-            // In this case the best way to get the content of the element is using
-            // the getElementText method
-            base64 = reader.getElementText();
+        if (dhr != null && dhr.isBinary()) {
+            DataHandler dh = dhr.getDataHandler();
+            reader.next();
+            return dh;
         } else {
-            int event = reader.next();
-            if (event == XMLStreamConstants.END_ELEMENT) {
-                // This means that the element is actually empty -> return empty DataHandler
-                return new DataHandler(new ByteArrayDataSource(new byte[0]));
-            } else if (event != XMLStreamConstants.CHARACTERS) {
-                throw new XMLStreamException("Expected a CHARACTER event");
-            }
-            if (dhr.isBinary()) {
-                DataHandler dh = dhr.getDataHandler();
-                reader.next();
-                return dh;
-            }
-            base64 = reader.getText();
-            StringBuffer buff = null;
-            // Take into account that in non coalescing mode, there may be additional
-            // CHARACTERS events
-            loop: while (true) {
-                switch (reader.next()) {
-                    case XMLStreamConstants.CHARACTERS:
-                        if (buff == null) {
-                            buff = new StringBuffer(base64);
-                        }
-                        buff.append(reader.getText());
-                        break;
-                    case XMLStreamConstants.END_ELEMENT:
-                        break loop;
-                    default:
-                        throw new XMLStreamException("Expected a CHARACTER event");
+            WritableBlob blob = new MemoryBlob();
+            Writer out = new Base64DecodingOutputStreamWriter(blob.getOutputStream());
+            try {
+                writeTextTo(reader, out);
+                // Take into account that in non coalescing mode, there may be additional
+                // CHARACTERS events
+                loop: while (true) {
+                    switch (reader.next()) {
+                        case XMLStreamConstants.CHARACTERS:
+                            writeTextTo(reader, out);
+                            break;
+                        case XMLStreamConstants.END_ELEMENT:
+                            break loop;
+                        default:
+                            throw new XMLStreamException("Expected a CHARACTER event");
+                    }
                 }
+                out.close();
+            } catch (IOException ex) {
+                throw new XMLStreamException("Error during base64 decoding", ex);
             }
-            if (buff != null) {
-                base64 = buff.toString();
-            }
+            return new DataHandler(new BlobDataSource(blob, "application/octet-string"));
+        }
+    }
+    
+    /**
+     * Get the character data for the current event from the given reader and
+     * write it to the given writer. The method will try to figure out the most
+     * efficient way to copy the data without unnecessary buffering or
+     * conversions between strings and character arrays.
+     * 
+     * @param reader
+     *            the reader to get the character data from
+     * @param writer
+     *            the writer to write the character data to
+     * @throws XMLStreamException
+     *             if the underlying XML source is not well-formed
+     * @throws IOException
+     *             if an I/O error occurs when writing the character data
+     * @throws IllegalStateException
+     *             if this state is not a valid text state.
+     * @see CharacterDataReader
+     */
+    public static void writeTextTo(XMLStreamReader reader, Writer writer) throws XMLStreamException, IOException {
+        CharacterDataReader cdataReader;
+        try {
+            cdataReader = (CharacterDataReader)reader.getProperty(CharacterDataReader.PROPERTY);
+        } catch (IllegalArgumentException ex) {
+            cdataReader = null;
+        }
+        if (cdataReader != null) {
+            cdataReader.writeTextTo(writer);
+        } else {
+            writer.write(reader.getText());
         }
-        return new DataHandler(new ByteArrayDataSource(Base64Utils.decode(base64)));
     }
     
     /**