You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2012/11/12 23:16:29 UTC
svn commit: r1408474 - in /webservices/commons/trunk/modules/axiom/modules:
axiom-api/src/main/java/org/apache/axiom/util/base64/
axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/
axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-testsu...
Author: veithen
Date: Mon Nov 12 22:16:28 2012
New Revision: 1408474
URL: http://svn.apache.org/viewvc?rev=1408474&view=rev
Log:
AXIOM-442: Fixed a NullPointerException in OMText#getTextCharacters().
Added:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java (with props)
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java (with props)
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java
webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java?rev=1408474&r1=1408473&r2=1408474&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/Base64Utils.java Mon Nov 12 22:16:28 2012
@@ -54,8 +54,20 @@ public class Base64Utils {
return (unencodedSize+2) / 3 * 4;
}
+ private static int getBufferSize(DataHandler dh) {
+ long size = DataSourceUtils.getSize(dh.getDataSource());
+ if (size == -1) {
+ // Use a reasonable default capacity.
+ return 4096;
+ } else if (size > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("DataHandler is too large to encode to string");
+ } else {
+ return getEncodedSize((int)size);
+ }
+ }
+
/**
- * Get a base64 representation of the content of a given {@link DataHandler}.
+ * Get a base64 representation of the content of a given {@link DataHandler} as a string.
* This method will try to carry out the encoding operation in the most efficient way.
*
* @param dh the data handler with the content to encode
@@ -63,16 +75,7 @@ public class Base64Utils {
* @throws IOException if an I/O error occurs when reading the content of the data handler
*/
public static String encode(DataHandler dh) throws IOException {
- long size = DataSourceUtils.getSize(dh.getDataSource());
- StringBuilder buffer;
- if (size == -1) {
- // Use a reasonable default capacity (better than the default of 16).
- buffer = new StringBuilder(4096);
- } else if (size > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("DataHandler is too large to encode to string");
- } else {
- buffer = new StringBuilder(getEncodedSize((int)size));
- }
+ StringBuilder buffer = new StringBuilder(getBufferSize(dh));
Base64EncodingStringBufferOutputStream out = new Base64EncodingStringBufferOutputStream(buffer);
// Always prefer writeTo, because getInputStream will create a thread and a pipe if
// the DataHandler was constructed using an object instead of a DataSource
@@ -81,6 +84,22 @@ public class Base64Utils {
return buffer.toString();
}
+ /**
+ * Get a base64 representation of the content of a given {@link DataHandler} as a char array.
+ * This method will try to carry out the encoding operation in the most efficient way.
+ *
+ * @param dh the data handler with the content to encode
+ * @return the base64 encoded content
+ * @throws IOException if an I/O error occurs when reading the content of the data handler
+ */
+ public static char[] encodeToCharArray(DataHandler dh) throws IOException {
+ NoCopyCharArrayWriter buffer = new NoCopyCharArrayWriter(getBufferSize(dh));
+ Base64EncodingWriterOutputStream out = new Base64EncodingWriterOutputStream(buffer);
+ dh.writeTo(out);
+ out.complete();
+ return buffer.toCharArray();
+ }
+
private static int decode0(char[] ibuf, byte[] obuf, int wp) {
int outlen = 3;
if (ibuf[3] == S_BASE64PAD)
Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java?rev=1408474&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java Mon Nov 12 22:16:28 2012
@@ -0,0 +1,36 @@
+/*
+ * 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.CharArrayWriter;
+
+/**
+ * Modified {@link CharArrayWriter} that returns the internal buffer (instead of a copy) if the
+ * buffer has just the right size, i.e. if the size of the buffer is equal to the number of
+ * characters that have been written.
+ */
+class NoCopyCharArrayWriter extends CharArrayWriter {
+ NoCopyCharArrayWriter(int expectedSize) {
+ super(expectedSize);
+ }
+
+ public char[] toCharArray() {
+ return count == buf.length ? buf : super.toCharArray();
+ }
+}
Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/base64/NoCopyCharArrayWriter.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java?rev=1408474&r1=1408473&r2=1408474&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/TextNodeImpl.java Mon Nov 12 22:16:28 2012
@@ -269,7 +269,17 @@ public abstract class TextNodeImpl exten
}
public char[] getTextCharacters() {
- return charArray != null ? charArray : this.textValue.toCharArray();
+ if (charArray != null) {
+ return charArray;
+ } else if (textValue != null) {
+ return textValue.toCharArray();
+ } else {
+ try {
+ return Base64Utils.encodeToCharArray((DataHandler)getDataHandler());
+ } catch (IOException ex) {
+ throw new OMException(ex);
+ }
+ }
}
public boolean isCharacters() {
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java?rev=1408474&r1=1408473&r2=1408474&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMTextImpl.java Mon Nov 12 22:16:28 2012
@@ -255,7 +255,17 @@ public class OMTextImpl extends OMLeafNo
}
public char[] getTextCharacters() {
- return charArray != null ? charArray : value.toCharArray();
+ if (charArray != null) {
+ return charArray;
+ } else if (value != null) {
+ return value.toCharArray();
+ } else {
+ try {
+ return Base64Utils.encodeToCharArray((DataHandler)getDataHandler());
+ } catch (IOException ex) {
+ throw new OMException(ex);
+ }
+ }
}
public boolean isCharacters() {
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1408474&r1=1408473&r2=1408474&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java Mon Nov 12 22:16:28 2012
@@ -475,6 +475,7 @@ public class OMTestSuiteBuilder extends
addTest(new org.apache.axiom.ts.om.pi.TestDigest(metaFactory));
addTest(new org.apache.axiom.ts.om.text.TestBase64Streaming(metaFactory));
addTest(new org.apache.axiom.ts.om.text.TestDigest(metaFactory));
+ addTest(new org.apache.axiom.ts.om.text.TestGetTextCharactersFromDataHandler(metaFactory));
Method[] methods = AXIOMXPathTestCase.class.getMethods();
for (int i=0; i<methods.length; i++) {
String methodName = methods[i].getName();
Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java?rev=1408474&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java Mon Nov 12 22:16:28 2012
@@ -0,0 +1,45 @@
+/*
+ * 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.ts.om.text;
+
+import javax.activation.DataHandler;
+
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.util.base64.Base64Utils;
+
+/**
+ * Tests that {@link OMText#getTextCharacters()} returns the expected result for an {@link OMText}
+ * node backed by a {@link DataHandler}. This is a regression test for <a
+ * href="https://issues.apache.org/jira/browse/AXIOM-442">AXIOM-442</a>.
+ */
+public class TestGetTextCharactersFromDataHandler extends AxiomTestCase {
+ public TestGetTextCharactersFromDataHandler(OMMetaFactory metaFactory) {
+ super(metaFactory);
+ }
+
+ protected void runTest() throws Throwable {
+ DataHandler dh = new DataHandler("test content", "text/plain; charset=utf-8");
+ OMText text = metaFactory.getOMFactory().createOMText(dh, true);
+ char[] chars = text.getTextCharacters();
+ byte[] decoded = Base64Utils.decode(chars, 0, chars.length);
+ assertEquals("test content", new String(decoded, "utf-8"));
+ }
+}
Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/text/TestGetTextCharactersFromDataHandler.java
------------------------------------------------------------------------------
svn:eol-style = native