abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r555879 - /incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
Date Fri, 13 Jul 2007 05:38:59 GMT
Author: jmsnell
Date: Thu Jul 12 22:38:58 2007
New Revision: 555879

URL: http://svn.apache.org/viewvc?view=rev&rev=555879
Log:
Previously, this would generate the serialized output whenever
getInputStream was called.  If the base object changed between the time
the data source was created and the time getInputStream was called,
you'd see the modified result, but would take a performance hit when
serializing.  This change does the serialization (and the mime type lookup)
when the data source is created, allowing the getInputStream method to 
return pretty much immediately using the memory buffered serialization.

This also makes the data source threadsafe.

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java?view=diff&rev=555879&r1=555878&r2=555879
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaDataSource.java
Thu Jul 12 22:38:58 2007
@@ -30,34 +30,39 @@
 /**
  * Utility implementation of javax.activation.DataSource that wraps Abdera Base
  */
-public class AbderaDataSource 
+public final class AbderaDataSource 
   implements DataSource {
-
-  private Base base = null;
+  
+  private final byte[] data;
+  private final String mimetype;
+  private final String name;
   
   public AbderaDataSource(Base base) {
-    this.base = base;
+    this.data = read(base);
+    this.mimetype = MimeTypeHelper.getMimeType(base);
+    this.name = base.getClass().getName();
+  }
+  
+  private byte[] read(Base base) {
+    byte[] data = null;
+    try {
+      ByteArrayOutputStream out = new ByteArrayOutputStream();
+      base.writeTo(out);
+      data = out.toByteArray();
+    } catch (IOException e) {}
+    return data;
   }
   
   public String getContentType() {
-    return MimeTypeHelper.getMimeType(base);
+    return mimetype;
   }
 
   public InputStream getInputStream() throws IOException {
-    try {
-      ByteArrayOutputStream out = new ByteArrayOutputStream();
-      base.writeTo(out);
-      ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-      return in;
-    } catch (IOException io) {
-      throw io;
-    } catch (Exception e) {
-      throw new RuntimeException(e);
-    }
+    return new ByteArrayInputStream(data);
   }
 
   public String getName() {
-    return "Abdera Data Source::" + base.getClass().getName();
+    return "Abdera Data Source::" + name;
   }
 
   public OutputStream getOutputStream() throws IOException {



Mime
View raw message