[MOBY-guts] biomoby commit

Paul Gordon gordonp at pub.open-bio.org
Thu Aug 4 15:46:50 UTC 2005


gordonp
Thu Aug  4 11:46:49 EDT 2005
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data
In directory pub.open-bio.org:/tmp/cvs-serv8547

Modified Files:
	MobyDataBytes.java 
Log Message:
Made it easier to put data in (via URLs) and get data out (avoiding casts) for ease of use

moby-live/Java/src/main/org/biomoby/shared/data MobyDataBytes.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java	2005/05/19 15:57:26	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/shared/data/MobyDataBytes.java	2005/08/04 15:46:49	1.3
@@ -2,6 +2,7 @@
 package org.biomoby.shared.data;
 
 import org.biomoby.shared.MobyDataType;
+import java.io.*;
 
 /**	
  * A convenience class generally intended for the transmission of 
@@ -20,6 +21,9 @@
     private byte[] bytes;
     private static MobyDataType base64DataType; 
 
+    /** How much is read from a input stream (e.g. file) at once */
+    public static final int BYTE_READ_SIZE = 4096;  
+
     static{
 	base64DataType = new MobyDataType("text-base64");
     }
@@ -58,6 +62,36 @@
     }
 
     /**
+     * C-tor to use when you want to encode a resource, such as an image file.
+     *
+     * @param resourceURL the URL of the resource to encode, such as "file:..." or "http:..."
+     */
+    public MobyDataBytes(String name, java.net.URL resourceURL) throws IOException{
+	super(name);
+	setDataType(base64DataType);
+	if(resourceURL == null){
+	    return;
+	}
+	InputStream inputStream = resourceURL.openStream();
+	bytes = readStream(inputStream);
+    }
+
+    protected byte[] readStream(InputStream in) throws IOException {
+	byte buffer[] = new byte[BYTE_READ_SIZE];
+	ByteArrayOutputStream byteStream = new ByteArrayOutputStream(buffer.length);
+	
+	while (true) {
+	    int r = in.read(buffer);
+	    if (r<=0) {
+		break;
+	    }
+	    byteStream.write(buffer, 0, r);
+	}
+	return byteStream.toByteArray();
+    }
+
+
+    /**
      * Convenience method that returns an InputStream for use in image loaders,
      * file savers, etc.
      */
@@ -66,13 +100,20 @@
     }
     
     /**
-     * @return byte[] representing the underlying data in this object
+     * @return byte[] representing the (mutable) underlying data in this object
      */
     public Object getObject(){
 	return bytes;
     }
 
     /**
+     * The same as getObject, but doesn't require a cast of the result
+     */
+    public byte[] getBytes(){
+	return bytes;
+    }
+    
+    /**
      * This method base64 encodes the binary data held in this MOBY Object
      * if in service mode.
      */




More information about the MOBY-guts mailing list