Index: test/clienttestfiles/unicodeEML.xml
===================================================================
--- test/clienttestfiles/unicodeEML.xml	(nonexistent)
+++ test/clienttestfiles/unicodeEML.xml	(revision 5752)
@@ -0,0 +1,71 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<eml:eml xmlns:eml="eml://ecoinformatics.org/eml-2.1.0"
+	packageId="jscientist.7.2" system="knb"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="eml://ecoinformatics.org/eml-2.1.0 eml.xsd">
+	<access authSystem="knb" order="allowFirst">
+		<allow>
+			<principal>public</principal>
+			<permission>read</permission>
+		</allow>
+	</access>
+	<dataset>
+		<title>測試中的數據包 (Test Chinese data package) _DOCID_</title>
+		<creator>
+			<individualName>
+				<givenName>Joe</givenName>
+				<surName>Scientist</surName>
+			</individualName>
+			<address>
+				<deliveryPoint>Marine Science Institute</deliveryPoint>
+				<city>Santa Barbara</city>
+				<administrativeArea>CA</administrativeArea>
+				<postalCode>90024</postalCode>
+				<country>USA</country>
+			</address>
+			<phone>(310) 206-1984</phone>
+			<electronicMailAddress>jsci@msi.ucsb.edu</electronicMailAddress>
+		</creator>
+		<associatedParty>
+			<individualName>
+				<givenName>Joe</givenName>
+				<surName>Scientist</surName>
+			</individualName>
+			<address>
+				<deliveryPoint>Marine Science Institute</deliveryPoint>
+				<city>Santa Barbara</city>
+				<administrativeArea>CA</administrativeArea>
+				<postalCode>90024</postalCode>
+				<country>USA</country>
+			</address>
+			<phone>(310) 206-1984</phone>
+			<electronicMailAddress>jsci@msi.ucsb.edu</electronicMailAddress>
+			<role>owner</role>
+		</associatedParty>
+		<abstract>
+			<para>人口抽樣數據的各種浮游動物物種在大湖區，2000年。抽樣樣本採用凡德恩在1米，深5米放置一個隨機抽樣的網格。物種名稱根據 ITIS的分類數據庫（http://www.itis.usda.gov/）。這是虛構的樣本數據，並應以任何方式被解釋為有意義的信息！</para>
+		</abstract>
+		<keywordSet>
+			<keyword keywordType="theme">zooplankton</keyword>
+			<keyword keywordType="theme">Van Dorn sampling</keyword>
+			<keyword keywordType="theme">population survey</keyword>
+			<keyword keywordType="theme">fictitious sample data</keyword>
+			<keyword keywordType="place">Great Lakes</keyword>
+		</keywordSet>
+		<contact>
+			<individualName>
+				<givenName>Joe</givenName>
+				<surName>Scientist</surName>
+			</individualName>
+			<address>
+				<deliveryPoint>Marine Science Institute</deliveryPoint>
+				<city>Santa Barbara</city>
+				<administrativeArea>CA</administrativeArea>
+				<postalCode>90024</postalCode>
+				<country>USA</country>
+			</address>
+			<phone>(310) 206-1984</phone>
+			<electronicMailAddress>jsci@msi.ucsb.edu</electronicMailAddress>
+		</contact>
+	</dataset>
+</eml:eml>
\ No newline at end of file

Property changes on: test/clienttestfiles/unicodeEML.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
Index: test/edu/ucsb/nceas/metacattest/NonAsciiCharacterTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/NonAsciiCharacterTest.java	(revision 5751)
+++ test/edu/ucsb/nceas/metacattest/NonAsciiCharacterTest.java	(revision 5752)
@@ -43,6 +43,7 @@
 import edu.ucsb.nceas.metacat.client.MetacatFactory;
 import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
 import edu.ucsb.nceas.metacat.properties.PropertyService;
+import edu.ucsb.nceas.utilities.FileUtil;
 import edu.ucsb.nceas.utilities.IOUtil;
 import edu.ucsb.nceas.utilities.PropertyNotFoundException;
 import junit.framework.Test;
@@ -204,10 +205,10 @@
     
     /**
      * Returns an xml squery that searches for the doc id in the
-     * title of documents. This function is for eml-2.0.1 only. For 
+     * title of documents. This function is for eml-2.0.1+ only. For 
      * other eml versions, this function might have to modified.
      */
-    private String getTestEmlQuery(String docid, String emlVersion) {
+    private String getTestEmlQuery(String titlePart, String emlVersion) {
 
     	String docType;
     	if (emlVersion.equals(EML2_0_1)) {
@@ -227,7 +228,7 @@
         		"<returndoctype>" + docType + "</returndoctype>" +
         		"<querygroup operator=\"UNION\">" +
         			"<queryterm casesensitive=\"false\" searchmode=\"contains\">" +
-        				"<value>" + docid + "</value>" +
+        				"<value>" + titlePart + "</value>" +
         				"<pathexpr>dataset/title</pathexpr>" +
         			"</queryterm>" +
         		"</querygroup>" +
@@ -280,9 +281,11 @@
         suite.addTest(new NonAsciiCharacterTest("quote210Test"));
         suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat201Test"));
         suite.addTest(new NonAsciiCharacterTest("numericCharacterReferenceFormat210Test"));*/
-        suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
+        //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter201Test"));
         //suite.addTest(new NonAsciiCharacterTest("nonLatinUnicodeCharacter210Test"));
+        suite.addTest(new NonAsciiCharacterTest("unicodeCharacterTest"));
 
+
         return suite;
     }
 
@@ -506,7 +509,7 @@
      * Test inserting and reading an EML 2.0.1 document with the code representation 
      * of a micro sign (&#181). Read should succeed since the same document should be 
      * read back from disk that was submitted.  Query should succeed because we look 
-     * for the converted character (¬µ).
+     * for the converted character (µ).
      */
     public void numericCharacterReferenceFormat201Test() {
     	debug("\nRunning: numericCharacterReferenceFormat201Test");
@@ -515,7 +518,7 @@
             m.login(username, password);
             
             String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
-            String convertedTestTitle = "Checking ¬µ in doc: " + newdocid  + ".1";
+            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
             
             testdocument = getTestEmlDoc(testTitle, EML2_0_1);
             insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
@@ -546,7 +549,7 @@
      * Test inserting and reading an EML 2.1.0 document with the code representation 
      * of a micro sign (&#181). Read should succeed since the same document should be 
      * read back from disk that was submitted.  Query should succeed because we look 
-     * for the converted character (¬µ).
+     * for the converted character (µ).
      */
     public void numericCharacterReferenceFormat210Test() {
     	debug("\nRunning: numericCharacterReferenceFormat210Test");
@@ -555,7 +558,7 @@
             m.login(username, password);
             
             String testTitle = "Checking &#181; in doc: " + newdocid  + ".1";
-            String convertedTestTitle = "Checking ¬µ in doc: " + newdocid  + ".1";
+            String convertedTestTitle = "Checking µ in doc: " + newdocid  + ".1";
             
             testdocument = getTestEmlDoc(testTitle, EML2_1_0);
             insertDocid(newdocid + ".1", testdocument, SUCCESS, true);
@@ -583,10 +586,10 @@
     }
 
     /**
-     * Test inserting and reading an EML 2.0.1 document with the micro sign (¬µ). 
+     * Test inserting and reading an EML 2.0.1 document with the micro sign (µ). 
      * Read should succeed since the same document should be read back from disk 
      * that was submitted.  Query should succeed because we look for the same 
-     * character (¬µ).
+     * character (µ).
      */
     public void nonLatinUnicodeCharacter201Test() {
     	debug("\nRunning: nonLatinUnicodeCharacter201Test");
@@ -597,6 +600,9 @@
             String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
             
             testdocument = getTestEmlDoc(testTitle, EML2_0_1);
+            
+            debug("original test document:	" + testdocument);
+            
             insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
             
             // this tests reading the document back from disk
@@ -633,7 +639,7 @@
             String newdocid = generateDocid();
             m.login(username, password);
             
-            String testTitle = "Checking characters like ¬µ in doc: " + newdocid  + ".1";
+            String testTitle = "Checking characters like µ in doc: " + newdocid  + ".1";
             
             testdocument = getTestEmlDoc(testTitle, EML2_1_0);
             insertDocid(newdocid + ".1", testdocument, SUCCESS, false);
@@ -659,7 +665,52 @@
             fail("General exception:\n" + e.getMessage());
         }
     }
+    
+    /**
+     * Test inserting and reading an EML 2.1.0 with Chinese
+     */
+    public void unicodeCharacterTest() {
+    	debug("\nRunning: unicodeCharacterTest");
+        try {
+            
+        	String filePath = "test/clienttestfiles/unicodeEML.xml";
+            String testTitle = "測試中的數據包 (Test Chinese data package) _DOCID_";
+            String newdocid = generateDocid() + ".1";
+            testdocument = FileUtil.readFileToString(filePath, "UTF-8");
+            
+            // include the docid
+            testdocument = testdocument.replaceAll("_DOCID_", newdocid);
+            testTitle = testTitle.replaceAll("_DOCID_", newdocid);
+            
+            // login
+            m.login(username, password);
+            
+            // insert
+            insertDocid(newdocid, testdocument, SUCCESS, false);
 
+            // this tests reading the document back from disk
+            readDocidWhichEqualsDoc(newdocid, testdocument, SUCCESS, true);
+            
+            // this tests searching for the document in the database
+            Thread.sleep(3000);
+            queryDocWhichHasTitle(testTitle, testTitle, EML2_1_0, SUCCESS);
+            
+            // clean up
+            //deleteDocid(newdocid, SUCCESS, false);
+            
+            m.logout();
+        }
+        catch (MetacatAuthException mae) {
+            fail("Authorization failed:\n" + mae.getMessage());
+        }
+        catch (MetacatInaccessibleException mie) {
+            fail("Metacat Inaccessible:\n" + mie.getMessage());
+        }
+        catch (Exception e) {
+            fail("General exception:\n" + e.getMessage());
+        }
+    }
+
     /**
      * Insert a document into metacat. The expected result is passed as result
      */
@@ -667,6 +718,9 @@
                                boolean expectMetacatException) {
         String response = null;
         try {
+        	
+        	debug("doctext: " + docText);
+        	
             response = m.insert(docid,
                                 new StringReader(docText), null);
             System.err.println(response);
@@ -883,17 +937,17 @@
     }
     
     /**
-     * Query a document by looking for it's doc id in the title element.
+     * Query a document by looking for a part of the title in the title element.
      * Then check if the testTitle exists in the doc.
-     * @param docId the id of the doc to look for
+     * @param titlePart the part of the title of the doc to look for
      * @param testTitle the title containing special characters
      * @param result are we expecting SUCCESS or FAILURE
      * @param expextedKarmaFailure
      */
-    private void queryDocWhichHasTitle(String docId, String testTitle,
+    private void queryDocWhichHasTitle(String titlePart, String testTitle,
                                          String emlVersion, boolean result) {
         try {
-            String sQuery = getTestEmlQuery(docId, emlVersion);
+            String sQuery = getTestEmlQuery(titlePart, emlVersion);
         	Reader queryReader = new StringReader(sQuery);
             Reader resultReader = m.query(queryReader);
             String queryResult = IOUtil.getAsString(resultReader, true);
Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 5752)
@@ -27,7 +27,9 @@
 package edu.ucsb.nceas.metacat;
 
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.Writer;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -263,6 +265,7 @@
     private static final String TRUE  = "true";
     private static String LOG_CONFIG_NAME = null;
     public static final String APPLICATION_NAME = "metacat";
+	public static final String DEFAULT_ENCODING = "UTF-8";
     
     /**
      * Initialize the servlet by creating appropriate database connections
@@ -631,6 +634,9 @@
 			HttpServletResponse response) throws ServletException, IOException {
 		Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
 
+		String requestEncoding = request.getCharacterEncoding();
+		logMetacat.warn("requestEncoding: " + requestEncoding);
+		
 		// Update the last update time for this user if they are not new
 		HttpSession httpSession = request.getSession(false);
 		if (httpSession != null) {
@@ -829,18 +835,16 @@
 			// Now that we know the session is valid, we can delegate the
 			// request to a particular action handler
 			if (action.equals("query")) {
-				ServletOutputStream streamOut = response.getOutputStream();
-				PrintWriter out = new PrintWriter(streamOut);
+		        Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
 				handler.handleQuery(out, params, response, userName, groupNames, sessionId);
 				out.close();
 			} else if (action.equals("squery")) {
-				ServletOutputStream streamOut = response.getOutputStream();
-				PrintWriter out = new PrintWriter(streamOut);
+				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
 				if (params.containsKey("query")) {
 					handler.handleSQuery(out, params, response, userName, groupNames, sessionId);
 					out.close();
 				} else {
-					out.println("Illegal action squery without \"query\" parameter");
+					out.write("Illegal action squery without \"query\" parameter");
 					out.close();
 				}
 			} else if (action.trim().equals("spatial_query")) {
@@ -847,8 +851,7 @@
 
 				logMetacat
 						.debug("MetaCatServlet.handleGetOrPost - ******************* SPATIAL QUERY ********************");
-				ServletOutputStream streamOut = response.getOutputStream();
-				PrintWriter out = new PrintWriter(streamOut);
+				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
 				handler.handleSpatialQuery(out, params, response, userName, groupNames, sessionId);
 				out.close();
 
Index: src/edu/ucsb/nceas/metacat/DocumentImpl.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/DocumentImpl.java	(revision 5752)
@@ -32,13 +32,12 @@
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.PrintWriter;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.Reader;
 import java.io.StringReader;
-import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
 import java.sql.PreparedStatement;
@@ -76,6 +75,7 @@
 import edu.ucsb.nceas.utilities.UtilException;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.XmlStreamReader;
 import org.apache.log4j.Logger;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.DTDHandler;
@@ -933,37 +933,69 @@
 
     /**
      * Print a string representation of the XML document
+     * NOTE: this detects the character encoding, or uses the XML default
      */
     public String toString(String user, String[] groups, boolean withInlinedata)
     {
-        StringWriter docwriter = new StringWriter();
+    	ByteArrayOutputStream out = new ByteArrayOutputStream();
         try {
-            this.toXml(docwriter, user, groups, withInlinedata);
+            this.toXml(out, user, groups, withInlinedata);
         } catch (McdbException mcdbe) {
             return null;
         }
-        String document = docwriter.toString();
+        String encoding = null;
+        String document = null;
+        try {
+        	XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(out.toByteArray()));
+        	encoding = xsr.getEncoding();
+        	document = out.toString(encoding);
+        } catch (Exception e) {
+        	document = out.toString();
+		}
         return document;
     }
 
     /**
      * Print a string representation of the XML document
+     * NOTE: this detects the character encoding, or uses the XML default
      */
     public String toString()
     {
-        StringWriter docwriter = new StringWriter();
-        String userName = null;
+    	ByteArrayOutputStream out = new ByteArrayOutputStream();
+    	String userName = null;
         String[] groupNames = null;
         boolean withInlineData = true;
         try {
-            this.toXml(docwriter, userName, groupNames, withInlineData);
+            this.toXml(out, userName, groupNames, withInlineData);
         } catch (McdbException mcdbe) {
         	logMetacat.warn("Could not convert documentImpl to xml: " + mcdbe.getMessage());
             return null;
         }
-        String document = docwriter.toString();
+        String encoding = null;
+        String document = null;
+        try {
+        	XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(out.toByteArray()));
+        	encoding = xsr.getEncoding();
+        	document = out.toString(encoding);
+        } catch (Exception e) {
+        	document = out.toString();
+		}
         return document;
     }
+    
+	public byte[] getBytes() {
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+    	String userName = null;
+        String[] groupNames = null;
+        boolean withInlineData = true;
+        try {
+            this.toXml(out, userName, groupNames, withInlineData);
+        } catch (McdbException mcdbe) {
+        	logMetacat.warn("Could not convert documentImpl to xml: " + mcdbe.getMessage());
+            return null;
+        }
+        return out.toByteArray();
+	}
 
     /**
      * Get a text representation of the XML document as a string This older
@@ -1010,7 +1042,7 @@
 	 *            matter withinInlineData's value, the document will
 	 * 
 	 */
-	public void toXml(Writer pw, String user, String[] groups, boolean withInLineData)
+	public void toXml(OutputStream out, String user, String[] groups, boolean withInLineData)
 			throws McdbException {
 		String documentDir = null;
 		String documentPath = null;
@@ -1021,8 +1053,8 @@
 
 			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
 					|| FileUtil.getFileSize(documentPath) == 0) {
-				FileWriter fileWriter = new FileWriter(documentPath);
-				toXmlFromDb(fileWriter, user, groups, true);
+				FileOutputStream fos = new FileOutputStream(documentPath);
+				toXmlFromDb(fos, user, groups, true);
 			}
 		} catch (PropertyNotFoundException pnfe) {
 			throw new McdbException("Could not write file: " + documentPath + " : "
@@ -1036,7 +1068,7 @@
 			throw new McdbException("Attempting to read a zero length document from disk: " + documentPath);
 		}
 		
-		readFromFileSystem(pw, user, groups, documentPath);
+		readFromFileSystem(out, user, groups, documentPath);
 	}
     
     /**
@@ -1047,8 +1079,8 @@
 	 *            matter withinInlineData's value, the document will
 	 * 
 	 */
-    public void toXmlFromDb(Writer pw, String user, String[] groups,
-            boolean withInLineData) throws McdbException
+    public void toXmlFromDb(OutputStream outputStream, String user, String[] groups,
+            boolean withInLineData) throws McdbException, IOException
     {
         // flag for process eml2
         boolean proccessEml2 = false;
@@ -1070,12 +1102,10 @@
         boolean processInlineData = false;
 
         TreeSet<NodeRecord> nodeRecordLists = null;
-        PrintWriter out = null;
-        if (pw instanceof PrintWriter) {
-            out = (PrintWriter) pw;
-        } else {
-            out = new PrintWriter(pw);
-        }
+        
+        // Note: we haven't stored the encoding, so we use the default for XML
+        String encoding = "UTF-8";
+        Writer out = new OutputStreamWriter(outputStream, encoding);
        
         // Here add code to handle subtree access control
         /*
@@ -1134,14 +1164,14 @@
                         logMetacat.debug("\n POPPED: "
                                 + currentElement.getNodeName());
                         if (previousNodeWasElement) {
-                            out.print(">");
+                            out.write(">");
                             previousNodeWasElement = false;
                         }
                         if (currentElement.getNodePrefix() != null) {
-                            out.print("</" + currentElement.getNodePrefix() + ":"
+                            out.write("</" + currentElement.getNodePrefix() + ":"
                                     + currentElement.getNodeName() + ">");
                         } else {
-                            out.print("</" + currentElement.getNodeName() + ">");
+                            out.write("</" + currentElement.getNodeName() + ">");
                         }
                         currentElement = openElements.peek();
                     }
@@ -1150,7 +1180,7 @@
 
             // Handle the DOCUMENT node
             if (currentNode.getNodeType().equals("DOCUMENT")) {
-                out.print("<?xml version=\"1.0\"?>");
+                out.write("<?xml version=\"1.0\"?>");
 
                 // Handle the ELEMENT nodes
             } else if (currentNode.getNodeType().equals("ELEMENT")) {
@@ -1158,7 +1188,7 @@
                     atRootElement = false;
                 } else {
                     if (previousNodeWasElement) {
-                        out.print(">");
+                        out.write(">");
                     }
                 }
 
@@ -1169,11 +1199,11 @@
                             && validateType.equals(DTD)) {
                         if ((doctype != null) && (system_id != null)) {
 
-                            out.print("<!DOCTYPE " + docname + " PUBLIC \""
+                            out.write("<!DOCTYPE " + docname + " PUBLIC \""
                                     + doctype + "\" \"" + system_id + "\">");
                         } else {
 
-                            out.print("<!DOCTYPE " + docname + ">");
+                            out.write("<!DOCTYPE " + docname + ">");
                         }
                     }
                 }
@@ -1182,10 +1212,10 @@
                 logMetacat.debug("\n PUSHED: " + currentNode.getNodeName());
                 previousNodeWasElement = true;
                 if (currentNode.getNodePrefix() != null) {
-                    out.print("<" + currentNode.getNodePrefix() + ":"
+                    out.write("<" + currentNode.getNodePrefix() + ":"
                             + currentNode.getNodeName());
                 } else {
-                    out.print("<" + currentNode.getNodeName());
+                    out.write("<" + currentNode.getNodeName());
                 }
 
                 // if currentNode is inline and handle eml2, set flag process
@@ -1199,11 +1229,11 @@
                 // Handle the ATTRIBUTE nodes
             } else if (currentNode.getNodeType().equals("ATTRIBUTE")) {
                 if (currentNode.getNodePrefix() != null) {
-                    out.print(" " + currentNode.getNodePrefix() + ":"
+                    out.write(" " + currentNode.getNodePrefix() + ":"
                             + currentNode.getNodeName() + "=\""
                             + currentNode.getNodeData() + "\"");
                 } else {
-                    out.print(" " + currentNode.getNodeName() + "=\""
+                    out.write(" " + currentNode.getNodeName() + "=\""
                             + currentNode.getNodeData() + "\"");
                 }
 
@@ -1215,17 +1245,17 @@
                   nsprefix = " xmlns";
                 }
                 
-                out.print(nsprefix + currentNode.getNodeName() + "=\""
+                out.write(nsprefix + currentNode.getNodeName() + "=\""
                           + currentNode.getNodeData() + "\"");
 
                 // Handle the TEXT nodes
             } else if (currentNode.getNodeType().equals("TEXT")) {
                 if (previousNodeWasElement) {
-                    out.print(">");
+                    out.write(">");
                 }
                 if (!processInlineData) {
                     // if it is not inline data just out put data
-                    out.print(currentNode.getNodeData());
+                    out.write(currentNode.getNodeData());
                 } else {
                     // if it is inline data first to get the inline data
                     // internal id
@@ -1253,12 +1283,12 @@
                     	// inline data file path, to get it
 
                         Reader reader = Eml200SAXHandler
-                                .readInlineDataFromFileSystem(fileName);
+                                .readInlineDataFromFileSystem(fileName, encoding);
                         char[] characterArray = new char[4 * 1024];
                         try {
                             int length = reader.read(characterArray);
                             while (length != -1) {
-                                out.print(new String(characterArray, 0,
+                                out.write(new String(characterArray, 0,
                                                 length));
                                 out.flush();
                                 length = reader.read(characterArray);
@@ -1271,7 +1301,7 @@
                     else {
                         // if user can't read it, we only send it back a empty
                         // string in inline element.
-                        out.print("");
+                        out.write("");
                     }// else can't read inlinedata
                     // reset proccess inline data false
                     processInlineData = false;
@@ -1280,17 +1310,17 @@
                 // Handle the COMMENT nodes
             } else if (currentNode.getNodeType().equals("COMMENT")) {
                 if (previousNodeWasElement) {
-                    out.print(">");
+                    out.write(">");
                 }
-                out.print("<!--" + currentNode.getNodeData() + "-->");
+                out.write("<!--" + currentNode.getNodeData() + "-->");
                 previousNodeWasElement = false;
 
                 // Handle the PI nodes
             } else if (currentNode.getNodeType().equals("PI")) {
                 if (previousNodeWasElement) {
-                    out.print(">");
+                    out.write(">");
                 }
-                out.print("<?" + currentNode.getNodeName() + " "
+                out.write("<?" + currentNode.getNodeName() + " "
                         + currentNode.getNodeData() + "?>");
                 previousNodeWasElement = false;
                 // Handle the DTD nodes (docname, publicid, systemid)
@@ -1309,12 +1339,12 @@
                         if ((dbPublicID != null) && (dbSystemID != null)) {
 
                             out
-                                    .print("<!DOCTYPE " + dbDocName
+                                    .write("<!DOCTYPE " + dbDocName
                                             + " PUBLIC \"" + dbPublicID
                                             + "\" \"" + dbSystemID + "\">");
                         } else {
 
-                            out.print("<!DOCTYPE " + dbDocName + ">");
+                            out.write("<!DOCTYPE " + dbDocName + ">");
                         }
                     }
 
@@ -1338,10 +1368,10 @@
             NodeRecord currentElement = (NodeRecord) openElements.pop();
             logMetacat.debug("\n POPPED: " + currentElement.getNodeName());
             if (currentElement.getNodePrefix() != null) {
-                out.print("</" + currentElement.getNodePrefix() + ":"
+                out.write("</" + currentElement.getNodePrefix() + ":"
                         + currentElement.getNodeName() + ">");
             } else {
-                out.print("</" + currentElement.getNodeName() + ">");
+                out.write("</" + currentElement.getNodeName() + ">");
             }
         }
         out.flush();
@@ -1361,15 +1391,22 @@
 	 *            the location of the document on disk
 	 * 
 	 */
-    public void readFromFileSystem(Writer pw, String user, String[] groups,
-            String documentPath) throws McdbException {
+    public void readFromFileSystem(
+    		OutputStream out, String user, String[] groups, String documentPath) throws McdbException {
         
 		String xmlFileContents = "";
 		
 		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
             Hashtable<String, String> unReadableInlineDataList =
@@ -1401,6 +1438,8 @@
 				}
 			}
             
+			// write with the detected encoding
+			Writer pw = new OutputStreamWriter(out, encoding);
             // write the doc
 			pw.write(xmlFileContents);
 			pw.close();	
@@ -1420,10 +1459,9 @@
 	 * @param accNumber
 	 *            the document id which is used to name the output file
 	 */
-    private static void writeToFileSystem(InputStream xml, String accNumber) throws McdbException {
+    private static void writeToFileSystem(String xml, String accNumber, String encoding) throws McdbException {
 
 		// write the document to disk
-//		FileWriter fileWriter = null;
 		String documentDir = null;
 		String documentPath = null;
 
@@ -1448,32 +1486,12 @@
 					&& (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST 
 							|| FileUtil.getFileSize(documentPath) == 0)) {
 
-				//FileUtil.writeFile(documentPath, xml);
 			    try
 			    {
-			        String s = IOUtils.toString(xml);
-			        //TODO: the doc gets put into memory here.  I can't get
-			        //it to write to the filesystem correctly without doing 
-			        //this.  Need to revisit at some point.
-			        if(s.endsWith("\n"))
-			        {
-			          s += "\n";
-			        }
-			        //HACK: For some reason, FileOutputStream is cutting off the last
-			        //byte of the file if there is a newline present.  The only
-			        //way I could find to fix this was to add another newline.
-			         
+			        
 			        FileOutputStream fos = new FileOutputStream(documentPath);
-			        IOUtils.write(s.getBytes(), fos);
-			        byte[] b = new byte[1024];
-			        int numread = xml.read(b, 0, 1024);
-			        /*while(numread != -1)
-			        {
-			            System.out.println("numread: " + numread + " s: \"" + new String(b, 0, numread) + "\"");
-			            System.out.println("last byte: " + b[numread] + "  one less: " + b[numread - 1] + " one more: " + b[numread + 1]);
-			            fos.write(b, 0, numread);
-			            numread = xml.read(b, 0, 1024);
-			        }*/
+			        IOUtils.write(xml.getBytes(encoding), fos);
+			  
 			        fos.flush();
 			        fos.close();
 			    }
@@ -1487,10 +1505,7 @@
 		} catch (PropertyNotFoundException pnfe) {
 			throw new McdbException("Could not write file: " + documentPath + " : "
 					+ pnfe.getMessage());
-		} /*catch (UtilException ue) {
-			throw new McdbException("Could not write file: " + documentPath + " : "
-					+ ue.getMessage());
-		}*/
+		}
 	}	
     
     /**
@@ -2647,6 +2662,11 @@
                     //System.out.println("In lockgranted");
                 	logReplication.info("lock granted for " + accnum
                             + " from " + server);
+                	
+                	// detect encoding
+                    XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlString.getBytes()));
+			        String encoding = xsr.getEncoding();
+			        
                     /*
                      * XMLReader parser = initializeParser(conn, action, docid,
                      * updaterev, validate, user, groups, pub, serverCode, dtd);
@@ -2654,7 +2674,8 @@
                     logMetacat.debug("DocumentImpl.write - initializing parser");
                     parser = initializeParser(conn, action, docid, xmlReader, updaterev,
                             user, groups, pub, serverCode, dtd, ruleBase,
-                            needValidation, false, null, null);// false means it is not a revision doc
+                            needValidation, false, null, null, encoding);
+                    	// false means it is not a revision doc
                                    //null, null are createdate and updatedate
                                    //null will use current time as create date time
                     conn.setAutoCommit(false);
@@ -2664,10 +2685,8 @@
                     conn.setAutoCommit(true);
                     
                     //write the file to disk
-                    logMetacat.debug("DocumentImpl.write - Writing xml to file system");
-                    
-                    ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes());
-                	writeToFileSystem(xmlBAOS, accnum);
+                    logMetacat.debug("DocumentImpl.write - Writing xml to file system");                    
+                	writeToFileSystem(xmlString, accnum, encoding);
 
                     // write to xml_node complete. start the indexing thread.
                     addDocidToIndexingQueue(docid, rev);
@@ -2733,8 +2752,12 @@
         }
         XMLReader parser = null;
         try {
+            // detect encoding
+        	XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlString.getBytes()));
+	        String encoding = xsr.getEncoding();
+	        
             parser = initializeParser(conn, action, docid, xmlReader, rev, user, groups,
-                    pub, serverCode, dtd, ruleBase, needValidation, false, null, null);
+                    pub, serverCode, dtd, ruleBase, needValidation, false, null, null, encoding);
                     // null and null are createtime and updatetime
                     // null will create current time
                     //false means it is not a revision doc
@@ -2747,9 +2770,9 @@
             conn.setAutoCommit(true);
             
             //write the file to disk
-            ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes());
-        	writeToFileSystem(xmlBAOS, accnum);
 
+        	writeToFileSystem(xmlString, accnum, encoding);
+
             addDocidToIndexingQueue(docid, rev);
         } catch (Exception e) {
         	logMetacat.error("DocumentImpl.write - Problem with parsing: " + e.getMessage());
@@ -2916,9 +2939,13 @@
             {
                 isRevision = true;
             }
+            // detect encoding
+            XmlStreamReader xsr = new XmlStreamReader(new ByteArrayInputStream(xmlString.getBytes()));
+	        String encoding = xsr.getEncoding();
+	       
             parser = initializeParser(conn, action, docid, xmlReader, rev, user, groups,
                     pub, serverCode, dtd, ruleBase, needValidation, 
-                    isRevision, createDate, updateDate);
+                    isRevision, createDate, updateDate, encoding);
          
             conn.setAutoCommit(false);
             parser.parse(new InputSource(xmlReader));
@@ -2926,8 +2953,7 @@
             conn.setAutoCommit(true);
             
             // Write the file to disk
-            ByteArrayInputStream xmlBAOS = new ByteArrayInputStream(xmlString.getBytes());
-        	writeToFileSystem(xmlBAOS, accnum);
+        	writeToFileSystem(xmlString, accnum, encoding);
             
             // write to xml_node complete. start the indexing thread.
             // this only for xml_documents
@@ -3432,7 +3458,7 @@
             String action, String docid, Reader xml, String rev, String user,
             String[] groups, String pub, int serverCode, Reader dtd,
             String ruleBase, boolean needValidation, boolean isRevision,
-            String createDate, String updateDate) throws Exception
+            String createDate, String updateDate, String encoding) throws Exception
     {
         XMLReader parser = null;
         try {
@@ -3449,6 +3475,7 @@
                 chandler = new Eml200SAXHandler(dbconn, action, docid, rev,
                         user, groups, pub, serverCode, createDate, updateDate);
                 chandler.setIsRevisionDoc(isRevision);
+                chandler.setEncoding(encoding);
                 parser.setContentHandler((ContentHandler) chandler);
                 parser.setErrorHandler((ErrorHandler) chandler);
                 parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
@@ -3475,6 +3502,7 @@
                 chandler = new Eml210SAXHandler(dbconn, action, docid, rev,
                         user, groups, pub, serverCode, createDate, updateDate);
                 chandler.setIsRevisionDoc(isRevision);
+                chandler.setEncoding(encoding);
                 parser.setContentHandler((ContentHandler) chandler);
                 parser.setErrorHandler((ErrorHandler) chandler);
                 parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
@@ -3501,6 +3529,7 @@
                 chandler = new DBSAXHandler(dbconn, action, docid, rev, user,
                         groups, pub, serverCode, createDate, updateDate);
                 chandler.setIsRevisionDoc(isRevision);
+                chandler.setEncoding(encoding);
                 parser.setContentHandler((ContentHandler) chandler);
                 parser.setErrorHandler((ErrorHandler) chandler);
                 parser.setProperty(DECLARATIONHANDLERPROPERTY, chandler);
@@ -4071,7 +4100,7 @@
                 if (useOldReadAlgorithm) {
                     logMetacat.error("DocumentImpl.main - " + xmldoc.readUsingSlowAlgorithm());
                 } else {
-                    xmldoc.toXml(new PrintWriter(System.out), null, null, true);
+                    xmldoc.toXml(System.out, null, null, true);
                 }
             } else if (action.equals("DELETE")) {
                 DocumentImpl.delete(docid, null, null,null);
@@ -4105,7 +4134,7 @@
             }
             dbconn.close();
         } catch (McdbException me) {
-            me.toXml(new PrintWriter(System.err));
+            me.toXml(new OutputStreamWriter(System.err));
         } catch (AccessionNumberException ane) {
             System.err.println(ane.getMessage());
         } catch (Exception e) {
Index: src/edu/ucsb/nceas/metacat/MetacatHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetacatHandler.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/MetacatHandler.java	(revision 5752)
@@ -27,13 +27,15 @@
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.Reader;
 import java.io.StringReader;
 import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -53,7 +55,6 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
-import javax.servlet.ServletContext;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -60,6 +61,7 @@
 import javax.servlet.http.HttpSession;
 import javax.activation.MimetypesFileTypeMap;
 
+import org.apache.commons.io.input.XmlStreamReader;
 import org.apache.log4j.Logger;
 import org.ecoinformatics.eml.EMLParser;
 
@@ -83,6 +85,7 @@
 import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
 import edu.ucsb.nceas.metacat.service.SessionService;
 import edu.ucsb.nceas.metacat.service.XMLSchemaService;
+import edu.ucsb.nceas.metacat.shared.HandlerException;
 import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
 import edu.ucsb.nceas.metacat.shared.ServiceException;
 import edu.ucsb.nceas.metacat.spatial.SpatialHarvester;
@@ -109,6 +112,8 @@
 public class MetacatHandler {
 
     private static boolean _sitemapScheduled = false;
+    
+    private static Logger logMetacat = Logger.getLogger(MetacatHandler.class);
 
     // Constants -- these should be final in a servlet    
     private static final String PROLOG = "<?xml version=\"1.0\"?>";
@@ -219,19 +224,24 @@
      * queries supported by the WFS / WMS standards
      * 
      * handleSQuery(out, params, response, username, groupnames, sess_id);
+     * @throws HandlerException 
      */
-    protected void handleSpatialQuery(PrintWriter out, Hashtable<String, String[]> params,
+    protected void handleSpatialQuery(Writer out, Hashtable<String, String[]> params,
             HttpServletResponse response,
             String username, String[] groupnames,
-            String sess_id) throws PropertyNotFoundException {
+            String sess_id) throws PropertyNotFoundException, HandlerException {
         
         Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         
         if ( !PropertyService.getProperty("spatial.runSpatialOption").equals("true") ) {
             response.setContentType("text/html");
-            out.println("<html> Metacat Spatial Option is turned off </html>");
-            out.close();
-            return ;
+            try {
+				out.write("<html> Metacat Spatial Option is turned off </html>");
+	            out.close();
+			} catch (IOException e) {
+				throw new HandlerException(e.getMessage());
+			}
+            return;
         }
         
         /*
@@ -458,7 +468,7 @@
      * @param response the response object linked to the client
      * @param conn the database connection
      */
-    protected void handleSQuery(PrintWriter out, Hashtable<String, String[]> params,
+    protected void handleSQuery(Writer out, Hashtable<String, String[]> params,
             HttpServletResponse response, String user, String[] groups,
             String sessionid) throws PropertyNotFoundException {
         Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
@@ -485,10 +495,12 @@
      * @param params the Hashtable of parameters that should be included in the
      *            squery.
      * @param response the response object linked to the client
+     * @throws IOException 
+     * @throws UnsupportedEncodingException 
      */
-    protected void handleQuery(PrintWriter out, Hashtable<String, String[]> params,
+    protected void handleQuery(Writer out, Hashtable<String, String[]> params,
             HttpServletResponse response, String user, String[] groups,
-            String sessionid) throws PropertyNotFoundException {
+            String sessionid) throws PropertyNotFoundException, UnsupportedEncodingException, IOException {
         Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         long queryWarnLimit = Long.parseLong(PropertyService.getProperty("database.queryTimeWarnLimit"));
         
@@ -920,13 +932,14 @@
       throws Exception
     {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        PrintWriter out = new PrintWriter(baos);
+     // use UTF-8 encoding for DB query
+        Writer out = new OutputStreamWriter(baos, MetaCatServlet.DEFAULT_ENCODING);
         handleQuery(out, params, null, username, groups, sessionid);
         out.flush();
         baos.flush();
         //baos.close(); 
         //System.out.println("result from query: " + baos.toString());
-        MetacatResultSet rs = new MetacatResultSet(baos.toString());
+        MetacatResultSet rs = new MetacatResultSet(baos.toString(MetaCatServlet.DEFAULT_ENCODING));
         return rs;
     }
     
@@ -1047,8 +1060,7 @@
                     // 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 {
-                        PrintWriter pw = new PrintWriter(out);
-                        doc.toXml(pw, user, groups, withInlineData);               
+                        doc.toXml(out, user, groups, withInlineData);               
                     } catch (McdbException e) {
                         // any exceptions in reading the xml from disc, and we go back to the
                         // old way of creating the xml directly.
@@ -1055,8 +1067,7 @@
                         logMetacat.error("MetaCatServlet.readFromMetacat - could not read from document file " + docid 
                                 + ": " + e.getMessage());
                         e.printStackTrace(System.out);
-                        PrintWriter pw = new PrintWriter(out);
-                        doc.toXmlFromDb(pw, user, groups, withInlineData);
+                        doc.toXmlFromDb(out, user, groups, withInlineData);
                     }
                 } else {
                     // TODO MCD, this should read from disk as well?
@@ -1074,15 +1085,20 @@
                         response.setContentType("text/html"); //MIME type
                     }
                     
-                    PrintWriter pw = new PrintWriter(out);
+                    // TODO: detect actual encoding
+                    Writer w = new OutputStreamWriter(out, response.getCharacterEncoding());
                     
                     // Look up the document type
                     String doctype = doc.getDoctype();
                     // Transform the document to the new doctype
                     DBTransform dbt = new DBTransform();
-                    dbt.transformXMLDocument(doc.toString(user, groups,
-                            withInlineData), doctype, "-//W3C//HTML//EN",
-                            qformat, pw, params, null);
+                    dbt.transformXMLDocument(
+                    		doc.toString(user, groups, withInlineData), 
+                    		doctype, "-//W3C//HTML//EN",
+                            qformat, 
+                            w, 
+                            params, 
+                            null);
                 }
                 
             }
@@ -1280,7 +1296,7 @@
                     
                 } else {
                     // this is metadata doc; add doc to zip
-                    bytestring = doc.toString().getBytes();
+                    bytestring = doc.getBytes();
                     //use provided file name if given
                     if (providedFileName != null && providedFileName.length() > 1) {
                         zentry = new ZipEntry(providedFileName);
@@ -2436,8 +2452,12 @@
                 logMetacat.info("MetaCatServlet.handleInsertMultipartAction - Uploading the following to Metacat:" +
                         fileName + ", " + docid + ", " +
                         username + ", " + groupnames);
-                FileReader fr = new FileReader(newFile);
                 
+                // Read the file with appropriate encoding
+                XmlStreamReader xsr = new XmlStreamReader(new FileInputStream(newFile));
+                String encoding = xsr.getEncoding();
+                Reader fr = new InputStreamReader(new FileInputStream(newFile), encoding);
+                
                 char[] c = new char[1024];
                 int numread = fr.read(c, 0, 1024);
                 StringBuffer sb = new StringBuffer();
@@ -2652,7 +2672,7 @@
      */
     protected void handleSetAccessAction(PrintWriter out, Hashtable<String, String[]> params,
             String username, HttpServletRequest request, HttpServletResponse response) {
-        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
+        
         String permission = null;
         String permType = null;
         String permOrder = null;
@@ -2788,7 +2808,6 @@
                     //insert permission
                     AccessControlForSingleFile accessControl = 
                         new AccessControlForSingleFile(accessionNumber);
-                    Long l = new Long(permission);
                     //System.out.println("permission in MetacatHandler: " + l.longValue());
                     //System.out.println("permission in MetacatHandler: " + Integer.valueOf(AccessControlList.intValue(permission)).longValue());
                     accessControl.insertPermissions(principal, Integer.valueOf(AccessControlList.intValue(permission)).longValue(), permType, permOrder);
@@ -2835,7 +2854,6 @@
      */
     private String getFieldValueForDoc(String accessionNumber, String fieldName)
     throws Exception {
-        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         if (accessionNumber == null || accessionNumber.equals("")
         || fieldName == null || fieldName.equals("")) { throw new Exception(
                 "Docid or field name was not specified"); }
@@ -2936,36 +2954,40 @@
      * A method to output setAccess action result
      */
     private void outputResponse(Vector<String> successList, Vector<String> errorList,
-            PrintWriter out) {
-        boolean error = false;
-        boolean success = false;
-        // Output prolog
-        out.println(PROLOG);
-        // output success message
-        if (successList != null) {
-            for (int i = 0; i < successList.size(); i++) {
-                out.println(SUCCESS);
-                out.println(successList.elementAt(i));
-                out.println(SUCCESSCLOSE);
-                success = true;
-            }
-        }
-        // output error message
-        if (errorList != null) {
-            for (int i = 0; i < errorList.size(); i++) {
-                out.println(ERROR);
-                out.println(errorList.elementAt(i));
-                out.println(ERRORCLOSE);
-                error = true;
-            }
-        }
-        
-        // if no error and no success info, send a error that nothing happened
-        if (!error && !success) {
-            out.println(ERROR);
-            out.println("Nothing happend for setaccess action");
-            out.println(ERRORCLOSE);
-        }
+            Writer out) {
+    	try {
+	        boolean error = false;
+	        boolean success = false;
+	        // Output prolog
+	        out.write(PROLOG);
+	        // output success message
+	        if (successList != null) {
+	            for (int i = 0; i < successList.size(); i++) {
+	                out.write(SUCCESS);
+	                out.write(successList.elementAt(i));
+	                out.write(SUCCESSCLOSE);
+	                success = true;
+	            }
+	        }
+	        // output error message
+	        if (errorList != null) {
+	            for (int i = 0; i < errorList.size(); i++) {
+	                out.write(ERROR);
+	                out.write(errorList.elementAt(i));
+	                out.write(ERRORCLOSE);
+	                error = true;
+	            }
+	        }
+	        
+	        // if no error and no success info, send a error that nothing happened
+	        if (!error && !success) {
+	            out.write(ERROR);
+	            out.write("Nothing happend for setaccess action");
+	            out.write(ERRORCLOSE);
+	        }
+    	} catch (Exception e) {
+			logMetacat.error(e.getMessage(), e);
+		} 
     }
     
     /*
@@ -2973,7 +2995,6 @@
      * append rev for it. The rev come from xml_documents
      */
     private static String appendRev(String docid) throws PropertyNotFoundException, SQLException, McdbDocNotFoundException {
-//        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         String newAccNum = null;
         String separator = PropertyService.getProperty("document.accNumSeparator");
         int firstIndex = docid.indexOf(separator);
@@ -3003,7 +3024,6 @@
      * @param request a servlet request, from which we determine the context
      */
     protected void scheduleSitemapGeneration(HttpServletRequest request) {
-        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
         if (!_sitemapScheduled) {
             String directoryName = null;
             String skin = null;
Index: src/edu/ucsb/nceas/metacat/McdbException.java
===================================================================
--- src/edu/ucsb/nceas/metacat/McdbException.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/McdbException.java	(revision 5752)
@@ -27,7 +27,9 @@
 
 package edu.ucsb.nceas.metacat;
 
+import java.io.IOException;
 import java.io.PrintWriter;
+import java.io.Writer;
 
 /**
  * Exception thrown when an error occurs because a problem occurred in
@@ -113,7 +115,7 @@
    * is null, and if the tunnelled exception is not null, then
    * it returns the message from the tunnelled exception.
    */
-  public void toXml(PrintWriter pw) {
+  public void toXml(Writer pw) {
     String msg = super.getMessage();
 
     if (msg == null && exception != null) {
@@ -120,8 +122,13 @@
       msg = exception.getMessage();
     }
 
-    pw.println("<?xml version=\"1.0\"?>");
-    pw.println("<error>" + msg + "</error>");
-    pw.flush();
+    try {
+		pw.write("<?xml version=\"1.0\"?>");
+		pw.write("<error>" + msg + "</error>");
+	    pw.flush();
+	} catch (IOException e) {
+		e.printStackTrace();
+	}
+    
   }
 }
Index: src/edu/ucsb/nceas/metacat/DBQuery.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 5752)
@@ -30,24 +30,34 @@
 
 package edu.ucsb.nceas.metacat;
 
-import java.io.*;
-import java.util.zip.*;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.util.*;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+import java.util.Vector;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
 import org.apache.log4j.Logger;
 
-import org.w3c.dom.*;
-import javax.xml.parsers.DocumentBuilderFactory;
-import org.xml.sax.InputSource;
-import org.w3c.dom.ls.*;
-
 import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
 import edu.ucsb.nceas.metacat.database.DBConnection;
 import edu.ucsb.nceas.metacat.database.DBConnectionPool;
@@ -113,7 +123,7 @@
      * the main routine used to test the DBQuery utility.
      * <p>
      * Usage: java DBQuery <xmlfile>
-     *
+     * NOTE: encoding should be provided for best results
      * @param xmlfile the filename of the xml file containing the query
      */
     static public void main(String[] args)
@@ -149,7 +159,7 @@
 
                 // Execute the query
                 DBQuery queryobj = new DBQuery();
-                FileReader xml = new FileReader(new File(xmlfile));
+                Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
                 Hashtable nodelist = null;
                 //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
 
@@ -187,7 +197,7 @@
                 //write into a file "result.txt"
                 if (!showRuntime) {
                     File f = new File("./result.txt");
-                    FileWriter fw = new FileWriter(f);
+                    Writer fw = new OutputStreamWriter(new FileOutputStream(f));
                     BufferedWriter out = new BufferedWriter(fw);
                     out.write(result.toString());
                     out.flush();
@@ -288,7 +298,7 @@
    * @param sessionid  the sessionid
    */
   public void findDocuments(HttpServletResponse response,
-                                       PrintWriter out, Hashtable params,
+                                       Writer out, Hashtable params,
                                        String user, String[] groups,
                                        String sessionid) throws PropertyNotFoundException
   {
@@ -309,7 +319,7 @@
      * @param sessionid  the sessionid
      */
     public void findDocuments(HttpServletResponse response,
-                                         PrintWriter out, Hashtable params,
+                                         Writer out, Hashtable params,
                                          String user, String[] groups,
                                          String sessionid, boolean useXMLIndex)
     {
@@ -379,7 +389,7 @@
       {
         //knb format, in this case we will get whole result and sent it out
         response.setContentType("text/html");
-        PrintWriter nonout = null;
+        Writer nonout = null;
         StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
                                                 groups, useXMLIndex, pagesize, 
                                                 pagestart, sessionid, qformat);
@@ -440,7 +450,7 @@
    */
     public StringBuffer createResultDocument(String xmlquery,
                                               QuerySpecification qspec,
-                                              PrintWriter out,
+                                              Writer out,
                                               String user, String[] groups,
                                               boolean useXMLIndex)
     {
@@ -455,7 +465,7 @@
    */
   public StringBuffer createResultDocument(String xmlquery,
                                             QuerySpecification qspec,
-                                            PrintWriter out,
+                                            Writer out,
                                             String user, String[] groups,
                                             boolean useXMLIndex, int pagesize,
                                             int pagestart, String sessionid, 
@@ -481,7 +491,11 @@
     //send out a new query
     if (out != null)
     {
-      out.println(resultset.toString());
+    	try {
+    	  out.write(resultset.toString());
+		} catch (IOException e) {
+			logMetacat.error(e.getMessage(), e);
+		}
     }
     if (qspec != null)
     {
@@ -543,7 +557,11 @@
     resultset.append(closeRestultset);
     if (out != null)
     {
-      out.println(closeRestultset);
+      try {
+		out.write(closeRestultset);
+		} catch (IOException e) {
+			logMetacat.error(e.getMessage(), e);
+		}
     }
 
     //default to returning the whole resultset
@@ -554,7 +572,7 @@
      * Find the doc list which match the query
      */
     private StringBuffer findResultDoclist(QuerySpecification qspec,
-                                      PrintWriter out,
+                                      Writer out,
                                       String user, String[]groups,
                                       DBConnection dbconn, boolean useXMLIndex,
                                       int pagesize, int pagestart, String sessionid, 
@@ -651,7 +669,7 @@
    	    	logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    	    	 if (out != null)
    	         {
-   	             out.println(cachedResult);
+   	             out.write(cachedResult);
    	         }
    	    	 resultsetBuffer.append(cachedResult);
    	    	 return resultsetBuffer;
@@ -793,7 +811,7 @@
      resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
      if (out != null)
      {
-         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
+         out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
      }
      
      // now we only cached none-paged query and user is public
@@ -814,7 +832,7 @@
      */
     private StringBuffer handleSubsetResult(QuerySpecification qspec,
                                            StringBuffer resultset,
-                                           PrintWriter out, ResultDocumentSet partOfDoclist,
+                                           Writer out, ResultDocumentSet partOfDoclist,
                                            String user, String[]groups,
                                        DBConnection dbconn, boolean useXMLIndex,
                                        String qformat)
@@ -951,7 +969,7 @@
          //send single element to output
          if (out != null)
          {
-             out.println(xmlElement);
+             out.write(xmlElement);
          }
          resultset.append(xmlElement);
      }//while
@@ -978,7 +996,7 @@
          //send single element to output
          if (out != null)
          {
-             out.println(xmlElement);
+             out.write(xmlElement);
          }
          resultset.append(xmlElement);
      }//while
Index: src/edu/ucsb/nceas/metacat/DBTransform.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBTransform.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/DBTransform.java	(revision 5752)
@@ -27,8 +27,15 @@
 
 package edu.ucsb.nceas.metacat;
 
-import java.io.*;
-import java.sql.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.sql.SQLException;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
@@ -100,36 +107,38 @@
    */
   public void transformXMLDocument(String doc, String sourceType,
                                    String targetType, String qformat,
-                                   PrintWriter pw, Hashtable<String, String[]> param,
+                                   Writer w, Hashtable<String, String[]> param,
                                    String sessionid)
  {
 
-    // Look up the stylesheet for this type combination
-    String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
-    if (xslSystemId != null)
-    {
-      try
-      {// Create a stylesheet from the system id that was found
-        doc = removeDOCTYPE(doc);
-        StringReader xml = new StringReader(doc);
-        StreamResult result = new StreamResult(pw);
-        doTransform(xml, result, xslSystemId, param, qformat, sessionid);
-
+	  String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
+	  try {
+		  // Look up the stylesheet for this type combination
+		  if (xslSystemId != null) {
+			// Create a stylesheet from the system id that was found
+			doc = removeDOCTYPE(doc);
+			StringReader xml = new StringReader(doc);
+			StreamResult result = new StreamResult(w);
+			doTransform(xml, result, xslSystemId, param, qformat, sessionid);
+		  }
+		  else {
+			  // No stylesheet registered form this document type, so just return the
+			  // XML stream we were passed
+			  w.write(doc);
+		  }
       }
       catch (Exception e)
       {
-        pw.println(xslSystemId + ": Error transforming document in " +
-                   "DBTransform.transformXMLDocument: " +
-                   e.getMessage());
+    	  try {
+			w.write(xslSystemId + ": Error transforming document in " +
+			           "DBTransform.transformXMLDocument: " +
+			           e.getMessage());
+		} catch (IOException e1) {
+			logMetacat.error(e1.getMessage(), e1);
+		}
 
       }
-    }
-    else
-    {
-      // No stylesheet registered form this document type, so just return the
-      // XML stream we were passed
-      pw.print(doc);
-    }
+    
   }
   
   /**
@@ -174,46 +183,7 @@
     }
   }
 
-  /**
-   * Method to do transform for a string reader
-   * @param doc the document to be transformed
-   * @param sourcetype the document type of the source
-   * @param targettype the target document type
-   * @param qformat the name of the style set to use
-   * @param pw the PrintWriter to which output is printed
-   * @param params some parameters for eml2 transformation
-   */
-   public void transformXMLDocument(StringReader docContent, String sourceType,
-                                   String targetType, String qformat,
-                                   PrintWriter pw, Hashtable<String, String[]> param,
-                                   String sessionid)
-   {
-     // Look up the stylesheet for this type combination
-    String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
-    if (xslSystemId != null)
-    {
-      try
-      {// Create a stylesheet from the system id that was found
-        StreamResult result = new StreamResult(pw);
-        doTransform(docContent, result, xslSystemId, param, qformat, sessionid);
 
-      }
-      catch (Exception e)
-      {
-        pw.println(xslSystemId + ": Error transforming document in " +
-                   "DBTransform.transformXMLDocument: " +
-                   e.getMessage());
-
-      }
-    }
-    else
-    {
-      // No stylesheet registered form this document type, so just return the
-      // XML stream we were passed
-      pw.print(docContent);
-    }
-   }
-
   /**
    * Reads skin's config file if it exists, and populates Transformer paramaters
    * with its contents.
@@ -485,18 +455,20 @@
 
           // Create a test document
           StringBuffer testdoc = new StringBuffer();
-          testdoc.append("<?xml version=\"1.0\"?>");
+          String encoding = "UTF-8";
+          testdoc.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
           testdoc.append("<eml-dataset><metafile_id>NCEAS-0001</metafile_id>");
           testdoc.append("<dataset_id>DS001</dataset_id>");
           testdoc.append("<title>My test doc</title></eml-dataset>");
 
           // Transform the document to the new doctype
+          Writer w = new OutputStreamWriter(System.out, encoding);
           DBTransform dbt = new DBTransform();
           dbt.transformXMLDocument(testdoc.toString(),
                                    "-//NCEAS//eml-dataset//EN",
                                    "-//W3C//HTML//EN",
                                    "knb",
-                                   new PrintWriter(System.out), null, null);
+                                   w, null, null);
 
         } catch (Exception e) {
           System.err.println("EXCEPTION HANDLING REQUIRED");
Index: src/edu/ucsb/nceas/metacat/DBSAXHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/DBSAXHandler.java	(revision 5752)
@@ -100,6 +100,8 @@
     protected String[] groups = null;
 
     protected String pub = null;
+    
+	protected String encoding = null;
 
 //    private boolean endDocument = false;
 
@@ -781,7 +783,15 @@
        this.isRevisionDoc = isRevisionDoc;   
     }
 
-    /* Method to write a text buffer for DBSAXNode */
+    public String getEncoding() {
+		return encoding;
+	}
+
+	public void setEncoding(String encoding) {
+		this.encoding = encoding;
+	}
+
+	/* Method to write a text buffer for DBSAXNode */
     protected long writeTextForDBSAXNode(long previousEndNodeId,
             StringBuffer strBuffer, DBSAXNode node) throws SAXException
     {
Index: src/edu/ucsb/nceas/metacat/Eml200SAXHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/Eml200SAXHandler.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/Eml200SAXHandler.java	(revision 5752)
@@ -29,10 +29,15 @@
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.io.Writer;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -213,7 +218,7 @@
 
     private Hashtable inlineDataNameSpace = null;
 
-    private FileWriter inlineDataFileWriter = null;
+    private Writer inlineDataFileWriter = null;
 
     private String inlineDataFileName = null;
 
@@ -629,7 +634,7 @@
                 // the new file name will look like docid.rev.2
                 inlineDataFileName = docidWithoutRev + seperator + revision
                         + seperator + inLineDataIndex;
-                inlineDataFileWriter = createInlineDataFileWriter(inlineDataFileName);
+                inlineDataFileWriter = createInlineDataFileWriter(inlineDataFileName, encoding);
                 // put the inline file id into a vector. If upload failed,
                 // metacat will
                 // delete the inline data file
@@ -1514,7 +1519,7 @@
 			File inlineDataDirectory = new File(path);
 			File inlineDataFile = new File(inlineDataDirectory, fileName);
 
-			FileReader inlineFileReader = new FileReader(inlineDataFile);
+			Reader inlineFileReader = new InputStreamReader(new FileInputStream(inlineDataFile), encoding);
 			BufferedReader inlineStringReader = new BufferedReader(inlineFileReader);
 			String string = inlineStringReader.readLine();
 			// at the end oldstring will be null
@@ -2366,10 +2371,10 @@
     }//deleteAccessSubTreeRecord
 
     // open a file writer for writing inline data to file
-    private FileWriter createInlineDataFileWriter(String fileName)
+    private Writer createInlineDataFileWriter(String fileName, String encoding)
             throws SAXException
     {
-        FileWriter writer = null;
+        Writer writer = null;
         String path;
         try {
         	 path = PropertyService.getProperty("application.inlinedatafilepath");
@@ -2383,7 +2388,7 @@
         logMetacat.info("inline file name: " + newFile);
         try {
             // true means append
-            writer = new FileWriter(newFile, true);
+        	writer = new OutputStreamWriter(new FileOutputStream(newFile, true), encoding);
         } catch (IOException ioe) {
             throw new SAXException(ioe.getMessage());
         }
@@ -2391,7 +2396,7 @@
     }
 
     // write inline data into file system and return file name(without path)
-    private void writeInlineDataIntoFile(FileWriter writer, StringBuffer data)
+    private void writeInlineDataIntoFile(Writer writer, StringBuffer data)
             throws SAXException
     {
         try {
@@ -2422,9 +2427,9 @@
 			File oldDataFile = new File(inlineDataDirectory, oldFileName);
 			File newDataFile = new File(inlineDataDirectory, newFileName);
 
-            FileReader oldFileReader = new FileReader(oldDataFile);
+            Reader oldFileReader = new InputStreamReader(new FileInputStream(oldDataFile), encoding);
             BufferedReader oldStringReader = new BufferedReader(oldFileReader);
-            FileReader newFileReader = new FileReader(newDataFile);
+            Reader newFileReader = new InputStreamReader(new FileInputStream(newDataFile), encoding);
             BufferedReader newStringReader = new BufferedReader(newFileReader);
             // read first line of data
             String oldString = oldStringReader.readLine();
@@ -2476,8 +2481,8 @@
 		logMetacat.info("the inline data file name from xml_access is: "
 				+ oldInlineInternalFileName);
 
-		FileReader oldFileReader = null;
-		FileWriter newFileWriter = null;
+		InputStream oldFileReader = null;
+		OutputStream newFileWriter = null;
 		try {
 			String path = PropertyService.getProperty("application.inlinedatafilepath");
 			// the new file name will look like path/docid.rev.2
@@ -2485,9 +2490,9 @@
 			File oldDataFile = new File(inlineDataDirectory, oldInlineInternalFileName);
 			File newDataFile = new File(inlineDataDirectory, newFileName);
 
-			oldFileReader = new FileReader(oldDataFile);
-			newFileWriter = new FileWriter(newDataFile);
-			char[] buf = new char[4 * 1024]; // 4K buffer
+			oldFileReader = new FileInputStream(oldDataFile);
+			newFileWriter = new FileOutputStream(newDataFile);
+			byte[] buf = new byte[4 * 1024]; // 4K buffer
 			int b = oldFileReader.read(buf);
 			while (b != -1) {
 				newFileWriter.write(buf, 0, b);
@@ -2548,10 +2553,10 @@
 	 * In eml2, the inline data wouldn't store in db, it store in file system
 	 * The db stores file name(without path).
 	 */
-	public static Reader readInlineDataFromFileSystem(String fileName)
+	public static Reader readInlineDataFromFileSystem(String fileName, String encoding)
 			throws McdbException {
 		// BufferedReader stringReader = null;
-		FileReader fileReader = null;
+		Reader fileReader = null;
 		try {
 			String path = PropertyService.getProperty("application.inlinedatafilepath");
 			// the new file name will look like path/docid.rev.2
@@ -2558,7 +2563,7 @@
 			File inlineDataDirectory = new File(path);
 			File dataFile = new File(inlineDataDirectory, fileName);
 
-			fileReader = new FileReader(dataFile);
+			fileReader = new InputStreamReader(new FileInputStream(dataFile), encoding);
 			// stringReader = new BufferedReader(fileReader);
 		} catch (Exception e) {
 			throw new McdbException(e.getMessage());
Index: src/edu/ucsb/nceas/metacat/Eml210SAXHandler.java
===================================================================
--- src/edu/ucsb/nceas/metacat/Eml210SAXHandler.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/Eml210SAXHandler.java	(revision 5752)
@@ -27,12 +27,11 @@
 
 package edu.ucsb.nceas.metacat;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.Reader;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -115,7 +114,7 @@
 
 	private Hashtable<String, String> inlineDataNameSpace = null;
 
-	private FileWriter inlineDataFileWriter = null;
+	private Writer inlineDataFileWriter = null;
 
 	private String inlineDataFileName = null;
 
@@ -310,7 +309,7 @@
 				// the new file name will look like docid.rev.2
 				inlineDataFileName = docidWithoutRev + seperator + revision + seperator
 						+ distributionIndex;
-				inlineDataFileWriter = createInlineDataFileWriter(inlineDataFileName);
+				inlineDataFileWriter = createInlineDataFileWriter(inlineDataFileName, encoding);
 				// put the inline file id into a vector. If upload failed,
 				// metacat will delete the inline data file
 				inlineFileIdList.add(inlineDataFileName);
@@ -1670,8 +1669,8 @@
 	}// deleteAccessSubTreeRecord
 
 	// open a file writer for writing inline data to file
-	private FileWriter createInlineDataFileWriter(String fileName) throws SAXException {
-		FileWriter writer = null;
+	private Writer createInlineDataFileWriter(String fileName, String encoding) throws SAXException {
+		Writer writer = null;
 		String path;
 		try {
 			path = PropertyService.getProperty("application.inlinedatafilepath");
@@ -1685,7 +1684,7 @@
 		logMetacat.debug("inline file name: " + newFile);
 		try {
 			// true means append
-			writer = new FileWriter(newFile, true);
+			writer = new OutputStreamWriter(new FileOutputStream(newFile, true), encoding);
 		} catch (IOException ioe) {
 			throw new SAXException(ioe.getMessage());
 		}
@@ -1693,7 +1692,7 @@
 	}
 
 	// write inline data into file system and return file name(without path)
-	private void writeInlineDataIntoFile(FileWriter writer, StringBuffer data)
+	private void writeInlineDataIntoFile(Writer writer, StringBuffer data)
 			throws SAXException {
 		try {
 			writer.write(data.toString());
@@ -1703,55 +1702,8 @@
 		}
 	}
 
-	/*
-	 * In eml2, the inline data wouldn't store in db, it store in file system
-	 * The db stores file name(without path). We got the old file name from db
-	 * and compare to the new in line data file
-	 */
-	public boolean compareInlineDataFiles(String oldFileName, String newFileName)
-			throws McdbException {
-		// this method need to be testing
-		boolean same = true;
-		String data = null;
-		try {
-			String path = PropertyService.getProperty("application.inlinedatafilepath");
-			// the new file name will look like path/docid.rev.2
-			File inlineDataDirectory = new File(path);
-			File oldDataFile = new File(inlineDataDirectory, oldFileName);
-			File newDataFile = new File(inlineDataDirectory, newFileName);
 
-			FileReader oldFileReader = new FileReader(oldDataFile);
-			BufferedReader oldStringReader = new BufferedReader(oldFileReader);
-			FileReader newFileReader = new FileReader(newDataFile);
-			BufferedReader newStringReader = new BufferedReader(newFileReader);
-			// read first line of data
-			String oldString = oldStringReader.readLine();
-			String newString = newStringReader.readLine();
 
-			// at the end oldstring will be null
-			while (oldString != null) {
-				oldString = oldStringReader.readLine();
-				newString = newStringReader.readLine();
-				if (!oldString.equals(newString)) {
-					same = false;
-					break;
-				}
-			}
-
-			// if oldString is null but newString is not null, they are same
-			if (same) {
-				if (newString != null) {
-					same = false;
-				}
-			}
-
-		} catch (Exception e) {
-			throw new McdbException(e.getMessage());
-		}
-		logMetacat.debug("the inline data retrieved from file: " + data);
-		return same;
-	}
-
 	// if xml file failed to upload, we need to call this method to delete
 	// the inline data already in file system
 	public void deleteInlineFiles() throws SAXException {
@@ -1778,29 +1730,6 @@
 
 	}
 
-	/*
-	 * In eml2, the inline data wouldn't store in db, it store in file system
-	 * The db stores file name(without path).
-	 */
-	public static Reader readInlineDataFromFileSystem(String fileName)
-			throws McdbException {
-		// BufferedReader stringReader = null;
-		FileReader fileReader = null;
-		try {
-			String path = PropertyService.getProperty("application.inlinedatafilepath");
-			// the new file name will look like path/docid.rev.2
-			File inlineDataDirectory = new File(path);
-			File dataFile = new File(inlineDataDirectory, fileName);
-
-			fileReader = new FileReader(dataFile);
-			// stringReader = new BufferedReader(fileReader);
-		} catch (Exception e) {
-			throw new McdbException(e.getMessage());
-		}
-		// return stringReader;
-		return fileReader;
-	}
-
 	/* Delete relations */
 	private void deleteRelations() throws SAXException {
 		PreparedStatement pStmt = null;
Index: src/edu/ucsb/nceas/metacat/replication/ReplicationService.java
===================================================================
--- src/edu/ucsb/nceas/metacat/replication/ReplicationService.java	(revision 5751)
+++ src/edu/ucsb/nceas/metacat/replication/ReplicationService.java	(revision 5752)
@@ -33,6 +33,7 @@
 import java.net.*;
 import java.text.*;
 
+import javax.servlet.ServletOutputStream;
 import javax.servlet.http.*;
 
 import edu.ucsb.nceas.metacat.DocInfoHandler;
@@ -1113,13 +1114,13 @@
 			// it to disk.
 			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
 					|| FileUtil.getFileSize(documentPath) == 0) {
-				FileWriter fileWriter = new FileWriter(documentPath);
-				di.toXml(fileWriter, null, null, true);
+				FileOutputStream fos = new FileOutputStream(documentPath);
+				di.toXml(fos, null, null, true);
 			}
 
-			// read the file from disk and sent it to PrintWriter
-			// PrintWriter out = new PrintWriter(streamOut);
-			di.readFromFileSystem(out, null, null, documentPath);
+			// read the file from disk and send it to outputstream
+			OutputStream outputStream = response.getOutputStream();
+			di.readFromFileSystem(outputStream, null, null, documentPath);
 
 			// response.setContentType("text/xml");
 			// out.print(di.toString(null, null, true));
