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 2022/11/05 16:24:05 UTC

[ws-axiom] branch master updated: [AXIOM-506] Improve conversion between DataHandler and Blob

This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new 0f064099e [AXIOM-506] Improve conversion between DataHandler and Blob
0f064099e is described below

commit 0f064099ea044fecef7482c33fa8d3ecf6d008d2
Author: Andreas Veithen <an...@gmail.com>
AuthorDate: Sat Nov 5 16:23:54 2022 +0000

    [AXIOM-506] Improve conversion between DataHandler and Blob
    
    - Avoid calls to DataHandler.getDataSource() because for some
    DataHandlers they may have undesired side effects.
    - Delegate DataHandler.writeTo to Blob.writeTo.
---
 .../axiom/util/activation/BlobDataHandler.java     | 44 ++++++++++++++++++++++
 .../axiom/util/activation/DataHandlerUtils.java    |  7 ++--
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/axiom-api/src/main/java/org/apache/axiom/util/activation/BlobDataHandler.java b/axiom-api/src/main/java/org/apache/axiom/util/activation/BlobDataHandler.java
new file mode 100644
index 000000000..60976488c
--- /dev/null
+++ b/axiom-api/src/main/java/org/apache/axiom/util/activation/BlobDataHandler.java
@@ -0,0 +1,44 @@
+/*
+ * 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.activation;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.activation.DataHandler;
+
+import org.apache.axiom.blob.Blob;
+
+final class BlobDataHandler extends DataHandler {
+    private final Blob blob;
+
+    BlobDataHandler(Blob blob) {
+        super(new BlobDataSource(blob, "application/octet-stream"));
+        this.blob = blob;
+    }
+
+    Blob getBlob() {
+        return blob;
+    }
+
+    @Override
+    public void writeTo(OutputStream os) throws IOException {
+        blob.writeTo(os);
+    }
+}
diff --git a/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java b/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
index 4b7e809ee..9cbd50e69 100644
--- a/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
+++ b/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
@@ -78,7 +78,7 @@ public final class DataHandlerUtils {
         if (blob instanceof DataHandlerBlob) {
             return ((DataHandlerBlob)blob).getDataHandler();
         }
-        return new DataHandler(new BlobDataSource(blob, "application/octet-stream"));
+        return new BlobDataHandler(blob);
     }
 
     /**
@@ -89,9 +89,8 @@ public final class DataHandlerUtils {
      * @return a {@link Blob} representing the {@link DataHandler}
      */
     public static Blob toBlob(DataHandler dh) {
-        DataSource ds = dh.getDataSource();
-        if (ds instanceof BlobDataSource) {
-            return ((BlobDataSource)ds).getBlob();
+        if (dh instanceof BlobDataHandler) {
+            return ((BlobDataHandler)dh).getBlob();
         }
         return new DataHandlerBlob(dh);
     }