Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 7823)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 7824)
@@ -31,6 +31,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -1058,7 +1059,7 @@
 	 *            matter withinInlineData's value, the document will
 	 * 
 	 */
-	public void toXml(OutputStream out, String user, String[] groups, boolean withInLineData)
+	public InputStream toXml(OutputStream out, String user, String[] groups, boolean withInLineData)
 			throws McdbException {
 		String documentDir = null;
 		String documentPath = null;
@@ -1084,7 +1085,7 @@
 			throw new McdbException("Attempting to read a zero length document from disk: " + documentPath);
 		}
 		
-		readFromFileSystem(out, user, groups, documentPath);
+		return readFromFileSystem(out, user, groups, documentPath);
 	}
     
     /**
@@ -1406,21 +1407,13 @@
 	 *            the location of the document on disk
 	 * 
 	 */
-    public void readFromFileSystem(
+    public InputStream readFromFileSystem(
     		OutputStream out, String user, String[] groups, String documentPath) throws McdbException {
         
-		String xmlFileContents = "";
+		String xmlFileContents = null;
+		String encoding = null;
 		
 		try {
-			// detect and use correct encoding
-            xmlFileContents = FileUtil.readFileToString(documentPath);
-            // guess the encoding from default bytes
-			XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlFileContents.getBytes()));
-			String encoding = xsr.getEncoding();
-			// reread the contents using the correct encoding
-			if (encoding != null) {
-				xmlFileContents = FileUtil.readFileToString(documentPath, encoding);
-			}
 			
 			// get a list of inline data sections that are not readable
 			// by this user
@@ -1431,8 +1424,19 @@
             // If this is for each unreadable section, strip the inline data
 			// from the doc
 			if (unReadableInlineDataList.size() > 0 && doctype != null) {
+				
+				// detect and use correct encoding
+	            xmlFileContents = FileUtil.readFileToString(documentPath);
+	            // guess the encoding from default bytes
+				XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlFileContents.getBytes()));
+				encoding = xsr.getEncoding();
+				xsr.close();
+				// reread the contents using the correct encoding
+				if (encoding != null) {
+					xmlFileContents = FileUtil.readFileToString(documentPath, encoding);
+				}
+				
 				Set<String> inlineKeySet = unReadableInlineDataList.keySet();
-				
 				boolean pre210Doc = doctype.equals(EML2_0_0NAMESPACE)
 						|| doctype.equals(EML2_0_1NAMESPACE);
 
@@ -1443,8 +1447,7 @@
 						// on whether this is a 2.0.1 or earlier doc or 2.1.0 and later.  This 
 						// is because of eml schema changes for inline access.
 						if (pre210Doc) {
-							xmlFileContents = stripInline20XData(xmlFileContents,
-									inlineKey);
+							xmlFileContents = stripInline20XData(xmlFileContents, inlineKey);
 						} else {
 							xmlFileContents = stripInline21XData(xmlFileContents, inlineKey);
 						}
@@ -1452,17 +1455,29 @@
 				}
 			}
             
-			// write with the detected encoding
-			Writer pw = new OutputStreamWriter(out, encoding);
-            // write the doc
-			pw.write(xmlFileContents);
-			pw.close();	
+			// will get input either from string content or file on disk
+			InputStream is = null;
 			
+			// get the input stream
+			if (xmlFileContents != null) {
+				is = IOUtils.toInputStream(xmlFileContents, encoding);
+			} else {
+				is = new FileInputStream(documentPath);
+			}
+
+			// send it to out
+			if (out != null) {
+				IOUtils.copyLarge(is, out);
+			}
+			// return the stream
+			return is;
+			
 	     } catch (UtilException e) {
              throw new McdbException(e.getMessage());
          } catch (IOException e) {
              throw new McdbException(e.getMessage());
         }
+		
     }
     
     /**
Index: src/edu/ucsb/nceas/metacat/MetacatHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetacatHandler.java	(revision 7823)
+++ src/edu/ucsb/nceas/metacat/MetacatHandler.java	(revision 7824)
@@ -30,7 +30,6 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -49,7 +48,6 @@
 import java.sql.Timestamp;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.Date;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
@@ -1009,100 +1007,67 @@
      * @throws ClassNotFoundException
      * @throws IOException
      */
-    public static InputStream read(String docid)
-        throws ParseLSIDException,
-        PropertyNotFoundException, McdbException, SQLException, 
-        ClassNotFoundException, IOException
-    {
-        logMetacat.debug("MetacatHandler.read() called.");
-        
-        InputStream fileInputStream = null;
-        File tmpDir = null;
-        
-        // be sure we have a local ID from an LSID
-        if (docid.startsWith("urn:")) {
-          try {
-            docid = LSIDUtil.getDocId(docid, true);                 
-            
-          } catch ( ParseLSIDException ple ) {
-            logMetacat.debug("There was a problem parsing the LSID. The " +
-                             "error message was: " + ple.getMessage());
-            throw ple;
-          }
-        }
-                
-        // accomodate old clients that send docids without revision numbers
-        docid = DocumentUtil.appendRev(docid);
-        
-        DocumentImpl doc = new DocumentImpl(docid, false);
-        
-        // deal with data or metadata cases
-        if (doc.getRootNodeID() == 0) {
-          
-          // this is a data file
-          
-          // get the path to the file to read
-          try {
-            String filepath = PropertyService.getProperty("application.datafilepath");
-          
-            // ensure it is a directory path
-            if ( !(filepath.endsWith("/")) ) {
-                filepath += "/";
-                
-            }
-            String filename = filepath + docid;
-            fileInputStream = (InputStream) readFromFilesystem(filename);
-          
-          } catch ( PropertyNotFoundException pnf ) {
-            logMetacat.debug("There was a problem finding the " +
-                             "application.datafilepath property. The error " + 
-                             "message was: " + pnf.getMessage());
-            throw pnf;
-            
-          } // end try()
+	public static InputStream read(String docid) throws ParseLSIDException,
+			PropertyNotFoundException, McdbException, SQLException,
+			ClassNotFoundException, IOException {
+		logMetacat.debug("MetacatHandler.read() called.");
 
-        } else {
-          
-          // this is an metadata document
-          
-          // get a temporary file handle to write to as an OutputStream
-          try {
-            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
+		InputStream inputStream = null;
 
-          } catch ( PropertyNotFoundException pnfe ) {
-            logMetacat.debug("There was a problem getting the" + 
-                             "application.tempDir property. Using /tmp " + 
-                             "instead. The error was: " + pnfe.getMessage());
-            tmpDir = new File("/tmp");
+		// be sure we have a local ID from an LSID
+		if (docid.startsWith("urn:")) {
+			try {
+				docid = LSIDUtil.getDocId(docid, true);
+			} catch (ParseLSIDException ple) {
+				logMetacat.debug("There was a problem parsing the LSID. The "
+						+ "error message was: " + ple.getMessage());
+				throw ple;
+			}
+		}
 
-          } //end try()
-          
-          final File outputFile = File.createTempFile("metacat.output", null, tmpDir);
-          OutputStream out = new FileOutputStream(outputFile);
+		// accomodate old clients that send docids without revision numbers
+		docid = DocumentUtil.appendRev(docid);
+		DocumentImpl doc = new DocumentImpl(docid, false);
 
-          // Try to get the metadata file from disk. If it isn't
-          // found, create it from the db and write it to disk then.
-          try {
-              doc.toXml(out, null, null, true);
-                             
-          } catch (McdbException e) {
-              // any exceptions in reading the xml from disc, and we go back to the
-              // old way of creating the xml directly.
-              logMetacat.error("MetacatHandler.readFromMetacat() " +
-                "- could not read from document file " + 
-                docid + ": " + e.getMessage());
-              e.printStackTrace(System.out);
-              doc.toXmlFromDb(out, null, null, true);
-          }
-          
-          // set the input stream
-          fileInputStream = new FileInputStream(outputFile);
-          outputFile.deleteOnExit();
-          
-        }
-                
-        return fileInputStream;
-    }
+		// deal with data or metadata cases
+		if (doc.getRootNodeID() == 0) {
+
+			// this is a data file
+			// get the path to the file to read
+			try {
+				String filepath = PropertyService.getProperty("application.datafilepath");
+				// ensure it is a directory path
+				if (!(filepath.endsWith("/"))) {
+					filepath += "/";
+				}
+				String filename = filepath + docid;
+				inputStream = readFromFilesystem(filename);
+			} catch (PropertyNotFoundException pnf) {
+				logMetacat.debug("There was a problem finding the "
+						+ "application.datafilepath property. The error "
+						+ "message was: " + pnf.getMessage());
+				throw pnf;
+			} // end try()
+
+		} else {
+			// this is an metadata document
+			// Get the xml (will try disk then DB)
+			try {
+				// force the InputStream to be returned
+				OutputStream nout = null;
+				inputStream = doc.toXml(nout, null, null, true);
+			} catch (McdbException e) {
+				// report the error
+				logMetacat.error(
+						"MetacatHandler.readFromMetacat() "
+								+ "- could not read document " + docid + ": "
+								+ e.getMessage(), e);
+			}
+
+		}
+
+		return inputStream;
+	}
     
     /**
      * Read a file from Metacat's configured file system data directory.
