[136935] Slow Performance and Out of Memory Error Editing Large XML Document
diff --git a/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation/XMLValidator.java b/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation/XMLValidator.java
index 4bcd2fd..c777ac7 100644
--- a/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation/XMLValidator.java
+++ b/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation/XMLValidator.java
@@ -11,13 +11,13 @@
 
 package org.eclipse.wst.xml.core.internal.validation;
 
-import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
-import java.io.StringReader;
 import java.net.ConnectException;
 import java.net.URL;
 import java.net.UnknownHostException;
@@ -198,29 +198,6 @@
     return validate(uri, null, new XMLValidationConfiguration());  
   }
 
-  final String createStringForInputStream(InputStream inputStream)
-  {
-    // Here we are reading the file and storing to a stringbuffer.
-    StringBuffer fileString = new StringBuffer();
-    try
-    {
-      InputStreamReader inputReader = new InputStreamReader(inputStream, "UTF-8");
-      BufferedReader reader = new BufferedReader(inputReader);
-      char[] chars = new char[1024];
-      int numberRead = reader.read(chars);
-      while (numberRead != -1)
-      {
-        fileString.append(chars, 0, numberRead);
-        numberRead = reader.read(chars);
-      }
-    }
-    catch (Exception e)
-    {
-      //TODO: log error message
-      //e.printStackTrace();
-    }
-    return fileString.toString();
-  }
   /**
    * Validate the inputStream
    * 
@@ -247,13 +224,52 @@
   public XMLValidationReport validate(String uri, InputStream inputStream, XMLValidationConfiguration configuration)
   {
     Reader reader1 = null; // Used for the preparse.
-    Reader reader2 = null; // Used for validation parse.
-    
+    //Reader reader2 = null; // Used for validation parse.
+    ByteArrayInputStream baInputStream = null;    
     if (inputStream != null)
     {  
-      String string = createStringForInputStream(inputStream);
-      reader1 = new StringReader(string);
-      reader2 = new StringReader(string);
+      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+      byte[] byteArray = new byte[1024];
+      int bytesRead;
+      try
+      {
+        while((bytesRead = inputStream.read(byteArray)) != -1)
+        {
+          outputStream.write(byteArray, 0, bytesRead);
+          byteArray = new byte[1024];
+        }
+      }
+      catch(IOException e)
+      {
+        System.out.println(e);
+      }
+      finally
+      {
+        if(inputStream != null)
+        {
+      	  try
+      	  {
+      	    inputStream.close();
+      	    inputStream = null;
+      	  }
+      	  catch(IOException e)
+      	  {
+      	    // Do nothing.
+      	  }
+        }
+      }
+      baInputStream = new ByteArrayInputStream(outputStream.toByteArray());
+      try
+      {
+        outputStream.close();
+        outputStream = null;
+      }
+      catch(IOException e)
+      {
+        // Do nothing.
+      }
+      reader1 = new InputStreamReader(baInputStream);
+      //reader2 = new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray()));
     } 
         
     XMLValidationInfo valinfo = new XMLValidationInfo(uri);
@@ -262,6 +278,19 @@
     try
     {  
         helper.computeValidationInformation(uri, reader1, uriResolver);
+        if(reader1 != null)
+        {
+          baInputStream.reset();
+          try
+          {
+          	reader1.close();
+          }
+          catch(IOException e)
+          {
+          	
+          }
+          reader1 = new InputStreamReader(baInputStream);
+        }
         valinfo.setDTDEncountered(helper.isDTDEncountered);
         valinfo.setElementDeclarationCount(helper.numDTDElements);
         valinfo.setNamespaceEncountered(helper.isNamespaceEncountered);
@@ -272,7 +301,7 @@
         reader.setErrorHandler(errorhandler);
         
         InputSource inputSource = new InputSource(uri);
-        inputSource.setCharacterStream(reader2);
+        inputSource.setCharacterStream(reader1);
         reader.parse(inputSource);   
         if(configuration.getFeature(XMLValidationConfiguration.WARN_NO_GRAMMAR) && 
         		valinfo.isValid() && !helper.isGrammarEncountered)
@@ -293,7 +322,31 @@
     {  
     	LoggerFactory.getLoggerInstance().logError(exception.getLocalizedMessage(), exception);
     }
-     
+    finally
+    {
+      if(reader1 != null)
+      {
+        try
+        {
+          reader1.close();
+        }
+        catch(IOException e)
+        {
+          // Do nothing.
+        }
+      }
+      if(baInputStream != null)
+      {
+        try
+        {
+          baInputStream.close();
+        }
+        catch(IOException e)
+        {
+          // Do nothing.
+        }
+      }
+    }
     
     return valinfo;
        
diff --git a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/validation/DelegatingSourceValidator.java b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/validation/DelegatingSourceValidator.java
index b6bd222..ec916e3 100644
--- a/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/validation/DelegatingSourceValidator.java
+++ b/bundles/org.eclipse.wst.xml.ui/src/org/eclipse/wst/xml/ui/internal/validation/DelegatingSourceValidator.java
@@ -1,6 +1,8 @@
 package org.eclipse.wst.xml.ui.internal.validation;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
@@ -193,19 +195,32 @@
 
 				// store the text in a byte array; make a full copy to ease
 				// any threading problems
+				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 				byte[] byteArray;
 				try {
-					byteArray = xmlModel.getStructuredDocument().get().getBytes("UTF-8");
+					int docLength = xmlModel.getStructuredDocument().getLength();
+					for(int i = 0; i < docLength; i = i+1024 < docLength ? i+1024 : docLength) {
+						String docString = xmlModel.getStructuredDocument().get(i, 1024);
+						byteArray = docString.getBytes("UTF-8");
+						outputStream.write(byteArray);
+					}
+					//byteArray = xmlModel.getStructuredDocument().get().getBytes("UTF-8");
 				} catch (UnsupportedEncodingException e) {
 					// Not likely to happen
 					byteArray = xmlModel.getStructuredDocument().get().getBytes();
 				}
+				catch(BadLocationException e){
+					
+				}
+				catch(IOException e){
+					
+				}
 
 				if (isDelegateValidatorEnabled(file)) {
 					IValidator validator = getDelegateValidator();
 					if (validator != null) {
 						// Validate the file:
-						IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(byteArray), file);
+						IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(outputStream.toByteArray()), file);
 						MyReporter vReporter = new MyReporter();
 						if (validator instanceof IValidatorJob) {
 							((IValidatorJob) validator).validateInJob(vHelper, vReporter);