Index: /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java
===================================================================
--- /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java	(revision 1044)
+++ /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/plugin/fileupload/FileUploadPlugin.java	(working copy)
@@ -1,25 +1,25 @@
-/*
- * Copyright 2006, XpertNet SARL, and individual contributors as indicated
- * by the contributors.txt.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- *
- * @author ludovic
- * @author sdumitriu
- */
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author ludovic
+ * @author sdumitriu
+ */
 
 package com.xpn.xwiki.plugin.fileupload;
 
@@ -29,10 +29,7 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.fileupload.DefaultFileItem;
-import org.apache.commons.fileupload.DiskFileUpload;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadException;
+import org.apache.commons.fileupload.*;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -72,7 +69,7 @@
     public Api getPluginApi(XWikiPluginInterface plugin, XWikiContext context) {
         return new FileUploadPluginApi((FileUploadPlugin) plugin, context);
     }
-    
+
     /**
      *  endRendering to make sure we don't leave files in temp directories
      * @param context Context of the request
@@ -127,8 +124,9 @@
         fileupload.setSizeMax(uploadMaxSize);
         fileupload.setSizeThreshold(uploadSizeThreashold);
         context.put("fileupload", fileupload);
+        XWikiRequest request = context.getRequest() ;
 
-        if (tempdir!=null) {
+        if (tempdir != null) {
             fileupload.setRepositoryPath(tempdir);
             (new File(tempdir)).mkdirs();
         }
@@ -137,11 +135,14 @@
         }
 
         try {
-            XWikiRequest request = context.getRequest();
             List list = fileupload.parseRequest(request.getHttpServletRequest());
-            // We store the file list in the context
+            // We store the file list in the context, throw Exception ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE
             context.put("fileuploadlist", list);
-        } catch (FileUploadException e) {
+        }catch (FileUploadBase.SizeLimitExceededException  e) {
+              throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_FILE_EXCEPTION_MAXSIZE,
+                    "Exception uploaded file");
+        }catch(FileUploadException e){
             throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                     XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION,
                     "Exception while parsing uploaded file", e);
@@ -186,15 +187,19 @@
             return null;
 
         byte[] data = new byte[(int)fileitem.getSize()];
-        InputStream fileis = null;
-        try {
-            fileis = fileitem.getInputStream();
-            fileis.read(data);
-            fileis.close();
-        } catch (IOException e) {
+        try{
+            InputStream fileis = fileitem.getInputStream();
+            if(fileis != null){
+                fileis.read(data);
+                fileis.close();
+            }
+
+        } catch (java.lang.OutOfMemoryError e) {
             throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
-                    XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION,
-                    "Exception while reading uploaded parsed file", e);
+                   XWikiException.ERROR_XWIKI_APP_JAVA_HEAP_SPACE,"Java Heap Space, Out of memory exception",e);
+        }catch(IOException ie){
+            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
+                    XWikiException.ERROR_XWIKI_APP_UPLOAD_FILE_EXCEPTION,"Exception while reading uploaded parsed file",ie) ;
         }
         return data;
     }
