|Title|Optimize FileUtil.copyContents()
|Date|23-Feb-2006 19:49:19 EET
|Version|2.3.72
|Submitter|mchristensen
|[Bug criticality]|MediumBug
|Browser version|IE 6.0
|[Bug status]|ClosedBug
|[PageProvider] used|
|Servlet Container|Tomcat5
|Operating System|Linux
|URL|
|Java version|1.4.2

Uploading a 30MB attachment takes 10-20 minutes.  Researching the problem I traced the slowness to BasicAttachmentProvider.putAttachementData()'s call to com.ecyrd.jspwiki.FileUtil.copyContents().

This method copies the attachments byte-by-byte, which results in 90+% of the delay.  Please make this a buffered operation.

Instead of,

{{{
    public static void copyContents( InputStream in, OutputStream out )
        throws IOException
    {
        int c;
        
        while( (c = in.read()) != -1  )
        {
            out.write( c );
        }

        out.flush();
    }
}}}

How about something like the following (compiled but not tested.  I have used this approach before with MASSIVE speed improvement over the byte-by-byte method)

{{{
	public static void copyContents(InputStream in, OutputStream out)
			throws IOException {
		byte[] buf = new byte[4096];
		int bytesRead = 0;

		while ((bytesRead = in.read(buf)) > 0) {
			out.write(buf, 0, bytesRead);
		}

		out.flush();
	}
}}}

----

Oops... I thought I already had.  Thanks, will be fixed.

-- JanneJalkanen

----
Be careful with the fix! I had also changed that code because it performs horrible but there whas a problem with the javaclass TranslatorReader (I measured a 5-7x improvement). I had to revert my change! see [BugClickingOnmoreInfoOnHttpwww.jspwiki.orgwikiMainTakesAVeryLongTime] at 'Oef, A major setback'

-- KeesKuip

----

Since 2.3 no longer uses the TranslatorReader, we can safely go and take this patch in.  It's in 2.3.100.  Thanks!

-- JanneJalkanen, 13-Apr-2006