[Mulgara-svn] r1530 - trunk/src/jar/querylang/java/org/mulgara/protocol/http

pag at mulgara.org pag at mulgara.org
Thu Feb 19 16:49:38 UTC 2009


Author: pag
Date: 2009-02-19 08:49:37 -0800 (Thu, 19 Feb 2009)
New Revision: 1530

Modified:
   trunk/src/jar/querylang/java/org/mulgara/protocol/http/MimeMultiNamedPart.java
Log:
Fixed bug where MIME parts were presumed to be strings. For larger parts they arrive as input streams instead.

Modified: trunk/src/jar/querylang/java/org/mulgara/protocol/http/MimeMultiNamedPart.java
===================================================================
--- trunk/src/jar/querylang/java/org/mulgara/protocol/http/MimeMultiNamedPart.java	2009-02-19 16:44:19 UTC (rev 1529)
+++ trunk/src/jar/querylang/java/org/mulgara/protocol/http/MimeMultiNamedPart.java	2009-02-19 16:49:37 UTC (rev 1530)
@@ -17,6 +17,9 @@
 package org.mulgara.protocol.http;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.CharBuffer;
 
 import javax.activation.DataSource;
 import javax.mail.BodyPart;
@@ -32,6 +35,9 @@
  */
 public class MimeMultiNamedPart extends MimeMultipart {
 
+  /** The number of elements in internal data buffers. */
+  private static final int BUFFER_SIZE = 1024;
+
   /**
    * @param src The data source to retrieve the MIME data from
    * @throws MessagingException If the source cannot be parsed as valid MIME data.
@@ -61,7 +67,8 @@
    */
   public String getParameterString(String param) throws MessagingException, IOException {
     Object obj = getParameter(param);
-    return obj == null ? null : obj.toString();
+    if (obj == null) return null;
+    return toString(obj);
   }
 
 
@@ -129,4 +136,33 @@
     return str;
   }
 
+
+  /**
+   * Gets a string from an object. If the object is a stream, then it reads the stream
+   * otherwise it gets the string form of the object.
+   * @param o The object to convert to a string.
+   * @return The string form of the object, or <code>null</code> if the object could not be read.
+   */
+  private static String toString(Object o) {
+    if (o instanceof InputStream) {
+      CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE);
+      StringBuilder sb = new StringBuilder();
+      InputStreamReader in = new InputStreamReader((InputStream)o);
+      try {
+        while (in.read(buffer) >= 0) {
+          buffer.flip();
+          sb.append(buffer);
+        }
+        o = sb;
+      } catch (IOException e) {
+        o = null;
+      }
+      try {
+        in.close();
+      } catch (IOException e) {
+        // got our data at this point, so ignore
+      }
+    }
+    return o.toString();
+  }
 }




More information about the Mulgara-svn mailing list